From 70a528d81635d20dd83314c68031ba45b15090f0 Mon Sep 17 00:00:00 2001 From: Dotan Simha Date: Thu, 4 Jul 2019 17:14:38 +0300 Subject: [PATCH] try to fix fragments with `noGraphQlTag` --- .../src/client-side-base-visitor.ts | 24 +++++++++-- .../react-apollo/tests/react-apollo.spec.ts | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts index 5672517100e..98a2d98cf0e 100644 --- a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts @@ -91,10 +91,26 @@ export class ClientSideBaseVisitor 0) { - return `${fragments - .filter((name, i, all) => all.indexOf(name) === i) - .map(name => '${' + name + '}') - .join('\n')}`; + if (this.config.noGraphQLTag) { + return `${fragments + .filter((name, i, all) => all.indexOf(name) === i) + .map(name => { + const found = this._fragments.find(f => `${f.name}FragmentDoc` === name); + + if (found) { + return print(found.node); + } + + return null; + }) + .filter(a => a) + .join('\n')}`; + } else { + return `${fragments + .filter((name, i, all) => all.indexOf(name) === i) + .map(name => '${' + name + '}') + .join('\n')}`; + } } return ''; diff --git a/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts b/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts index 7027a9c9606..546480fc73e 100644 --- a/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts +++ b/packages/plugins/typescript/react-apollo/tests/react-apollo.spec.ts @@ -32,6 +32,47 @@ describe('React Apollo', () => { validateTs(merged, undefined, true); }; + describe('Issues', () => { + it('Issue #2080 - noGraphQLTag does not work with fragments correctly', async () => { + const docs = [ + { + filePath: '', + content: parse(/* GraphQL */ ` + query test { + feed { + id + commentCount + repository { + ...RepositoryFields + } + } + } + + fragment RepositoryFields on Repository { + full_name + html_url + owner { + avatar_url + } + } + `), + }, + ]; + const content = (await plugin( + schema, + docs, + { + noGraphQLTag: true, + }, + { + outputFile: 'graphql.tsx', + } + )) as Types.ComplexPluginOutput; + // make sure the fragment is there twice. + expect(content.content.split('{"kind":"FragmentDefinition","name":{"kind":"Name","value":"RepositoryFields"}').length).toBe(3); + }); + }); + describe('Imports', () => { it('should import React and ReactApollo dependencies', async () => { const docs = [{ filePath: '', content: basicDoc }];