Skip to content

Commit

Permalink
Merge pull request #1376 from dotansimha/feat/customize-graphql-tag-p…
Browse files Browse the repository at this point in the history
…luck

Allow to customize graphql-tag-pluck settings
  • Loading branch information
dotansimha committed Mar 1, 2019
2 parents a6c71b8 + d445812 commit 40c38b2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"graphql-config": "2.2.1",
"graphql-import": "0.7.1",
"graphql-tag-pluck": "0.6.0",
"graphql-toolkit": "0.1.1",
"graphql-toolkit": "0.1.2-cf77c5b.0",
"graphql-tools": "4.0.4",
"indent-string": "3.2.0",
"inquirer": "6.2.2",
Expand Down
13 changes: 12 additions & 1 deletion packages/graphql-codegen-cli/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const loadSchema = async (
options = schemaDef[pointToSchema];
}

if (config.pluckConfig) {
options.tagPluck = config.pluckConfig;
}

return loadSchemaToolkit(pointToSchema, options);
} catch (e) {
throw new DetailedError(
Expand Down Expand Up @@ -133,5 +137,12 @@ export const loadDocuments = async (
}
}

return loadDocumentsToolkit(documentDef as string);
return loadDocumentsToolkit(
documentDef as string,
config.pluckConfig
? {
tagPluck: config.pluckConfig
}
: {}
);
};
71 changes: 71 additions & 0 deletions packages/graphql-codegen-cli/tests/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,77 @@ describe('Codegen Executor', () => {
expect(result[0].content).toContain('MyQuery');
expect(result[0].filename).toEqual('out1.ts');
});

it('should handle graphql-tag and gatsby by default (documents)', async () => {
const result = await executeCodegen({
schema: ['./tests/test-documents/schema.graphql'],
documents: ['./tests/test-documents/gatsby-and-custom-parsers.ts'],
generates: {
'out1.ts': ['typescript-common', 'typescript-client']
}
});

expect(result[0].content).toContain('FragmentA'); // import gql from 'graphql-tag'
expect(result[0].content).toContain('FragmentB'); // import { graphql } from 'gatsby'
});

it('should handle custom graphql string parsers (documents)', async () => {
const result = await executeCodegen({
schema: ['./tests/test-documents/schema.graphql'],
documents: ['./tests/test-documents/gatsby-and-custom-parsers.ts'],
generates: {
'out1.ts': ['typescript-common', 'typescript-client']
},
pluckConfig: {
modules: [
{
name: 'custom-graphql-parser',
identifier: 'parser'
}
]
}
});

expect(result[0].content).toContain('FragmentC'); // import { parser } from 'custom-graphql-parser';
});

it('should handle graphql-tag and gatsby by default (schema)', async () => {
const result = await executeCodegen({
schema: './tests/test-files/schema-dir/gatsby-and-custom-parsers/*.ts',
generates: {
'out1.ts': ['typescript-common', 'typescript-server']
}
});

const content = result[0].content;

expect(content).toContain('Used graphql-tag'); // import gql from 'graphql-tag'
expect(content).toContain('Used gatsby'); // import { graphql } from 'gatsby'
expect(content).not.toContain('Used custom parser');
});

it('should handle custom graphql string parsers (schema)', async () => {
const result = await executeCodegen({
schema: './tests/test-files/schema-dir/gatsby-and-custom-parsers/*.ts',
generates: {
'out1.ts': ['typescript-common', 'typescript-server']
},
pluckConfig: {
modules: [
{
name: 'custom-graphql-parser',
identifier: 'parser'
}
]
}
});

const content = result[0].content;

expect(content).toContain('Used custom parser'); // import { parser } from 'custom-graphql-parser';
expect(content).not.toContain('Used graphql-tag');
expect(content).not.toContain('Used gatsby');
});
});

describe('Plugin Configuration', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import gql from 'graphql-tag';
import { graphql } from 'gatsby';
import { parser } from 'custom-graphql-parser';

export const fragmentA = gql`
fragment FragmentA on MyType {
fieldC
}
`;

export const fragmentB = graphql`
fragment FragmentB on MyType {
fieldC
}
`;

export const fragmentC = parser`
fragment FragmentC on MyType {
fieldC
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { parser } from 'custom-graphql-parser';

export const typeDefs = parser`
type Query {
book: Book
}
type Book {
"""
Used custom parser
"""
a: String
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { graphql } from 'gatsby';

export const typeDefs = graphql`
extend type User {
"""
Used gatsby
"""
b: String
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import gql from 'graphql-tag';

export const typeDefs = gql`
type Query {
user: User
}
type User {
"""
Used graphql-tag
"""
a: String
}
`;
2 changes: 1 addition & 1 deletion packages/graphql-codegen-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"change-case": "3.1.0",
"common-tags": "1.8.0",
"graphql-tag": "2.10.1",
"graphql-toolkit": "0.1.1",
"graphql-toolkit": "0.1.2-cf77c5b.0",
"graphql-tools": "4.0.4",
"ts-log": "2.1.4",
"winston": "3.2.1"
Expand Down
8 changes: 8 additions & 0 deletions packages/graphql-codegen-core/src/yml-config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export namespace Types {
watch?: boolean | string | string[];
silent?: boolean;
pluginLoader?: PluginLoaderFn;
pluckConfig?: {
modules?: Array<{
name: string;
identifier?: string;
}>;
magicComment?: string;
globalIdentifier?: string;
};
}
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5735,10 +5735,10 @@ graphql-tag@2.10.1:
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.10.1.tgz#10aa41f1cd8fae5373eaf11f1f67260a3cad5e02"
integrity sha512-jApXqWBzNXQ8jYa/HLkZJaVw9jgwNqZkywa2zfFn16Iv1Zb7ELNHkJaXHR7Quvd5SIGsy6Ny7SUKATgnu05uEg==

graphql-toolkit@0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/graphql-toolkit/-/graphql-toolkit-0.1.1.tgz#b1d50afeb9f11ba90f93e896ca8f48308ca7b549"
integrity sha512-MLTqeYi0k1KVEeVEPMLBoeAYWWOUc8OCu3DmfzHMHtahz+RatV9zBlRA0vghWAjzJP7/zH4xM9bJldF5RaV7GQ==
graphql-toolkit@0.1.2-cf77c5b.0:
version "0.1.2-cf77c5b.0"
resolved "https://registry.yarnpkg.com/graphql-toolkit/-/graphql-toolkit-0.1.2-cf77c5b.0.tgz#b2fa3cac21bb674a074c63a1f069ce775ab15851"
integrity sha512-MJAhtrL/rrUR8DZ6tya3U/hiVRHaINn74xJAJhv9YyLjcy5PXEgz5DGxR2AspA2KU0C2EWMctbOt+IDTWy4eXA==
dependencies:
aggregate-error "2.0.0"
deepmerge "3.2.0"
Expand Down

0 comments on commit 40c38b2

Please sign in to comment.