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

Support tagName flag #793

Merged
merged 2 commits into from
Dec 6, 2018
Merged
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
20 changes: 19 additions & 1 deletion packages/apollo/src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export interface Flags {
skipSSLValidation?: boolean;
}

export interface ClientCommandFlags extends Flags {
includes?: string;
queries?: string;
excludes?: string;
tagName?: string;
clientName?: string;
clientReferenceId?: string;
clientVersion?: string;
}

const headersArrayToObject = (
arr?: string[]
): Record<string, string> | undefined => {
Expand Down Expand Up @@ -249,13 +259,17 @@ export abstract class ClientCommand extends ProjectCommand {
excludes: flags.string({
description:
"Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently work in watch mode"
}),
tagName: flags.string({
description:
"Name of the template literal tag used to identify template literals containing GraphQL queries in Javascript/Typescript code"
})
};
public project!: GraphQLClientProject;
constructor(argv, config) {
super(argv, config);
this.type = "client";
this.configMap = (flags: any) => {
this.configMap = (flags: ClientCommandFlags) => {
const config = {
client: {
name: flags.clientName,
Expand All @@ -278,6 +292,10 @@ export abstract class ClientCommand extends ProjectCommand {
config.client.excludes = [flags.excludes];
}

if (flags.tagName) {
config.client.tagName = flags.tagName;
}

return config;
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`client:codegen extracts queries with a custom tagName provided as a flag 1`] = `
"/* tslint:disable */
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL query operation: SimpleQuery
// ====================================================

export interface SimpleQuery {
hello: string;
}
"
`;

exports[`client:codegen extracts queries with a custom tagName provided in the config 1`] = `
"/* tslint:disable */
// This file was automatically generated and should not be edited.

// ====================================================
// GraphQL query operation: SimpleQuery
// ====================================================

export interface SimpleQuery {
hello: string;
}
"
`;

exports[`client:codegen generates operation IDs for swift files when flag is set 1`] = `
"{
\\"4de1e386589b0853a102a87a3f21ca25266116517e59c1e8be9442e2571f00bc\\": {
Expand Down
58 changes: 58 additions & 0 deletions packages/apollo/src/commands/client/__tests__/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,64 @@ describe("client:codegen", () => {
).toMatchSnapshot();
}
);

test
.fs({
"schema.json": fullSchemaJsonString,
"components/component.tsx": `
const query = customGraphQLTag\`
query SimpleQuery {
hello
}
\`;
`,
"apollo.config.js": `
module.exports = {
client: {
includes: ["./**/*.tsx"],
service: { name: "my-service-name", localSchemaFile: "./schema.json" }
}
}
`
})
.command([
"client:codegen",
"--target=typescript",
"--tagName=customGraphQLTag",
"--outputFlat"
])
.it("extracts queries with a custom tagName provided as a flag", () => {
expect(
fs.readFileSync("__generated__/SimpleQuery.ts").toString()
).toMatchSnapshot();
});

test
.fs({
"schema.json": fullSchemaJsonString,
"components/component.tsx": `
const query = customGraphQLTag\`
query SimpleQuery {
hello
}
\`;
`,
"apollo.config.js": `
module.exports = {
client: {
includes: ["./**/*.tsx"],
service: { name: "my-service-name", localSchemaFile: "./schema.json" },
tagName: 'customGraphQLTag'
}
}
`
})
.command(["client:codegen", "--target=typescript", "--outputFlat"])
.it("extracts queries with a custom tagName provided in the config", () => {
expect(
fs.readFileSync("__generated__/SimpleQuery.ts").toString()
).toMatchSnapshot();
});
});

describe("error handling", () => {
Expand Down
5 changes: 0 additions & 5 deletions packages/apollo/src/commands/client/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ export default class Generate extends ClientCommand {
mergeInFieldsFromFragmentSpreads: flags.boolean({
description: "Merge fragment fields onto its enclosing type"
}),
tagName: flags.string({
description:
"Name of the template literal tag used to identify template literals containing GraphQL queries in Javascript/Typescript code",
default: "gql"
}),

// swift
namespace: flags.string({
Expand Down