From ab91ced39d7e638560b05351705ee7c7a9d604bb Mon Sep 17 00:00:00 2001 From: Ozan Sener Date: Tue, 24 Jul 2018 22:17:48 +0200 Subject: [PATCH 1/2] Allow introspection of default scalar args --- graphql/src/graphql_intf.ml | 1 + graphql/src/graphql_schema.ml | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/graphql/src/graphql_intf.ml b/graphql/src/graphql_intf.ml index 7927c1e..7992f6e 100644 --- a/graphql/src/graphql_intf.ml +++ b/graphql/src/graphql_intf.ml @@ -56,6 +56,7 @@ module type Schema = sig val scalar : ?doc:string -> string -> + default_doc:('a -> Yojson.Basic.json) -> coerce:(Graphql_parser.const_value -> ('a, string) result) -> 'a option arg_typ diff --git a/graphql/src/graphql_schema.ml b/graphql/src/graphql_schema.ml index 7337af6..087a7e1 100644 --- a/graphql/src/graphql_schema.ml +++ b/graphql/src/graphql_schema.ml @@ -108,6 +108,7 @@ module Make(Io : IO) = struct | Scalar : { name : string; doc : string option; + default_doc: 'a -> Yojson.Basic.json; coerce : Graphql_parser.const_value -> ('a, string) result; } -> 'a option arg_typ | Object : { @@ -145,8 +146,8 @@ module Make(Io : IO) = struct let arg' ?doc name ~typ ~default = DefaultArg { name; doc; typ; default } - let scalar ?doc name ~coerce = - Scalar { name; doc; coerce } + let scalar ?doc name ~default_doc ~coerce = + Scalar { name; doc; default_doc; coerce } let enum ?doc name ~values = Enum { name; doc; values } @@ -158,6 +159,7 @@ module Make(Io : IO) = struct let int = Scalar { name = "Int"; doc = None; + default_doc = (fun n -> `Int n); coerce = function | `Int n -> Ok n | _ -> Error "Invalid int" @@ -166,6 +168,7 @@ module Make(Io : IO) = struct let string = Scalar { name = "String"; doc = None; + default_doc = (fun s -> `String s); coerce = function | `String s -> Ok s | _ -> Error "Invalid string" @@ -174,6 +177,7 @@ module Make(Io : IO) = struct let float = Scalar { name = "Float"; doc = None; + default_doc = (fun f -> `Float f); coerce = function | `Float f -> Ok f | `Int n -> Ok (float_of_int n) @@ -183,6 +187,7 @@ module Make(Io : IO) = struct let bool = Scalar { name = "Boolean"; doc = None; + default_doc = (fun b -> `Bool b); coerce = function | `Bool b -> Ok b | _ -> Error "Invalid boolean" @@ -191,6 +196,7 @@ module Make(Io : IO) = struct let guid = Scalar { name = "ID"; doc = None; + default_doc = (fun s -> `String s); coerce = function | `String s -> Ok s | `Int n -> Ok (string_of_int n) @@ -656,7 +662,10 @@ module Introspection = struct typ = string; args = Arg.[]; lift = Io.ok; - resolve = fun _ (AnyArg v) -> None + resolve = fun _ (AnyArg arg) -> match arg with + | Arg.DefaultArg { typ = Arg.Scalar { default_doc; _ }; default; _ } -> + Some (Yojson.Basic.to_string (default_doc default)) + | _ -> None } ] } From a6e4a90083a9e348902d78f0ca0c37df0d945cd7 Mon Sep 17 00:00:00 2001 From: Ozan Sener Date: Wed, 25 Jul 2018 07:49:10 +0200 Subject: [PATCH 2/2] Make default_doc for scalar args optional --- graphql/src/graphql_intf.ml | 2 +- graphql/src/graphql_schema.ml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/graphql/src/graphql_intf.ml b/graphql/src/graphql_intf.ml index 7992f6e..a74cb64 100644 --- a/graphql/src/graphql_intf.ml +++ b/graphql/src/graphql_intf.ml @@ -56,7 +56,7 @@ module type Schema = sig val scalar : ?doc:string -> string -> - default_doc:('a -> Yojson.Basic.json) -> + ?default_doc:('a -> Yojson.Basic.json) -> coerce:(Graphql_parser.const_value -> ('a, string) result) -> 'a option arg_typ diff --git a/graphql/src/graphql_schema.ml b/graphql/src/graphql_schema.ml index 087a7e1..d035c23 100644 --- a/graphql/src/graphql_schema.ml +++ b/graphql/src/graphql_schema.ml @@ -146,7 +146,7 @@ module Make(Io : IO) = struct let arg' ?doc name ~typ ~default = DefaultArg { name; doc; typ; default } - let scalar ?doc name ~default_doc ~coerce = + let scalar ?doc name ?(default_doc=(fun _ -> `Null)) ~coerce = Scalar { name; doc; default_doc; coerce } let enum ?doc name ~values =