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

Preserve directives on fragments when merging AST #3290

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/tidy-comics-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/toolkit': minor
---

Preserve directives on fragment during merge
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ describe('MergeAst', () => {
expect(removeParametersCommas(parseMergeAndPrint(query))).toBe(mergedQuery);
expect(parseMergeAndPrint(query, sorareSchema)).toBe(mergedQueryWithSchema);
});

it('preserve directives on fragments', () => {
const query = /* GraphQL */ `
query Test($param: Boolean!) {
...Fragment1 @include(if: $param)
}

fragment Fragment1 on Test {
id
}
`;
const mergedQuery = stripWhitespace(/* GraphQL */ `
query Test($param: Boolean!) {
... on Test @include(if: $param) {
id
}
}
`);
const mergedQueryWithSchema = stripWhitespace(/* GraphQL */ `
query Test($param: Boolean!) {
... on Test @include(if: $param) {
id
}
}
`);
expect(parseMergeAndPrint(query)).toBe(mergedQuery);
expect(parseMergeAndPrint(query, schema)).toBe(mergedQueryWithSchema);
});
});

function parseMergeAndPrint(query: string, maybeSchema?: GraphQLSchema) {
Expand Down
4 changes: 2 additions & 2 deletions packages/graphiql-toolkit/src/graphql-helpers/merge-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ function inlineRelevantFragmentSpreads(
}
const fragmentDefinition = fragmentDefinitions[selection.name.value];
if (fragmentDefinition) {
const { typeCondition, directives, selectionSet } = fragmentDefinition;
const { typeCondition, selectionSet } = fragmentDefinition;
selection = {
kind: Kind.INLINE_FRAGMENT,
typeCondition,
directives,
directives: selection.directives,
selectionSet,
};
}
Expand Down
Loading