Skip to content

Commit 484d57a

Browse files
committed
Fix __typename addition for InlineFragments
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.
1 parent 7b22548 commit 484d57a

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import gql from "graphql-tag";
2+
import { withTypenameFieldAddedWhereNeeded } from "../graphql";
3+
import { print } from "graphql";
4+
5+
describe("withTypenameFieldAddedWhereNeeded", () => {
6+
it("properly adds __typename to each selectionSet", () => {
7+
const query = gql`
8+
query Product {
9+
product {
10+
sku
11+
color {
12+
id
13+
value
14+
}
15+
}
16+
}
17+
`;
18+
19+
const withTypenames = withTypenameFieldAddedWhereNeeded(query);
20+
21+
expect(print(withTypenames)).toMatchInlineSnapshot(`
22+
"query Product {
23+
product {
24+
__typename
25+
sku
26+
color {
27+
__typename
28+
id
29+
value
30+
}
31+
}
32+
}
33+
"
34+
`);
35+
});
36+
37+
it("adds __typename to InlineFragment nodes (as ApolloClient does)", () => {
38+
const query = gql`
39+
query CartItems {
40+
product {
41+
items {
42+
... on Table {
43+
material
44+
}
45+
... on Paint {
46+
color
47+
}
48+
}
49+
}
50+
}
51+
`;
52+
53+
const withTypenames = withTypenameFieldAddedWhereNeeded(query);
54+
55+
expect(print(withTypenames)).toMatchInlineSnapshot(`
56+
"query CartItems {
57+
product {
58+
__typename
59+
items {
60+
__typename
61+
... on Table {
62+
__typename
63+
material
64+
}
65+
... on Paint {
66+
__typename
67+
color
68+
}
69+
}
70+
}
71+
}
72+
"
73+
`);
74+
});
75+
});

packages/apollo-language-server/src/utilities/graphql.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,15 @@ export function withTypenameFieldAddedWhereNeeded(ast: ASTNode) {
136136
}
137137
},
138138
leave(node: ASTNode) {
139-
if (!(node.kind === "Field" || node.kind === "FragmentDefinition"))
139+
if (
140+
!(
141+
node.kind === "Field" ||
142+
node.kind === "FragmentDefinition" ||
143+
node.kind === "InlineFragment"
144+
)
145+
) {
140146
return undefined;
147+
}
141148
if (!node.selectionSet) return undefined;
142149

143150
if (true) {

0 commit comments

Comments
 (0)