From 918dd2b369681ddd3ee6c692f2378437d91a3702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Cichoci=C5=84ski?= Date: Mon, 10 Dec 2018 22:33:41 +0100 Subject: [PATCH] Add -apollo-mode which adds __typename on every GraphQL Object in query --- src/base/graphql_printer.ml | 6 +++- src/base/ppx_config.ml | 4 +++ src/bucklescript/graphql_ppx.ml | 3 ++ tests_bucklescript/__tests__/apolloMode.re | 35 +++++++++++++++++++++ tests_bucklescript/__tests__/apolloMode.rei | 12 +++++++ tests_bucklescript/bsconfig.json | 2 +- 6 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests_bucklescript/__tests__/apolloMode.re create mode 100644 tests_bucklescript/__tests__/apolloMode.rei diff --git a/src/base/graphql_printer.ml b/src/base/graphql_printer.ml index 42416b43..0f2cfbc0 100644 --- a/src/base/graphql_printer.ml +++ b/src/base/graphql_printer.ml @@ -70,7 +70,11 @@ let rec print_type ty = match ty with let rec print_selection_set schema ty ss = match ss with | [] -> [||] | l -> - let add_typename = match ty with | Interface _ | Union _ -> true | _ -> false in + let add_typename = match ty with + | Interface _ | Union _ -> true + | Object _ -> Ppx_config.apollo_mode () + | _ -> false + in Array.concat [ [| String "{\n"; diff --git a/src/base/ppx_config.ml b/src/base/ppx_config.ml index bff3453e..ed0c1962 100644 --- a/src/base/ppx_config.ml +++ b/src/base/ppx_config.ml @@ -6,6 +6,7 @@ type config = { verbose_logging: bool; output_mode: output_mode; verbose_error_handling: bool; + apollo_mode: bool; root_directory: string; schema_file: string; raise_error_with_loc: 'a. Source_pos.ast_location -> string -> 'a @@ -21,6 +22,9 @@ let verbose_logging () = let output_mode () = (!config_ref |> Option.unsafe_unwrap).output_mode +let apollo_mode () = + (!config_ref |> Option.unsafe_unwrap).apollo_mode + let verbose_error_handling () = (!config_ref |> Option.unsafe_unwrap).verbose_error_handling diff --git a/src/bucklescript/graphql_ppx.ml b/src/bucklescript/graphql_ppx.ml index 61873a0f..0ce5bc83 100644 --- a/src/bucklescript/graphql_ppx.ml +++ b/src/bucklescript/graphql_ppx.ml @@ -114,6 +114,9 @@ let mapper argv = | _ -> true | exception Not_found -> true end); + apollo_mode = (match List.find ((=) "-apollo-mode") argv with + | _ -> true + | exception Not_found -> false); root_directory = Sys.getcwd (); schema_file = (match List.find (is_prefixed "-schema=") argv with | arg -> drop_prefix "-schema=" arg diff --git a/tests_bucklescript/__tests__/apolloMode.re b/tests_bucklescript/__tests__/apolloMode.re new file mode 100644 index 00000000..2d6a1699 --- /dev/null +++ b/tests_bucklescript/__tests__/apolloMode.re @@ -0,0 +1,35 @@ + +module MyQuery = [%graphql {| + { + first: nestedObject { + inner { + inner { + field + } + } + } + + second: nestedObject { + inner { + inner { + f1: field + f2: field + } + } + } + } +|}]; + +Jest.(describe("Apollo mode", () => { + open Expect; + open! Expect.Operators; + + test("Adds __typename to objects", () => { + let typenameRegex = [%bs.re {|/__typename/g|}]; + Js.log(MyQuery.query) + MyQuery.query + |> Js.String.match(typenameRegex) + |> Belt.Option.map(_, Array.length) + |> expect == Some(7); + }); +})); diff --git a/tests_bucklescript/__tests__/apolloMode.rei b/tests_bucklescript/__tests__/apolloMode.rei new file mode 100644 index 00000000..5d26a4fa --- /dev/null +++ b/tests_bucklescript/__tests__/apolloMode.rei @@ -0,0 +1,12 @@ +module MyQuery: { + type t = Js.t({ + . + first: Js.t({. inner: option(Js.t({. inner : option(Js.t({. field : string})) })) }), + second: Js.t({. inner : option(Js.t({. inner : option(Js.t({. f1: string, f2: string})) })) }), + }); + + let make: unit => Js.t({ . parse: Js.Json.t => t, query: string, variables: Js.Json.t }); + let makeWithVariables: Js.t({.}) => Js.t({ . parse: Js.Json.t => t, query: string, variables: Js.Json.t }); + + let query: string; +}; \ No newline at end of file diff --git a/tests_bucklescript/bsconfig.json b/tests_bucklescript/bsconfig.json index d6adab34..ef7e5c77 100644 --- a/tests_bucklescript/bsconfig.json +++ b/tests_bucklescript/bsconfig.json @@ -4,7 +4,7 @@ "__tests__" ], "ppx-flags": [ - "../graphql_ppx.exe" + "../graphql_ppx.exe -apollo-mode" ], "bs-dependencies": [ "@glennsl/bs-jest"