Skip to content

Commit

Permalink
fix: don't add top level __typename to subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
baransu committed Aug 10, 2019
1 parent 3697fe4 commit cf4eaf6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 38 deletions.
53 changes: 32 additions & 21 deletions src/base/graphql_printer.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ let rec print_input_value = iv =>
| Iv_boolean(b) => string_of_bool(b)
| Iv_enum(s) => s
| Iv_variable(v) => "$" ++ v
| Iv_list(l) =>
| Iv_list(list) =>
"["
++ (
List.map(({item, _}) => print_input_value(item), l)
list
|> List.map(({item, _}) => print_input_value(item))
|> String.concat(", ")
)
++ "]"
| Iv_object(o) =>
| Iv_object(obj) =>
"{"
++ (
List.map(
(({item: key, _}, {item: value, _})) =>
key ++ ": " ++ print_input_value(value),
o,
)
obj
|> List.map((({item: key, _}, {item: value, _})) =>
key ++ ": " ++ print_input_value(value)
)
|> String.concat(", ")
)
++ "}"
Expand Down Expand Up @@ -106,33 +106,44 @@ let rec print_type = ty =>
| Tr_non_null_named(n) => n.item ++ "!"
};

let rec print_selection_set = (schema, ty, ss) =>
switch (ss) {
let rec print_selection_set = (schema, ty, selection_set) =>
switch (selection_set) {
| [] => [||]
| l =>
| selection =>
let add_typename =
switch (ty) {
| Interface(_)
| Union(_) => true
| Object(_) => Ppx_config.apollo_mode()
| Object({om_name, _}) =>
let is_top_level_subscrption_type =
schema.meta.sm_subscription_type == Some(om_name);

!is_top_level_subscrption_type && Ppx_config.apollo_mode();
| _ => false
};

Array.concat([
[|
String("{\n"),
if (add_typename) {String("__typename\n")} else {Empty},
|],
l
let maybe_typename =
if (add_typename) {
String("__typename\n");
} else {
Empty;
};

let selection =
selection
|> List.map(s =>
Array.append(print_selection(schema, ty, s), [|String("\n")|])
)
|> Array.concat,
|> Array.concat;

Array.concat([
[|String("{\n"), maybe_typename|],
selection,
[|String("}\n")|],
]);
}
and print_selection = (schema, ty, s) =>
switch (s) {
and print_selection = (schema, ty, selection) =>
switch (selection) {
| Field({item, _}) => print_field(schema, ty, item)
| FragmentSpread({item, _}) => print_fragment_spread(item)
| InlineFragment({item, _}) => print_inline_fragment(schema, ty, item)
Expand Down
2 changes: 1 addition & 1 deletion src/bucklescript/graphql_ppx.re
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ let mapper = (_config, _cookies) => {
switch (Sys.getenv("GRAPHQL_PPX_APOLLO_MODE")) {
| "true" => true
| _ => false
| exception Not_found => true
| exception Not_found => false
},
root_directory: Sys.getcwd(),
schema_file:
Expand Down
51 changes: 37 additions & 14 deletions tests/__tests__/apolloMode.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module MyQuery = [%graphql
open Jest;
open Expect;

module BasciQuery = [%graphql
{|
{
first: nestedObject {
Expand All @@ -21,18 +24,38 @@ module MyQuery = [%graphql
|}
];

Jest.(
describe("Apollo mode", () => {
open Expect;
open! Expect.Operators;
module Subscription = [%graphql
{|
subscription {
simpleSubscription {
...on Dog {
name
}
...on Human {
name
}
}
}
|}
];

describe("Apollo mode", () => {
let typenameRegex = [%bs.re {|/__typename/g|}];

test("Adds __typename to objects", () => {
let typenameRegex = [%bs.re {|/__typename/g|}];
test("Adds __typename to objects", () =>
BasciQuery.query
|> Js.String.match(typenameRegex)
|> Belt.Option.map(_, Array.length)
|> expect
|> toEqual(Some(7))
);

MyQuery.query
|> Js.String.match(typenameRegex)
|> Belt.Option.map(_, Array.length)
|> expect == Some(7);
});
})
);
test("subscription don't have top level __typename", () =>
Subscription.query
|> Js.String.match(typenameRegex)
|> Belt.Option.map(_, Array.length)
|> expect
// 3 because on each union case and in simpleSubscription
|> toEqual(Some(3))
);
});
34 changes: 32 additions & 2 deletions tests/__tests__/apolloMode.rei
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module MyQuery: {
module BasciQuery: {
let query: string;
type t = {
.
"first": {. "inner": option({. "inner": option({. "field": string})})},
Expand All @@ -17,6 +18,7 @@ module MyQuery: {
},
};

let parse: Js.Json.t => t;
let make:
unit =>
{
Expand All @@ -25,6 +27,7 @@ module MyQuery: {
"query": string,
"variables": Js.Json.t,
};

let makeWithVariables:
Js.t({.}) =>
{
Expand All @@ -33,6 +36,33 @@ module MyQuery: {
"query": string,
"variables": Js.Json.t,
};

};
module Subscription: {
let query: string;
type t = {
.
"simpleSubscription": [
| `Dog({. "name": string})
| `Human({. "name": string})
],
};

let parse: Js.Json.t => t;
let make:
unit =>
{
.
"parse": Js.Json.t => t,
"query": string,
"variables": Js.Json.t,
};

let makeWithVariables:
Js.t({.}) =>
{
.
"parse": Js.Json.t => t,
"query": string,
"variables": Js.Json.t,
};
};

0 comments on commit cf4eaf6

Please sign in to comment.