Skip to content

Commit

Permalink
Merge pull request #65 from baransu/feature/apollo-mode
Browse files Browse the repository at this point in the history
Add -apollo-mode which adds __typename on every GraphQL Object in query
  • Loading branch information
mhallin authored Jan 17, 2019
2 parents b9afb51 + 918dd2b commit 0b1ce89
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/base/graphql_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions src/base/ppx_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/bucklescript/graphql_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions tests_bucklescript/__tests__/apolloMode.re
Original file line number Diff line number Diff line change
@@ -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);
});
}));
12 changes: 12 additions & 0 deletions tests_bucklescript/__tests__/apolloMode.rei
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion tests_bucklescript/bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"__tests__"
],
"ppx-flags": [
"../graphql_ppx.exe"
"../graphql_ppx.exe -apollo-mode"
],
"bs-dependencies": [
"@glennsl/bs-jest"
Expand Down

0 comments on commit 0b1ce89

Please sign in to comment.