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

Let addTypenameToDocument take any ASTNode (including DocumentNode, as before) #9595

Merged
merged 2 commits into from
Apr 11, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
- Fixed bug where the `useLazyQuery` execution function would always use the `refetch` method of `ObservableQuery`, instead of properly reapplying the current `fetchPolicy` using the `reobserve` method. <br/>
[@benjamn](https://github.com/benjamn) in [#9564](https://github.com/apollographql/apollo-client/pull/9564)

- Let `addTypenameToDocument` take any `ASTNode` (including `DocumentNode`, as before). <br/>
[@benjamn](https://github.com/benjamn) in [#9595](https://github.com/apollographql/apollo-client/pull/9595)

### Potentially disruptive changes

- Calling `fetchMore` for queries using the `cache-and-network` or `network-only` fetch policies should no longer trigger additional network requests when cache results are complete. <br/>
Expand Down
116 changes: 115 additions & 1 deletion src/utilities/graphql/__tests__/transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { print } from 'graphql';
import { DocumentNode, Kind, print } from 'graphql';
import gql from 'graphql-tag';
import { disableFragmentWarnings } from 'graphql-tag';

Expand Down Expand Up @@ -653,6 +653,120 @@ describe('query transforms', () => {
expect(print(expectedQuery)).toBe(print(newQueryDoc));
});

it('should be capable of modifying any ASTNode', () => {
const originalQuery = gql`
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
}
}
}

fragment friendFields on User {
firstName
lastName
}
`;

const fragmentSubtree: DocumentNode = {
...originalQuery,
definitions: originalQuery.definitions.map(def => {
if (def.kind === Kind.FRAGMENT_DEFINITION) {
return addTypenameToDocument(def);
}
return def;
}),
};

expect(print(fragmentSubtree)).toEqual(print(gql`
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
}
}
}

fragment friendFields on User {
firstName
lastName
__typename
}
`));

const userFieldSubtree: DocumentNode = {
...originalQuery,
definitions: originalQuery.definitions.map(def => {
if (def.kind === Kind.OPERATION_DEFINITION) {
return {
...def,
selectionSet: {
...def.selectionSet,
selections: def.selectionSet.selections.map(selection => {
if (
selection.kind === Kind.FIELD &&
selection.name.value === "user"
) {
return addTypenameToDocument(selection);
}
return selection;
}),
},
};
}
return def;
}),
};

expect(print(userFieldSubtree)).toEqual(print(gql`
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
__typename
}
__typename
}
}

fragment friendFields on User {
firstName
lastName
}
`));

const wholeQueryFromJustTheFragment =
addTypenameToDocument(userFieldSubtree);

const wholeQueryFromUserFieldSubtree =
addTypenameToDocument(fragmentSubtree);

expect(
print(wholeQueryFromUserFieldSubtree)
).toEqual(
print(wholeQueryFromJustTheFragment)
);

expect(print(wholeQueryFromUserFieldSubtree)).toEqual(print(gql`
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
__typename
}
__typename
}
}

fragment friendFields on User {
firstName
lastName
__typename
}
`));
});

it('should be able to apply a QueryTransformer correctly', () => {
const testQuery = gql`
query {
Expand Down
11 changes: 7 additions & 4 deletions src/utilities/graphql/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
VariableDefinitionNode,
VariableNode,
visit,
ASTNode,
} from 'graphql';

// TODO(brian): A hack until this issue is resolved (https://github.com/graphql/graphql-js/issues/3356)
Expand Down Expand Up @@ -213,10 +214,12 @@ export function removeDirectivesFromDocument(
return modifiedDoc;
}

export const addTypenameToDocument = Object.assign(function (
doc: DocumentNode
): DocumentNode {
return visit(checkDocument(doc), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth checking that doc.kind is a string, to protect against passing in strings by accident? Or is it unlikely that anyone is going to be passing a string into this function from JS.

export const addTypenameToDocument = Object.assign(function <
TNode extends ASTNode
>(
doc: TNode
): TNode {
return visit(doc, {
SelectionSet: {
enter(node, _key, parent) {
// Don't add __typename to OperationDefinitions.
Expand Down