Skip to content

Commit

Permalink
Fix __typename addition for InlineFragments
Browse files Browse the repository at this point in the history
We need operation registration's __typename adding to match how
Apollo Client does this in order for query hashes to match.

In order to achieve parity here, I've added the __typename field
to InlineFragments as well. While the __typename on an interface
may be redundant (see new tests for example), it's also a safer
change for me to recommend than anything within Apollo Client.
  • Loading branch information
trevor-scheer committed May 20, 2019
1 parent 7b22548 commit 484d57a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import gql from "graphql-tag";
import { withTypenameFieldAddedWhereNeeded } from "../graphql";
import { print } from "graphql";

describe("withTypenameFieldAddedWhereNeeded", () => {
it("properly adds __typename to each selectionSet", () => {
const query = gql`
query Product {
product {
sku
color {
id
value
}
}
}
`;

const withTypenames = withTypenameFieldAddedWhereNeeded(query);

expect(print(withTypenames)).toMatchInlineSnapshot(`
"query Product {
product {
__typename
sku
color {
__typename
id
value
}
}
}
"
`);
});

it("adds __typename to InlineFragment nodes (as ApolloClient does)", () => {
const query = gql`
query CartItems {
product {
items {
... on Table {
material
}
... on Paint {
color
}
}
}
}
`;

const withTypenames = withTypenameFieldAddedWhereNeeded(query);

expect(print(withTypenames)).toMatchInlineSnapshot(`
"query CartItems {
product {
__typename
items {
__typename
... on Table {
__typename
material
}
... on Paint {
__typename
color
}
}
}
}
"
`);
});
});
9 changes: 8 additions & 1 deletion packages/apollo-language-server/src/utilities/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,15 @@ export function withTypenameFieldAddedWhereNeeded(ast: ASTNode) {
}
},
leave(node: ASTNode) {
if (!(node.kind === "Field" || node.kind === "FragmentDefinition"))
if (
!(
node.kind === "Field" ||
node.kind === "FragmentDefinition" ||
node.kind === "InlineFragment"
)
) {
return undefined;
}
if (!node.selectionSet) return undefined;

if (true) {
Expand Down

0 comments on commit 484d57a

Please sign in to comment.