object.ex (3385B)
1 defmodule Absinthe.Type.Object do 2 @moduledoc """ 3 Represents a non-leaf node in a GraphQL tree of information. 4 5 Objects represent a list of named fields, each of which yield a value of a 6 specific type. Object values are serialized as unordered maps, where the 7 queried field names (or aliases) are the keys and the result of evaluating the 8 field is the value. 9 10 Also see `Absinthe.Type.Scalar`. 11 12 ## Examples 13 14 Given a type defined as the following (see `Absinthe.Schema.Notation.object/3`): 15 16 ``` 17 @desc "A person" 18 object :person do 19 field :name, :string 20 field :age, :integer 21 field :best_friend, :person 22 field :pets, list_of(:pet) 23 end 24 ``` 25 26 The "Person" type (referred inside Absinthe as `:person`) is an object, with 27 fields that use `Absinthe.Type.Scalar` types (namely `:name` and `:age`), and 28 other `Absinthe.Type.Object` types (`:best_friend` and `:pets`, assuming 29 `:pet` is an object). 30 31 Given we have a query that supports getting a person by name 32 (see `Absinthe.Schema`), and a query document like the following: 33 34 ```graphql 35 { 36 person(name: "Joe") { 37 name 38 best_friend { 39 name 40 age 41 } 42 pets { 43 breed 44 } 45 } 46 } 47 ``` 48 49 We could get a result like this: 50 51 ``` 52 %{ 53 data: %{ 54 "person" => %{ 55 "best_friend" => %{ 56 "name" => "Jill", 57 "age" => 29 58 }, 59 "pets" => [ 60 %{"breed" => "Wyvern"}, 61 %{"breed" => "Royal Griffon"} 62 ] 63 } 64 } 65 } 66 ``` 67 """ 68 69 alias Absinthe.Type 70 use Absinthe.Introspection.TypeKind, :object 71 72 @typedoc """ 73 A defined object type. 74 75 Note new object types (with the exception of the root-level `query`, `mutation`, and `subscription`) 76 should be defined using `Absinthe.Schema.Notation.object/3`. 77 78 * `:name` - The name of the object type. Should be a TitleCased `binary`. Set automatically. 79 * `:description` - A nice description for introspection. 80 * `:fields` - A map of `Absinthe.Type.Field` structs. Usually built via `Absinthe.Schema.Notation.field/4`. 81 * `:interfaces` - A list of interfaces that this type guarantees to implement. See `Absinthe.Type.Interface`. 82 * `:is_type_of` - A function used to identify whether a resolved object belongs to this defined type. For use with `:interfaces` entry and `Absinthe.Type.Interface`. This function will be passed one argument; the object whose type needs to be identified, and should return `true` when the object matches this type. 83 84 The `__private__` and `:__reference__` keys are for internal use. 85 """ 86 @type t :: %__MODULE__{ 87 identifier: atom, 88 name: binary, 89 description: binary, 90 fields: map, 91 interfaces: [Absinthe.Type.Interface.t()], 92 __private__: Keyword.t(), 93 definition: module, 94 __reference__: Type.Reference.t() 95 } 96 97 defstruct identifier: nil, 98 name: nil, 99 description: nil, 100 fields: nil, 101 interfaces: [], 102 __private__: [], 103 definition: nil, 104 __reference__: nil, 105 is_type_of: nil 106 107 @doc false 108 defdelegate functions, to: Absinthe.Blueprint.Schema.ObjectTypeDefinition 109 110 @doc false 111 @spec field(t, atom) :: Absinthe.Type.Field.t() 112 def field(%{fields: fields}, identifier) do 113 fields 114 |> Map.get(identifier) 115 end 116 end