Skip to content

Commit

Permalink
fix(documents): do not order mutations (#6196)
Browse files Browse the repository at this point in the history
* fix(documents): do not order mutations

* Go

---------

Co-authored-by: Arda TANRIKULU <[email protected]>
  • Loading branch information
martyganz and ardatan authored May 27, 2024
1 parent 7368829 commit 26d29d0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-flowers-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/documents': patch
---

Do not sort mutations when using `printExecutableGraphQLDocument`
28 changes: 25 additions & 3 deletions packages/documents/src/sort-executable-document.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { visit, type DocumentNode } from 'graphql';
import { SelectionNode, SelectionSetNode, visit, type DocumentNode } from 'graphql';
import { sortExecutableNodes } from './sort-executable-nodes.js';

/**
* Sort an executable GraphQL document.
*/
export function sortExecutableDocument(document: DocumentNode): DocumentNode {
const ignoredNodes = new WeakSet<SelectionSetNode>();
const ignoredSelectionsArr = new Set<readonly SelectionNode[]>();
const ignoredFragments = new Set<string>();
return visit(document, {
Document(node) {
return {
Expand All @@ -13,30 +16,49 @@ export function sortExecutableDocument(document: DocumentNode): DocumentNode {
};
},
OperationDefinition(node) {
if (node.operation === 'mutation') {
ignoredNodes.add(node.selectionSet);
ignoredSelectionsArr.add(node.selectionSet.selections);
}
return {
...node,
variableDefinitions: sortExecutableNodes(node.variableDefinitions),
};
},
SelectionSet(node) {
if (ignoredNodes.has(node)) {
ignoredSelectionsArr.add(node.selections);
return node;
}
return {
...node,
selections: sortExecutableNodes(node.selections),
};
},
FragmentSpread(node) {
FragmentSpread(node, _key, parent) {
if (Array.isArray(parent) && ignoredSelectionsArr.has(parent)) {
ignoredFragments.add(node.name.value);
}
return {
...node,
directives: sortExecutableNodes(node.directives),
};
},
InlineFragment(node) {
InlineFragment(node, _key, parent) {
if (Array.isArray(parent) && ignoredSelectionsArr.has(parent)) {
ignoredNodes.add(node.selectionSet);
ignoredSelectionsArr.add(node.selectionSet.selections);
return node;
}
return {
...node,
directives: sortExecutableNodes(node.directives),
};
},
FragmentDefinition(node) {
if (ignoredFragments.has(node.name.value)) {
return node;
}
return {
...node,
directives: sortExecutableNodes(node.directives),
Expand Down
26 changes: 26 additions & 0 deletions packages/documents/tests/print-executable-graphql-document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,30 @@ describe('printExecutableGraphQLDocument', () => {
`"fragment B on Query { c } query A { ... on Query { a { a ...B } } ... on Query { a { b ...B } } }"`,
);
});

test('should not sort a mutation as mutations run in series and order matters', () => {
const inputDocument = parse(/* GraphQL */ `
mutation A {
... on Mutation {
c
b
d
}
c {
d
e {
b
a
}
f
}
b
a
}
`);
const outputStr = printExecutableGraphQLDocument(inputDocument);
expect(outputStr).toMatchInlineSnapshot(
`"mutation A { ... on Mutation { c b d } c { d e { a b } f } b a }"`,
);
});
});

0 comments on commit 26d29d0

Please sign in to comment.