Skip to content

Commit

Permalink
First stab at __type field
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Jul 24, 2019
1 parent 8992694 commit 9743915
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions graphql/src/graphql_schema.ml
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ module Introspection = struct
| DefaultArg a -> arg_types memo a.typ
in arg_list_types memo' args

let types_of_schema s =
let types, _ =
List.fold_left
(fun memo op ->
match op with
| None -> memo
| Some op -> types ~memo (Object op))
([], StringSet.empty)
[Some s.query; s.mutation; Option.map s.subscription ~f:obj_of_subscription_obj]
in
types

let rec args_to_list : type a b. ?memo:any_arg list -> (a, b) Arg.arg_list -> any_arg list = fun ?memo:(memo=[]) arglist ->
let open Arg in
match arglist with
Expand Down Expand Up @@ -1146,16 +1158,7 @@ module Introspection = struct
typ = NonNullable (List (NonNullable __type));
args = Arg.[];
lift = Io.ok;
resolve = fun _ s ->
let types, _ = List.fold_left
(fun memo op ->
match op with
| None -> memo
| Some op -> types ~memo (Object op))
([], StringSet.empty)
[Some s.query; s.mutation; Option.map s.subscription ~f:obj_of_subscription_obj]
in
types
resolve = fun _ s -> types_of_schema s
};
Field {
name = "queryType";
Expand Down Expand Up @@ -1197,7 +1200,7 @@ module Introspection = struct
]
}

let add_schema_field s =
let add_built_in_fields s =
let schema_field = Field {
name = "__schema";
doc = None;
Expand All @@ -1207,7 +1210,31 @@ module Introspection = struct
lift = Io.ok;
resolve = fun _ _ -> s
} in
let fields = lazy (schema_field::(Lazy.force s.query.fields)) in
let type_field = Field {
name = "__type";
doc = None;
deprecated = NotDeprecated;
typ = __type;
args = Arg.[arg "name" ~typ:(non_null string)];
lift = Io.ok;
resolve = fun _ _ name ->
let types = types_of_schema s in
List.find (fun typ ->
match typ with
| AnyTyp (Object o) -> o.name = name
| AnyTyp (Scalar s) -> s.name = name
| AnyTyp (Enum e) -> e.name = name
| AnyTyp (Abstract a) -> a.name = name
| AnyArgTyp (Arg.Object o) -> o.name = name
| AnyArgTyp (Arg.Scalar s) -> s.name = name
| AnyArgTyp (Arg.Enum e) -> e.name = name
| AnyTyp (List _) -> false
| AnyTyp (NonNullable _) -> false
| AnyArgTyp (List _) -> false
| AnyArgTyp (NonNullable _) -> false
) types
} in
let fields = lazy (schema_field::type_field::(Lazy.force s.query.fields)) in
{ s with query = { s.query with fields } }
end

Expand Down Expand Up @@ -1570,7 +1597,7 @@ end
let open Io.Infix in
let execute' schema ctx doc =
Io.return (collect_and_validate_fragments doc) >>=? fun fragments ->
let schema' = Introspection.add_schema_field schema in
let schema' = Introspection.add_built_in_fields schema in
Io.return (select_operation ?operation_name doc) >>=? fun op ->
let default_variables = List.fold_left (fun memo { Graphql_parser.name; default_value; _ } ->
match default_value with
Expand Down

0 comments on commit 9743915

Please sign in to comment.