document.ex (2551B)
1 defmodule Absinthe.Language.Document do 2 @moduledoc false 3 4 alias Absinthe.{Blueprint, Language} 5 6 defstruct definitions: [], 7 loc: %{line: nil} 8 9 @typedoc false 10 @type t :: %__MODULE__{ 11 definitions: [Absinthe.Language.t()], 12 loc: Language.loc_t() 13 } 14 15 @doc "Extract a named operation definition from a document" 16 @spec get_operation(t, String.t()) :: nil | Absinthe.Language.OperationDefinition.t() 17 def get_operation(%{definitions: definitions}, name) do 18 definitions 19 |> Enum.find(nil, fn 20 %Language.OperationDefinition{name: ^name} -> 21 true 22 23 _ -> 24 false 25 end) 26 end 27 28 @doc false 29 @spec fragments_by_name(Absinthe.Language.Document.t()) :: %{ 30 String.t() => Absinthe.Language.Fragment.t() 31 } 32 def fragments_by_name(%{definitions: definitions}) do 33 definitions 34 |> Enum.reduce(%{}, fn statement, memo -> 35 case statement do 36 %Absinthe.Language.Fragment{} -> 37 memo |> Map.put(statement.name, statement) 38 39 _ -> 40 memo 41 end 42 end) 43 end 44 45 defimpl Blueprint.Draft do 46 @operations [ 47 Language.OperationDefinition 48 ] 49 @types [ 50 Language.SchemaDefinition, 51 Language.EnumTypeDefinition, 52 Language.InputObjectTypeDefinition, 53 Language.InputValueDefinition, 54 Language.InterfaceTypeDefinition, 55 Language.ObjectTypeDefinition, 56 Language.ScalarTypeDefinition, 57 Language.UnionTypeDefinition 58 ] 59 @directives [ 60 Language.DirectiveDefinition 61 ] 62 @fragments [ 63 Language.Fragment 64 ] 65 66 def convert(node, bp) do 67 Enum.reduce(node.definitions, bp, &convert_definition(&1, node, &2)) 68 end 69 70 defp convert_definition(%struct{} = node, doc, blueprint) when struct in @operations do 71 update_in(blueprint.operations, &[Blueprint.Draft.convert(node, doc) | &1]) 72 end 73 74 defp convert_definition(%struct{} = node, doc, blueprint) when struct in @types do 75 update_in(blueprint.schema_definitions, &[Blueprint.Draft.convert(node, doc) | &1]) 76 end 77 78 defp convert_definition(%struct{} = node, doc, blueprint) when struct in @directives do 79 update_in(blueprint.directives, &[Blueprint.Draft.convert(node, doc) | &1]) 80 end 81 82 defp convert_definition(%struct{} = node, doc, blueprint) when struct in @fragments do 83 update_in(blueprint.fragments, &[Blueprint.Draft.convert(node, doc) | &1]) 84 end 85 end 86 87 defimpl Inspect do 88 defdelegate inspect(term, options), 89 to: Absinthe.Language.Render 90 end 91 end