Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion graphql/src/graphql_schema.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1557,11 +1557,16 @@ end
|> Io.Result.map_error ~f:(fun e -> `Argument_error e) >>=? fun fields ->
begin match fields with
| [field] ->
if field.name = "__typename" then
(Io.ok (`Response (data_to_json (`Assoc [ (field.name, `String subs.name) ], []))))
else
(match field_from_subscription_object subs field.name with
| Some subscription_field ->
(subscribe ctx subscription_field field : ((json, json) result Io.Stream.t, resolve_error) result Io.t :> ((json, json) result Io.Stream.t, [> execute_error]) result Io.t)
|> Io.Result.map ~f:(fun stream -> `Stream stream)
| None -> Io.ok (`Response (`Assoc [(alias_or_name field, `Null)])))
| None ->
let err = Printf.sprintf "Field '%s' is not defined on type '%s'" field.name subs.name in
Io.error (`Validation_error err))
(* see http://facebook.github.io/graphql/June2018/#sec-Response-root-field *)
| _ -> Io.error (`Validation_error "Subscriptions only allow exactly one selection for the operation.")
end
Expand Down
42 changes: 42 additions & 0 deletions graphql/test/introspection_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,46 @@ let suite = [
]
])
);
("__typename", `Quick, fun () ->
let schema = Graphql.Schema.(schema
~query_name:"MyQuery" []
~mutations:[]
~mutation_name:"MyMutation"
~subscriptions:[]
~subscription_name:"MySubscription"
) in
test_query schema
{|
query {
__typename
}
|}
(`Assoc [
"data", `Assoc [
"__typename", `String "MyQuery"
]
]);
test_query schema
{|
mutation {
__typename
}
|}
(`Assoc [
"data", `Assoc [
"__typename", `String "MyMutation"
]
]);
test_query schema
{|
subscription {
__typename
}
|}
(`Assoc [
"data", `Assoc [
"__typename", `String "MySubscription"
]
])
)
]
10 changes: 10 additions & 0 deletions graphql/test/schema_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,15 @@ let suite = [
]
]
])
);
("subscription field that doesn't exist", `Quick, fun () ->
let query = "subscription { dont_exist { id name } }" in
test_query query (`Assoc [
"errors", `List [
`Assoc [
"message", `String "Field 'dont_exist' is not defined on type 'subscription'"
]
]
])
)
]