@@ -2,8 +2,7 @@ defmodule GraphQL.Schema do
22
33 @ type t :: % GraphQL.Schema {
44 query: Map ,
5- mutation: Map ,
6- types: [ GraphQL.Type.AbstractType . t | GraphQL.Type.ObjectType . t ]
5+ mutation: Map
76 }
87
98 alias GraphQL.Type.Interface
@@ -13,7 +12,16 @@ defmodule GraphQL.Schema do
1312 alias GraphQL.Type.CompositeType
1413 alias GraphQL.Lang.AST.Nodes
1514
16- defstruct query: nil , mutation: nil , types: [ ]
15+ defstruct query: nil , mutation: nil , type_cache: nil
16+
17+ def with_type_cache ( schema = % { type_cache: nil } ) , do: new ( schema )
18+ def with_type_cache ( schema ) , do: schema
19+
20+ def new ( % { query: query , mutation: mutation } ) do
21+ % GraphQL.Schema { query: query , mutation: mutation , type_cache: do_reduce_types ( query , mutation ) }
22+ end
23+ def new ( % { mutation: mutation } ) , do: new ( % { query: nil , mutation: mutation } )
24+ def new ( % { query: query } ) , do: new ( % { query: query , mutation: nil } )
1725
1826 # FIXME: I think *schema* should be the first argument in this module.
1927 def type_from_ast ( nil , _ ) , do: nil
@@ -24,32 +32,32 @@ defmodule GraphQL.Schema do
2432 % GraphQL.Type.List { ofType: type_from_ast ( input_type_ast . type , schema ) }
2533 end
2634 def type_from_ast ( % { kind: :NamedType } = input_type_ast , schema ) do
27- reduce_types ( schema ) |> Map . get ( input_type_ast . name . value , :not_found )
35+ schema . type_cache |> Map . get ( input_type_ast . name . value , :not_found )
2836 end
2937
30- def reduce_types ( type ) do
38+ defp do_reduce_types ( query , mutation ) do
3139 % { }
32- |> reduce_types ( type . query )
33- |> reduce_types ( type . mutation )
40+ |> reduce_types ( query )
41+ |> reduce_types ( mutation )
3442 |> reduce_types ( Introspection.Schema . type )
3543 end
3644
37- def reduce_types ( typemap , % { ofType: list_type } ) do
45+ defp reduce_types ( typemap , % { ofType: list_type } ) do
3846 reduce_types ( typemap , list_type )
3947 end
4048
41- def reduce_types ( typemap , % Interface { } = type ) do
49+ defp reduce_types ( typemap , % Interface { } = type ) do
4250 Map . put ( typemap , type . name , type )
4351 end
4452
45- def reduce_types ( typemap , % Union { } = type ) do
53+ defp reduce_types ( typemap , % Union { } = type ) do
4654 typemap = Map . put ( typemap , type . name , type )
4755 Enum . reduce ( type . types , typemap , fn ( fieldtype , map ) ->
4856 reduce_types ( map , fieldtype )
4957 end )
5058 end
5159
52- def reduce_types ( typemap , % ObjectType { } = type ) do
60+ defp reduce_types ( typemap , % ObjectType { } = type ) do
5361 if Map . has_key? ( typemap , type . name ) do
5462 typemap
5563 else
@@ -65,10 +73,10 @@ defmodule GraphQL.Schema do
6573 end
6674 end
6775
68- def reduce_types ( typemap , % { name: name } = type ) , do: Map . put ( typemap , name , type )
69- def reduce_types ( typemap , nil ) , do: typemap
76+ defp reduce_types ( typemap , % { name: name } = type ) , do: Map . put ( typemap , name , type )
77+ defp reduce_types ( typemap , nil ) , do: typemap
7078
71- def reduce_types ( typemap , type_module ) when is_atom ( type_module ) do
79+ defp reduce_types ( typemap , type_module ) when is_atom ( type_module ) do
7280 reduce_types ( typemap , apply ( type_module , :type , [ ] ) )
7381 end
7482
0 commit comments