introspection.ex (2013B)
1 defmodule Absinthe.Introspection do 2 @moduledoc """ 3 Introspection support. 4 5 You can introspect your schema using `__schema`, `__type`, and `__typename`, 6 as [described in the specification](https://facebook.github.io/graphql/#sec-Introspection). 7 8 ## Examples 9 10 Seeing the names of the types in the schema: 11 12 ``` 13 \""" 14 { 15 __schema { 16 types { 17 name 18 } 19 } 20 } 21 \""" 22 |> Absinthe.run(MyApp.Schema) 23 {:ok, 24 %{data: %{ 25 "__schema" => %{ 26 "types" => [ 27 %{"name" => "Boolean"}, 28 %{"name" => "Float"}, 29 %{"name" => "ID"}, 30 %{"name" => "Int"}, 31 %{"name" => "String"}, 32 ... 33 ] 34 } 35 }} 36 } 37 ``` 38 39 Getting the name of the queried type: 40 41 ``` 42 \""" 43 { 44 profile { 45 name 46 __typename 47 } 48 } 49 \""" 50 |> Absinthe.run(MyApp.Schema) 51 {:ok, 52 %{data: %{ 53 "profile" => %{ 54 "name" => "Joe", 55 "__typename" => "Person" 56 } 57 }} 58 } 59 ``` 60 61 Getting the name of the fields for a named type: 62 63 ``` 64 \""" 65 { 66 __type(name: "Person") { 67 fields { 68 name 69 type { 70 kind 71 name 72 } 73 } 74 } 75 } 76 \""" 77 |> Absinthe.run(MyApp.Schema) 78 {:ok, 79 %{data: %{ 80 "__type" => %{ 81 "fields" => [ 82 %{ 83 "name" => "name", 84 "type" => %{"kind" => "SCALAR", "name" => "String"} 85 }, 86 %{ 87 "name" => "age", 88 "type" => %{"kind" => "SCALAR", "name" => "Int"} 89 }, 90 ] 91 } 92 }} 93 } 94 ``` 95 96 (Note that you may have to nest several depths of `type`/`ofType`, as 97 type information includes any wrapping layers of [List](https://facebook.github.io/graphql/#sec-List) 98 and/or [NonNull](https://facebook.github.io/graphql/#sec-Non-null).) 99 """ 100 101 alias Absinthe.Type 102 103 # Determine if a term is an introspection type 104 @doc false 105 @spec type?(any) :: boolean 106 def type?(%Type.Object{name: "__" <> _}), do: true 107 def type?(_), do: false 108 end