directive.ex (3046B)
1 defmodule Absinthe.Type.Directive do 2 @moduledoc """ 3 Used by the GraphQL runtime as a way of modifying execution 4 behavior. 5 6 Type system creators will usually not create these directly. 7 """ 8 9 alias Absinthe.Type 10 alias Absinthe.Language 11 12 @typedoc """ 13 A defined directive. 14 15 * `:name` - The name of the directive. Should be a lowercase `binary`. Set automatically. 16 * `:description` - A nice description for introspection. 17 * `:args` - A map of `Absinthe.Type.Argument` structs. See `Absinthe.Schema.Notation.arg/2`. 18 * `:locations` - A list of places the directives can be used. 19 * `:repeatable` - A directive may be defined as repeatable by including the “repeatable” keyword 20 21 The `:__reference__` key is for internal use. 22 """ 23 @type t :: %{ 24 name: binary, 25 description: binary, 26 identifier: atom, 27 args: map, 28 locations: [location], 29 expand: (map, Absinthe.Blueprint.node_t() -> atom), 30 definition: module, 31 repeatable: boolean, 32 __private__: Keyword.t(), 33 __reference__: Type.Reference.t() 34 } 35 36 @type location :: 37 :query | :mutation | :field | :fragment_definition | :fragment_spread | :inline_fragment 38 39 defstruct name: nil, 40 description: nil, 41 identifier: nil, 42 args: nil, 43 locations: [], 44 expand: nil, 45 definition: nil, 46 repeatable: false, 47 __private__: [], 48 __reference__: nil 49 50 @doc false 51 defdelegate functions, to: Absinthe.Blueprint.Schema.DirectiveDefinition 52 53 # Whether the directive is active in `place` 54 @doc false 55 @spec on?(t, Language.t()) :: boolean 56 def on?(%{locations: locations}, place) do 57 Enum.any?(locations, &do_on?(&1, place)) 58 end 59 60 # Operations 61 defp do_on?(location, %Language.OperationDefinition{operation: location}), do: true 62 defp do_on?(:field, %Language.Field{}), do: true 63 defp do_on?(:fragment_definition, %Language.Fragment{}), do: true 64 defp do_on?(:fragment_spread, %Language.FragmentSpread{}), do: true 65 defp do_on?(:inline_fragment, %Language.InlineFragment{}), do: true 66 defp do_on?(:schema, %Language.SchemaDefinition{}), do: true 67 defp do_on?(:schema, %Language.SchemaDeclaration{}), do: true 68 defp do_on?(:scalar, %Language.ScalarTypeDefinition{}), do: true 69 defp do_on?(:object, %Language.ObjectTypeDefinition{}), do: true 70 defp do_on?(:field_definition, %Language.FieldDefinition{}), do: true 71 defp do_on?(:interface, %Language.InterfaceTypeDefinition{}), do: true 72 defp do_on?(:union, %Language.UnionTypeDefinition{}), do: true 73 defp do_on?(:enum, %Language.EnumTypeDefinition{}), do: true 74 defp do_on?(:enum_value, %Language.EnumValueDefinition{}), do: true 75 defp do_on?(:input_object, %Language.InputObjectTypeDefinition{}), do: true 76 defp do_on?(:argument_definition, %Language.InputValueDefinition{}), do: true 77 defp do_on?(:input_field_definition, %Language.InputValueDefinition{}), do: true 78 defp do_on?(_, _), do: false 79 end