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(documents): do not order mutations #6196

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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`
6 changes: 5 additions & 1 deletion packages/documents/src/sort-executable-document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { visit, type DocumentNode } from 'graphql';
import { OperationTypeNode, visit, type DocumentNode } from 'graphql';
import { sortExecutableNodes } from './sort-executable-nodes.js';

/**
Expand All @@ -13,6 +13,10 @@ export function sortExecutableDocument(document: DocumentNode): DocumentNode {
};
},
OperationDefinition(node) {
if (node.operation === ('mutation' as OperationTypeNode)) {
return false;
}

return {
...node,
variableDefinitions: sortExecutableNodes(node.variableDefinitions),
Expand Down
16 changes: 16 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,20 @@ 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 {
c {
f
e
d
}
b
a
}
`);
const outputStr = printExecutableGraphQLDocument(inputDocument);
expect(outputStr).toMatchInlineSnapshot(`"mutation A { c { f e d } b a }"`);
});
});
Loading