Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix __typename addition for InlineFragments #1286

Merged
merged 4 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- Fix \_\_typename addition for InlineFragments [#1286](https://github.com/apollographql/apollo-tooling/pull/1286)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
3 changes: 2 additions & 1 deletion apollo.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = {
client: {
name: "Apollo CLI",
service: "engine@master",
includes: ["./packages/apollo-language-server/**/*.ts"]
includes: ["./packages/apollo-language-server/**/*.ts"],
excludes: ["**/*.test.ts", "**/__tests__/*"]
},
engine: {
frontend: "https://engine-staging.apollographql.com",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import gql from "graphql-tag";
import { print } from "graphql";
import { withTypenameFieldAddedWhereNeeded } 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 === Kind.FIELD ||
node.kind === Kind.FRAGMENT_DEFINITION ||
node.kind === Kind.INLINE_FRAGMENT
)
) {
return undefined;
}
if (!node.selectionSet) return undefined;

if (true) {
Expand Down