Skip to content

Commit

Permalink
Fix deduplicating fragments for DocumentMode.string visitor (#6668)
Browse files Browse the repository at this point in the history
  • Loading branch information
duarten authored Oct 11, 2021
1 parent 3789169 commit 3c2c847
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-impalas-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Fix dedupleFragments option for typescript-react-query (and possibly others)
2 changes: 1 addition & 1 deletion packages/plugins/c-sharp/c-sharp-operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class CSharpOperationsVisitor extends ClientSideBaseVisitor<

protected _gql(node: OperationDefinitionNode): string {
const fragments = this._transformFragments(node);
const doc = this._prepareDocument([print(node), this._includeFragments(fragments)].join('\n'));
const doc = this._prepareDocument([print(node), this._includeFragments(fragments, node.kind)].join('\n'));

return doc.replace(/"/g, '""');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class ClientSideBaseVisitor<
);
}

protected _includeFragments(fragments: string[]): string {
protected _includeFragments(fragments: string[], nodeKind: 'FragmentDefinition' | 'OperationDefinition'): string {
if (fragments && fragments.length > 0) {
if (this.config.documentMode === DocumentMode.documentNode) {
return this._fragments
Expand All @@ -281,6 +281,9 @@ export class ClientSideBaseVisitor<
} else if (this.config.documentMode === DocumentMode.documentNodeImportFragments) {
return '';
} else {
if (this.config.dedupeFragments && nodeKind !== 'OperationDefinition') {
return '';
}
return `${fragments.map(name => '${' + name + '}').join('\n')}`;
}
}
Expand All @@ -297,7 +300,7 @@ export class ClientSideBaseVisitor<

const doc = this._prepareDocument(`
${print(node).split('\\').join('\\\\') /* Re-escape escaped values in GraphQL syntax */}
${this._includeFragments(fragments)}`);
${this._includeFragments(fragments, node.kind)}`);

if (this.config.documentMode === DocumentMode.documentNode) {
let gqlObj = gqlTag([doc]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { validateTs } from '@graphql-codegen/testing';
import { parse, buildClientSchema, GraphQLSchema } from 'graphql';
import { parse, buildClientSchema, GraphQLSchema, buildSchema } from 'graphql';
import { plugin } from '../src';
import { Types, mergeOutputs } from '@graphql-codegen/plugin-helpers';
import { plugin as tsPlugin } from '../../typescript/src/index';
Expand All @@ -20,6 +20,57 @@ const validateTypeScript = async (
};

describe('React-Query', () => {
it('Duplicated nested fragments are removed', async () => {
const schema = buildSchema(/* GraphQL */ `
schema {
query: Query
}
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String!
}
`);

const ast = parse(/* GraphQL */ `
query foo {
user1: user(id: 1) {
...userWithEmail
}
user2: user(id: 2) {
...userWithName
}
}
fragment userBase on User {
id
}
fragment userWithEmail on User {
...userBase
email
}
fragment userWithName on User {
...userBase
name
}
`);

const res = (await plugin(
schema,
[{ location: '', document: ast }],
{ dedupeFragments: true },
{ outputFile: '' }
)) as Types.ComplexPluginOutput;

expect((res.content.match(/\{UserBaseFragmentDoc\}/g) || []).length).toBe(1);
});

const schema = buildClientSchema(require('../../../../../dev-test/githunt/schema.json'));
const basicDoc = parse(/* GraphQL */ `
query test {
Expand Down

0 comments on commit 3c2c847

Please sign in to comment.