From 236cf65d6e14a7d0698d3a0ad188d0bf7d7488cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Nuno=20Monteiro?= Date: Sun, 5 Aug 2018 13:04:52 +0100 Subject: [PATCH] Pass operation name in graphql_lwt This is useful in the GraphiQL playground when writing multiple queries --- graphql-lwt/src/graphql_lwt.ml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/graphql-lwt/src/graphql_lwt.ml b/graphql-lwt/src/graphql_lwt.ml index 7658aa3..0d5c3be 100644 --- a/graphql-lwt/src/graphql_lwt.ml +++ b/graphql-lwt/src/graphql_lwt.ml @@ -15,10 +15,10 @@ module Server = struct | Ok _ as ok -> ok | Error err -> Error (`String err) - let execute_query ctx schema variables query = + let execute_query ctx schema variables operation_name query = let open Lwt_result in Lwt.return @@ json_err @@ Graphql_parser.parse query >>= fun doc -> - Schema.execute schema ctx ~variables doc + Schema.execute schema ctx ~variables ?operation_name doc let execute_request ctx schema req body = Cohttp_lwt.Body.to_string body >>= fun body' -> @@ -26,8 +26,12 @@ module Server = struct let json = Yojson.Basic.from_string body' in let query = Yojson.Basic.(json |> Util.member "query" |> Util.to_string) in let variables = try Yojson.Basic.Util.(json |> member "variables" |> to_assoc) with _ -> [] in + let operation_name = + try Some Yojson.Basic.Util.(json |> member "operationName" |> to_string) + with _ -> None + in Lwt_io.printf "Query: %s\n" query; - let result = execute_query ctx schema (variables :> (string * Graphql_parser.const_value) list) query in + let result = execute_query ctx schema (variables :> (string * Graphql_parser.const_value) list) operation_name query in result >>= function | Ok data -> let body = Yojson.Basic.to_string data in