From 9914a0e18d77178d20e7f2af7d68bcc52583fb98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20B=C3=BCrger?= Date: Tue, 31 Jul 2018 09:07:47 +0200 Subject: [PATCH 1/4] [TS] Dedup enums and inputs by using global types file --- .../__snapshots__/generate.test.ts.snap | 18 +- .../codegen/__tests__/generate.test.ts | 9 + packages/apollo-cli/src/generate.ts | 55 +- .../__snapshots__/codeGeneration.ts.snap | 971 ++++++++++++++++++ .../src/__tests__/codeGeneration.ts | 296 +++++- .../src/codeGeneration.ts | 92 ++ .../apollo-codegen-typescript/src/index.ts | 2 +- .../apollo-codegen-typescript/src/language.ts | 13 +- 8 files changed, 1444 insertions(+), 12 deletions(-) diff --git a/packages/apollo-cli/src/commands/codegen/__tests__/__snapshots__/generate.test.ts.snap b/packages/apollo-cli/src/commands/codegen/__tests__/__snapshots__/generate.test.ts.snap index d310e0f886..82ebd89097 100644 --- a/packages/apollo-cli/src/commands/codegen/__tests__/__snapshots__/generate.test.ts.snap +++ b/packages/apollo-cli/src/commands/codegen/__tests__/__snapshots__/generate.test.ts.snap @@ -592,7 +592,11 @@ exports[`successful codegen writes TypeScript types into a __generated__ directo export interface SimpleQuery { hello: string; -} +}" +`; + +exports[`successful codegen writes TypeScript types into a __generated__ directory next to sources when no output is set 2`] = ` +" /* tslint:disable */ // This file was automatically generated and should not be edited. @@ -618,7 +622,11 @@ exports[`successful codegen writes TypeScript types next to sources when output export interface SimpleQuery { hello: string; -} +}" +`; + +exports[`successful codegen writes TypeScript types next to sources when output is set to empty string 2`] = ` +" /* tslint:disable */ // This file was automatically generated and should not be edited. @@ -644,7 +652,11 @@ exports[`successful codegen writes TypeScript types to a custom directory next t export interface SimpleQuery { hello: string; -} +}" +`; + +exports[`successful codegen writes TypeScript types to a custom directory next to sources when output is set 2`] = ` +" /* tslint:disable */ // This file was automatically generated and should not be edited. diff --git a/packages/apollo-cli/src/commands/codegen/__tests__/generate.test.ts b/packages/apollo-cli/src/commands/codegen/__tests__/generate.test.ts index 5861f3d2a5..f8a8b435eb 100644 --- a/packages/apollo-cli/src/commands/codegen/__tests__/generate.test.ts +++ b/packages/apollo-cli/src/commands/codegen/__tests__/generate.test.ts @@ -325,6 +325,9 @@ describe("successful codegen", () => { expect( mockFS.readFileSync("directory/__generated__/SimpleQuery.ts").toString() ).toMatchSnapshot(); + expect( + mockFS.readFileSync("__generated__/globalTypes.ts").toString() + ).toMatchSnapshot(); }); test @@ -380,6 +383,9 @@ describe("successful codegen", () => { .readFileSync("directory/__foo__/SimpleQuery.ts") .toString() ).toMatchSnapshot(); + expect( + mockFS.readFileSync("__foo__/globalTypes.ts").toString() + ).toMatchSnapshot(); } ); @@ -442,6 +448,9 @@ describe("successful codegen", () => { .readFileSync("directory/SimpleQuery.ts") .toString() ).toMatchSnapshot(); + expect( + mockFS.readFileSync("globalTypes.ts").toString() + ).toMatchSnapshot(); } ); diff --git a/packages/apollo-cli/src/generate.ts b/packages/apollo-cli/src/generate.ts index 999611611b..2236733131 100644 --- a/packages/apollo-cli/src/generate.ts +++ b/packages/apollo-cli/src/generate.ts @@ -12,7 +12,7 @@ import { generateSource as generateSwiftSource } from "apollo-codegen-swift"; import { generateSource as generateTypescriptLegacySource } from "apollo-codegen-typescript-legacy"; import { generateSource as generateFlowLegacySource } from "apollo-codegen-flow-legacy"; import { generateSource as generateFlowSource } from "apollo-codegen-flow"; -import { generateSource as generateTypescriptSource } from "apollo-codegen-typescript"; +import { generateLocalSource as generateTypescriptLocalSource, generateGlobalSource as generateTypescriptGlobalSource } from "apollo-codegen-typescript"; import { generateSource as generateScalaSource } from "apollo-codegen-scala"; import { GraphQLSchema } from "graphql"; import { FlowCompilerOptions } from '../../apollo-codegen-flow/lib/language'; @@ -71,12 +71,9 @@ export default function generate( writeOperationIdsMap(context); writtenFiles += 1; } - } else if (target === "flow" || target === "typescript" || target === "ts") { + } else if (target === "flow") { const context = compileToIR(schema, document, options); - const { generatedFiles, common } = - target === "flow" - ? generateFlowSource(context) - : generateTypescriptSource(context); + const { generatedFiles, common } = generateFlowSource(context); const outFiles: { [fileName: string]: BasicGeneratedFile; @@ -118,6 +115,52 @@ export default function generate( generatedFiles.map(o => o.content.fileContents).join("\n") + common ); + writtenFiles += 1; + } + } else if (target === "typescript" || target === "ts") { + const context = compileToIR(schema, document, options); + const generatedFiles = generateTypescriptLocalSource(context); + const generatedGlobalFile = generateTypescriptGlobalSource(context); + + const outFiles: { + [fileName: string]: BasicGeneratedFile; + } = {}; + + if (nextToSources || (fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())) { + if (nextToSources && !fs.existsSync(outputPath)) { + fs.mkdirSync(outputPath); + } + + const globalSourcePath = path.join(outputPath, "globalTypes.ts"); + outFiles[globalSourcePath] = { + output: generatedGlobalFile.fileContents, + }; + + generatedFiles.forEach(({ sourcePath, fileName, content }) => { + let dir = outputPath; + if (nextToSources) { + dir = path.join(path.dirname(sourcePath), dir); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + } + + const outFilePath = path.join(dir, fileName); + outFiles[outFilePath] = { + output: content({ outputPath: outFilePath, globalSourcePath }).fileContents, + }; + }); + + writeGeneratedFiles(outFiles, path.resolve(".")); + + writtenFiles += Object.keys(outFiles).length; + } else { + fs.writeFileSync( + outputPath, + generatedFiles.map(o => o.content().fileContents).join("\n") + + generatedGlobalFile.fileContents, + ); + writtenFiles += 1; } } else { diff --git a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap index a9db06434b..b23fcb6378 100644 --- a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap +++ b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap @@ -646,6 +646,977 @@ export interface HeroNameVariables { } `; +exports[`Typescript codeGeneration local / global fragment spreads with inline fragments 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: HeroName +// ==================================================== + +export interface HeroName_hero_Human_friends_Human { + __typename: \\"Human\\"; + /** + * What this human calls themselves + */ + name: string; +} + +export interface HeroName_hero_Human_friends_Droid { + __typename: \\"Droid\\"; + /** + * The ID of the droid + */ + id: string; +} + +export type HeroName_hero_Human_friends = HeroName_hero_Human_friends_Human | HeroName_hero_Human_friends_Droid; + +export interface HeroName_hero_Human { + __typename: \\"Human\\"; + /** + * What this human calls themselves + */ + name: string; + /** + * The ID of the human + */ + id: string; + /** + * The home planet of the human, or null if unknown + */ + homePlanet: string | null; + /** + * This human's friends, or an empty list if they have none + */ + friends: (HeroName_hero_Human_friends | null)[] | null; +} + +export interface HeroName_hero_Droid { + __typename: \\"Droid\\"; + /** + * What others call this droid + */ + name: string; + /** + * The ID of the droid + */ + id: string; + /** + * The movies this droid appears in + */ + appearsIn: (Episode | null)[]; +} + +export type HeroName_hero = HeroName_hero_Human | HeroName_hero_Droid; + +export interface HeroName { + hero: HeroName_hero | null; +} + +export interface HeroNameVariables { + episode?: Episode | null; +}", + }, + "fileName": "HeroName.ts", + "sourcePath": "GraphQL request", + }, + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL fragment: humanFragment +// ==================================================== + +export interface humanFragment_friends_Human { + __typename: \\"Human\\"; + /** + * What this human calls themselves + */ + name: string; +} + +export interface humanFragment_friends_Droid { + __typename: \\"Droid\\"; + /** + * The ID of the droid + */ + id: string; +} + +export type humanFragment_friends = humanFragment_friends_Human | humanFragment_friends_Droid; + +export interface humanFragment { + __typename: \\"Human\\"; + /** + * The home planet of the human, or null if unknown + */ + homePlanet: string | null; + /** + * This human's friends, or an empty list if they have none + */ + friends: (humanFragment_friends | null)[] | null; +}", + }, + "fileName": "humanFragment.ts", + "sourcePath": "GraphQL request", + }, + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL fragment: droidFragment +// ==================================================== + +export interface droidFragment { + __typename: \\"Droid\\"; + /** + * The movies this droid appears in + */ + appearsIn: (Episode | null)[]; +}", + }, + "fileName": "droidFragment.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global fragment spreads with inline fragments 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global fragment with fragment spreads 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL fragment: simpleFragment +// ==================================================== + +export interface simpleFragment { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; +}", + }, + "fileName": "simpleFragment.ts", + "sourcePath": "GraphQL request", + }, + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL fragment: anotherFragment +// ==================================================== + +export interface anotherFragment { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The ID of the character + */ + id: string; + /** + * The name of the character + */ + name: string; +}", + }, + "fileName": "anotherFragment.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global fragment with fragment spreads 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global fragment with fragment spreads with inline fragment 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL fragment: simpleFragment +// ==================================================== + +export interface simpleFragment { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; +}", + }, + "fileName": "simpleFragment.ts", + "sourcePath": "GraphQL request", + }, + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL fragment: anotherFragment +// ==================================================== + +export interface anotherFragment_Droid { + __typename: \\"Droid\\"; + /** + * The ID of the droid + */ + id: string; + /** + * What others call this droid + */ + name: string; +} + +export interface anotherFragment_Human { + __typename: \\"Human\\"; + /** + * The ID of the human + */ + id: string; + /** + * What this human calls themselves + */ + name: string; + /** + * The movies this human appears in + */ + appearsIn: (Episode | null)[]; +} + +export type anotherFragment = anotherFragment_Droid | anotherFragment_Human;", + }, + "fileName": "anotherFragment.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global fragment with fragment spreads with inline fragment 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global handles multiline graphql comments 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: CustomScalar +// ==================================================== + +export interface CustomScalar_commentTest { + __typename: \\"CommentTest\\"; + /** + * This is a + * multi-line + * comment. + */ + multiLine: string | null; +} + +export interface CustomScalar { + commentTest: CustomScalar_commentTest | null; +}", + }, + "fileName": "CustomScalar.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global handles multiline graphql comments 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global inline fragment 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: HeroInlineFragment +// ==================================================== + +export interface HeroInlineFragment_hero { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; + /** + * The ID of the character + */ + id: string; +} + +export interface HeroInlineFragment { + hero: HeroInlineFragment_hero | null; +} + +export interface HeroInlineFragmentVariables { + episode?: Episode | null; +}", + }, + "fileName": "HeroInlineFragment.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global inline fragment 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global inline fragment on type conditions 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: HeroName +// ==================================================== + +export interface HeroName_hero_Human_friends { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; +} + +export interface HeroName_hero_Human { + __typename: \\"Human\\"; + /** + * What this human calls themselves + */ + name: string; + /** + * The ID of the human + */ + id: string; + /** + * The home planet of the human, or null if unknown + */ + homePlanet: string | null; + /** + * This human's friends, or an empty list if they have none + */ + friends: (HeroName_hero_Human_friends | null)[] | null; +} + +export interface HeroName_hero_Droid { + __typename: \\"Droid\\"; + /** + * What others call this droid + */ + name: string; + /** + * The ID of the droid + */ + id: string; + /** + * The movies this droid appears in + */ + appearsIn: (Episode | null)[]; +} + +export type HeroName_hero = HeroName_hero_Human | HeroName_hero_Droid; + +export interface HeroName { + hero: HeroName_hero | null; +} + +export interface HeroNameVariables { + episode?: Episode | null; +}", + }, + "fileName": "HeroName.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global inline fragment on type conditions 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global inline fragment on type conditions with differing inner fields 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: HeroName +// ==================================================== + +export interface HeroName_hero_Human_friends { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; +} + +export interface HeroName_hero_Human { + __typename: \\"Human\\"; + /** + * What this human calls themselves + */ + name: string; + /** + * The ID of the human + */ + id: string; + /** + * The home planet of the human, or null if unknown + */ + homePlanet: string | null; + /** + * This human's friends, or an empty list if they have none + */ + friends: (HeroName_hero_Human_friends | null)[] | null; +} + +export interface HeroName_hero_Droid_friends { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The ID of the character + */ + id: string; +} + +export interface HeroName_hero_Droid { + __typename: \\"Droid\\"; + /** + * What others call this droid + */ + name: string; + /** + * The ID of the droid + */ + id: string; + /** + * The movies this droid appears in + */ + appearsIn: (Episode | null)[]; + /** + * This droid's friends, or an empty list if they have none + */ + friends: (HeroName_hero_Droid_friends | null)[] | null; +} + +export type HeroName_hero = HeroName_hero_Human | HeroName_hero_Droid; + +export interface HeroName { + hero: HeroName_hero | null; +} + +export interface HeroNameVariables { + episode?: Episode | null; +}", + }, + "fileName": "HeroName.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global inline fragment on type conditions with differing inner fields 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global query with fragment spreads 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: HeroFragment +// ==================================================== + +export interface HeroFragment_hero { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; + /** + * The ID of the character + */ + id: string; +} + +export interface HeroFragment { + hero: HeroFragment_hero | null; +} + +export interface HeroFragmentVariables { + episode?: Episode | null; +}", + }, + "fileName": "HeroFragment.ts", + "sourcePath": "GraphQL request", + }, + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL fragment: simpleFragment +// ==================================================== + +export interface simpleFragment { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; +}", + }, + "fileName": "simpleFragment.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global query with fragment spreads 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global simple fragment 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL fragment: SimpleFragment +// ==================================================== + +export interface SimpleFragment { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; +}", + }, + "fileName": "SimpleFragment.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global simple fragment 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global simple hero query 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: HeroName +// ==================================================== + +export interface HeroName_hero { + __typename: \\"Human\\" | \\"Droid\\"; + /** + * The name of the character + */ + name: string; + /** + * The ID of the character + */ + id: string; +} + +export interface HeroName { + hero: HeroName_hero | null; +} + +export interface HeroNameVariables { + episode?: Episode | null; +}", + }, + "fileName": "HeroName.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global simple hero query 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global simple mutation 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { Episode, ReviewInput, ColorInput } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL mutation operation: ReviewMovie +// ==================================================== + +export interface ReviewMovie_createReview { + __typename: \\"Review\\"; + /** + * The number of stars this review gave, 1-5 + */ + stars: number; + /** + * Comment about the movie + */ + commentary: string | null; +} + +export interface ReviewMovie { + createReview: ReviewMovie_createReview | null; +} + +export interface ReviewMovieVariables { + episode?: Episode | null; + review?: ReviewInput | null; +}", + }, + "fileName": "ReviewMovie.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global simple mutation 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +/** + * The episodes in the Star Wars trilogy + */ +export enum Episode { + EMPIRE = \\"EMPIRE\\", + JEDI = \\"JEDI\\", + NEWHOPE = \\"NEWHOPE\\", +} + +/** + * The input object sent when someone is creating a new review + */ +export interface ReviewInput { + stars: number; + commentary?: string | null; + favorite_color?: ColorInput | null; +} + +/** + * The input object sent when passing in a color + */ +export interface ColorInput { + red: number; + green: number; + blue: number; +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + exports[`Typescript codeGeneration multiple files 1`] = `"generatedFiles"`; exports[`Typescript codeGeneration multiple files 2`] = ` diff --git a/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts b/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts index 1c6147a3c5..307be3ae17 100644 --- a/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts @@ -9,7 +9,7 @@ import { CompilerContext, } from 'apollo-codegen-core/lib/compiler'; -import { generateSource } from '../codeGeneration'; +import { generateSource, generateLocalSource, generateGlobalSource } from '../codeGeneration'; function compile( source: string, @@ -276,3 +276,297 @@ describe('Typescript codeGeneration', () => { expect(output).toMatchSnapshot(); }); }); + +describe('Typescript codeGeneration local / global', () => { + test('simple hero query', () => { + const context = compile(` + query HeroName($episode: Episode) { + hero(episode: $episode) { + name + id + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('simple mutation', () => { + const context = compile(` + mutation ReviewMovie($episode: Episode, $review: ReviewInput) { + createReview(episode: $episode, review: $review) { + stars + commentary + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('simple fragment', () => { + const context = compile(` + fragment SimpleFragment on Character{ + name + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('fragment with fragment spreads', () => { + const context = compile(` + fragment simpleFragment on Character { + name + } + + fragment anotherFragment on Character { + id + ...simpleFragment + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('fragment with fragment spreads with inline fragment', () => { + const context = compile(` + fragment simpleFragment on Character { + name + } + + fragment anotherFragment on Character { + id + ...simpleFragment + + ... on Human { + appearsIn + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('query with fragment spreads', () => { + const context = compile(` + fragment simpleFragment on Character { + name + } + + query HeroFragment($episode: Episode) { + hero(episode: $episode) { + ...simpleFragment + id + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('inline fragment', () => { + const context = compile(` + query HeroInlineFragment($episode: Episode) { + hero(episode: $episode) { + ... on Character { + name + } + id + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }) + + test('inline fragment on type conditions', () => { + const context = compile(` + query HeroName($episode: Episode) { + hero(episode: $episode) { + name + id + + ... on Human { + homePlanet + friends { + name + } + } + + ... on Droid { + appearsIn + } + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('inline fragment on type conditions with differing inner fields', () => { + const context = compile(` + query HeroName($episode: Episode) { + hero(episode: $episode) { + name + id + + ... on Human { + homePlanet + friends { + name + } + } + + ... on Droid { + appearsIn + friends { + id + } + } + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('fragment spreads with inline fragments', () => { + const context = compile(` + query HeroName($episode: Episode) { + hero(episode: $episode) { + name + id + ...humanFragment + ...droidFragment + } + } + + fragment humanFragment on Human { + homePlanet + friends { + ... on Human { + name + } + + ... on Droid { + id + } + } + } + + fragment droidFragment on Droid { + appearsIn + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('handles multiline graphql comments', () => { + const miscSchema = loadSchema(require.resolve('../../../common-test/fixtures/misc/schema.json')); + + const document = parse(` + query CustomScalar { + commentTest { + multiLine + } + } + `); + + const context = compileToIR(miscSchema, document, { + mergeInFieldsFromFragmentSpreads: true, + addTypename: true + }); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); +}); diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index 2dd391277d..219123dbad 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -1,3 +1,4 @@ +import * as path from "path"; import * as t from '@babel/types'; import { stripIndent } from 'common-tags'; import { @@ -67,6 +68,28 @@ function printEnumsAndInputObjects(generator: TypescriptAPIGenerator, typesUsed: `) } +function printGlobalImport( + generator: TypescriptAPIGenerator, + typesUsed: GraphQLType[], + outputPath: string, + globalSourcePath: string, +) { + const types = typesUsed.filter((type) => { + return type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType; + }); + if (types.length > 0) { + const relative = path.relative( + path.dirname(outputPath), + path.join( + path.dirname(globalSourcePath), + path.basename(globalSourcePath, '.ts') + ) + ); + generator.printer.enqueue(generator.import(types, relative)); + } +} + +// TODO: deprecate this, use generateLocalSource and generateGlobalSource instead. export function generateSource( context: CompilerContext, ) { @@ -111,6 +134,75 @@ export function generateSource( }; } +interface IGeneratedFileOptions { + outputPath?: string; + globalSourcePath?: string; +} + +interface IGeneratedFile { + sourcePath: string; + fileName: string; + content: (options?: IGeneratedFileOptions) => TypescriptGeneratedFile; +} + +export function generateLocalSource( + context: CompilerContext, +): IGeneratedFile[] { + const generator = new TypescriptAPIGenerator(context); + + const operations = Object.values(context.operations) + .map((operation) => ({ + sourcePath: operation.filePath, + fileName: `${operation.operationName}.ts`, + content: (options?: IGeneratedFileOptions) => { + generator.fileHeader(); + if (options && options.outputPath && options.globalSourcePath) { + printGlobalImport( + generator, + generator.getTypesUsedForOperation(operation, context), + options.outputPath, + options.globalSourcePath + ); + } + generator.interfacesForOperation(operation); + const output = generator.printer.printAndClear(); + return new TypescriptGeneratedFile(output); + }, + })); + + const fragments = Object.values(context.fragments) + .map((fragment) => ({ + sourcePath: fragment.filePath, + fileName: `${fragment.fragmentName}.ts`, + content: (options?: IGeneratedFileOptions) => { + generator.fileHeader(); + if (options && options.outputPath && options.globalSourcePath) { + printGlobalImport( + generator, + generator.getTypesUsedForOperation(fragment, context), + options.outputPath, + options.globalSourcePath + ); + } + generator.interfacesForFragment(fragment); + const output = generator.printer.printAndClear(); + return new TypescriptGeneratedFile(output); + }, + })); + + return operations.concat(fragments); +} + +export function generateGlobalSource( + context: CompilerContext, +): TypescriptGeneratedFile { + const generator = new TypescriptAPIGenerator(context); + generator.fileHeader(); + printEnumsAndInputObjects(generator, context.typesUsed); + const output = generator.printer.printAndClear(); + return new TypescriptGeneratedFile(output); +} + export class TypescriptAPIGenerator extends TypescriptGenerator { context: CompilerContext printer: Printer diff --git a/packages/apollo-codegen-typescript/src/index.ts b/packages/apollo-codegen-typescript/src/index.ts index ce3df3895f..16a723fb15 100644 --- a/packages/apollo-codegen-typescript/src/index.ts +++ b/packages/apollo-codegen-typescript/src/index.ts @@ -1 +1 @@ -export { generateSource } from './codeGeneration'; +export { generateSource, generateLocalSource, generateGlobalSource } from './codeGeneration'; diff --git a/packages/apollo-codegen-typescript/src/language.ts b/packages/apollo-codegen-typescript/src/language.ts index 42de0f2762..e718e1d186 100644 --- a/packages/apollo-codegen-typescript/src/language.ts +++ b/packages/apollo-codegen-typescript/src/language.ts @@ -1,6 +1,7 @@ import { GraphQLEnumType, - GraphQLInputObjectType + GraphQLInputObjectType, + GraphQLType } from 'graphql'; import { @@ -163,4 +164,14 @@ export default class TypescriptGenerator { public isNullableType(type: t.TSType) { return t.isTSUnionType(type) && type.types.some(type => t.isTSNullKeyword(type)); } + + public import(types: GraphQLType[], source: string) { + return t.importDeclaration( + types.map((type) => t.importSpecifier( + t.identifier(type.toString()), + t.identifier(type.toString()), + )), + t.stringLiteral(source) + ); + } } From eb863631daebb71405240ead10fbe88dfcc4025b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20B=C3=BCrger?= Date: Tue, 31 Jul 2018 09:32:11 +0200 Subject: [PATCH 2/4] [TS] Only import global types that are being used --- .../__snapshots__/codeGeneration.ts.snap | 8 +- .../src/codeGeneration.ts | 73 +++++++++++++++++-- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap index b23fcb6378..ac3ac5583f 100644 --- a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap +++ b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap @@ -735,8 +735,6 @@ export interface HeroNameVariables { /* tslint:disable */ // This file was automatically generated and should not be edited. -import { Episode } from \\"../../__generated__/globalTypes\\"; - // ==================================================== // GraphQL fragment: humanFragment // ==================================================== @@ -906,8 +904,6 @@ Array [ /* tslint:disable */ // This file was automatically generated and should not be edited. -import { Episode } from \\"../../__generated__/globalTypes\\"; - // ==================================================== // GraphQL fragment: simpleFragment // ==================================================== @@ -1374,8 +1370,6 @@ export interface HeroFragmentVariables { /* tslint:disable */ // This file was automatically generated and should not be edited. -import { Episode } from \\"../../__generated__/globalTypes\\"; - // ==================================================== // GraphQL fragment: simpleFragment // ==================================================== @@ -1540,7 +1534,7 @@ Array [ /* tslint:disable */ // This file was automatically generated and should not be edited. -import { Episode, ReviewInput, ColorInput } from \\"../../__generated__/globalTypes\\"; +import { Episode, ReviewInput } from \\"../../__generated__/globalTypes\\"; // ==================================================== // GraphQL mutation operation: ReviewMovie diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index 219123dbad..a7e91518d6 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -74,10 +74,7 @@ function printGlobalImport( outputPath: string, globalSourcePath: string, ) { - const types = typesUsed.filter((type) => { - return type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType; - }); - if (types.length > 0) { + if (typesUsed.length > 0) { const relative = path.relative( path.dirname(outputPath), path.join( @@ -85,7 +82,7 @@ function printGlobalImport( path.basename(globalSourcePath, '.ts') ) ); - generator.printer.enqueue(generator.import(types, relative)); + generator.printer.enqueue(generator.import(typesUsed, relative)); } } @@ -159,7 +156,7 @@ export function generateLocalSource( if (options && options.outputPath && options.globalSourcePath) { printGlobalImport( generator, - generator.getTypesUsedForOperation(operation, context), + generator.getGlobalTypesUsedForOperation(operation, context), options.outputPath, options.globalSourcePath ); @@ -179,7 +176,7 @@ export function generateLocalSource( if (options && options.outputPath && options.globalSourcePath) { printGlobalImport( generator, - generator.getTypesUsedForOperation(fragment, context), + generator.getGlobalTypesUsedForOperation(fragment, context), options.outputPath, options.globalSourcePath ); @@ -338,6 +335,68 @@ export class TypescriptAPIGenerator extends TypescriptGenerator { this.scopeStackPop(); } + public getGlobalTypesUsedForOperation(doc: Operation | Fragment, context: CompilerContext) { + let docTypesUsed: GraphQLType[] = []; + + if (doc.hasOwnProperty('operationName')) { + const operation = doc as Operation; + docTypesUsed = operation.variables.map(({ type }) => type); + } + + const reduceTypesForDocument = ( + nested: SelectionSet, + acc: GraphQLType[] + ) => { + acc = nested.selections + .reduce((selectionAcc, selection) => { + switch (selection.kind) { + case 'Field': + case 'TypeCondition': + if (selection.selectionSet) { + selectionAcc = reduceTypesForDocument(selection.selectionSet, selectionAcc); + } else { + selectionAcc = maybePush(selectionAcc, selection.type); + } + break; + case 'FragmentSpread': + selectionAcc = reduceTypesForDocument(selection.selectionSet, selectionAcc); + break; + default: + break; + } + + return selectionAcc; + }, acc); + + return acc; + } + + docTypesUsed = reduceTypesForDocument(doc.selectionSet, docTypesUsed) + .reduce(this.reduceGlobalTypesUsed, []); + + return context.typesUsed.filter((type) => { + return (type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType) + && docTypesUsed.find((typeUsed) => type === typeUsed); + }); + } + + private reduceGlobalTypesUsed = ( + acc: (GraphQLType | GraphQLOutputType)[], + type: GraphQLType + ) =>{ + if (type instanceof GraphQLNonNull) { + type = getNullableType(type); + } + + if (type instanceof GraphQLList) { + type = type.ofType + } + + acc = maybePush(acc, type); + + return acc; + } + public getTypesUsedForOperation(doc: Operation | Fragment, context: CompilerContext) { let docTypesUsed: GraphQLType[] = []; From 76b946258d1283f13da045a70eebc8f847bc99ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20B=C3=BCrger?= Date: Wed, 1 Aug 2018 20:29:27 +0200 Subject: [PATCH 3/4] Sync text schema.graphql and .json --- .../common-test/fixtures/misc/schema.graphql | 6 +- .../common-test/fixtures/misc/schema.json | 2457 ++++++++--------- 2 files changed, 1153 insertions(+), 1310 deletions(-) diff --git a/packages/common-test/fixtures/misc/schema.graphql b/packages/common-test/fixtures/misc/schema.graphql index 4e4aeae20b..a670ecb153 100644 --- a/packages/common-test/fixtures/misc/schema.graphql +++ b/packages/common-test/fixtures/misc/schema.graphql @@ -19,7 +19,8 @@ type CommentTest { "This is a single-line comment" singleLine: String """ - This is a multi-line + This is a + multi-line comment. """ multiLine: String @@ -30,7 +31,8 @@ enum EnumCommentTestCase { "This is a single-line comment" first """ - This is a multi-line + This is a + multi-line comment. """ second diff --git a/packages/common-test/fixtures/misc/schema.json b/packages/common-test/fixtures/misc/schema.json index 109612af0f..1ae69710d8 100644 --- a/packages/common-test/fixtures/misc/schema.json +++ b/packages/common-test/fixtures/misc/schema.json @@ -6,1380 +6,1221 @@ }, "mutationType": null, "subscriptionType": null, - "types": [ - { - "kind": "OBJECT", - "name": "Query", - "description": "", - "fields": [ - { - "name": "misc", - "description": "", - "args": [], - "type": { - "kind": "OBJECT", - "name": "OddType", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "commentTest", - "description": "", - "args": [], - "type": { - "kind": "OBJECT", - "name": "CommentTest", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "interfaceTest", - "description": "", - "args": [], - "type": { - "kind": "INTERFACE", - "name": "InterfaceTestCase", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "unionTest", - "description": "", - "args": [], - "type": { - "kind": "UNION", - "name": "UnionTestCase", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "scalarTest", - "description": "", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null + "types": [{ + "kind": "OBJECT", + "name": "Query", + "description": null, + "fields": [{ + "name": "misc", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "OddType", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "commentTest", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "CommentTest", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "interfaceTest", + "description": null, + "args": [], + "type": { + "kind": "INTERFACE", + "name": "InterfaceTestCase", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "unionTest", + "description": null, + "args": [], + "type": { + "kind": "UNION", + "name": "UnionTestCase", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "scalarTest", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "OddType", - "description": "", - "fields": [ - { - "name": "date", - "description": "", - "args": [], - "type": { - "kind": "SCALAR", - "name": "Date", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "camel", - "description": "", - "args": [], - "type": { - "kind": "OBJECT", - "name": "doubleHump", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "OddType", + "description": null, + "fields": [{ + "name": "date", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "Date", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "camel", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "doubleHump", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "SCALAR", + "name": "Date", + "description": null, + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "doubleHump", + "description": null, + "fields": [{ + "name": "humpOne", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "humpTwo", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "SCALAR", + "name": "String", + "description": "The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "CommentTest", + "description": null, + "fields": [{ + "name": "singleLine", + "description": "This is a single-line comment", + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "multiLine", + "description": "This is a\nmulti-line\ncomment.", + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "enumCommentTest", + "description": null, + "args": [], + "type": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "description": null, + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [{ + "name": "first", + "description": "This is a single-line comment", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "second", + "description": "This is a\nmulti-line\ncomment.", + "isDeprecated": false, + "deprecationReason": null + }], + "possibleTypes": null + }, { + "kind": "INTERFACE", + "name": "InterfaceTestCase", + "description": null, + "fields": [{ + "name": "prop", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "SCALAR", - "name": "Date", - "description": "", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": null, - "possibleTypes": null - }, - { + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": [{ "kind": "OBJECT", - "name": "doubleHump", - "description": "", - "fields": [ - { - "name": "humpOne", - "description": "", - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "humpTwo", - "description": "", - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "SCALAR", - "name": "String", - "description": "The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": null, - "possibleTypes": null - }, - { + "name": "ImplA", + "ofType": null + }, { "kind": "OBJECT", - "name": "CommentTest", - "description": "This is a type to test comments", - "fields": [ - { - "name": "singleLine", - "description": "This is a single-line comment", - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "multiLine", - "description": "This is a\nmulti-line\ncomment.", - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "enumCommentTest", - "description": "", - "args": [], - "type": { - "kind": "ENUM", - "name": "EnumCommentTestCase", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null + "name": "ImplB", + "ofType": null + }] + }, { + "kind": "UNION", + "name": "UnionTestCase", + "description": null, + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": [{ + "kind": "OBJECT", + "name": "PartialA", + "ofType": null + }, { + "kind": "OBJECT", + "name": "PartialB", + "ofType": null + }] + }, { + "kind": "OBJECT", + "name": "PartialA", + "description": null, + "fields": [{ + "name": "prop", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "ENUM", - "name": "EnumCommentTestCase", - "description": "", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": [ - { - "name": "first", - "description": "This is a single-line comment", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "second", - "description": "This is a\nmulti-line\ncomment.", - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "PartialB", + "description": null, + "fields": [{ + "name": "prop", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null } - ], - "possibleTypes": null - }, - { - "kind": "INTERFACE", - "name": "InterfaceTestCase", - "description": "", - "fields": [ - { - "name": "prop", - "description": "", - "args": [], - "type": { + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "SCALAR", + "name": "Boolean", + "description": "The `Boolean` scalar type represents `true` or `false`.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "__Schema", + "description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.", + "fields": [{ + "name": "types", + "description": "A list of all types supported by this server.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { "kind": "NON_NULL", "name": null, "ofType": { - "kind": "SCALAR", - "name": "String", + "kind": "OBJECT", + "name": "__Type", "ofType": null } - }, - "isDeprecated": false, - "deprecationReason": null + } } - ], - "inputFields": null, - "interfaces": null, - "enumValues": null, - "possibleTypes": [ - { + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "queryType", + "description": "The type that query operations will be rooted at.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", - "name": "ImplA", - "ofType": null - }, - { - "kind": "OBJECT", - "name": "ImplB", + "name": "__Type", "ofType": null } - ] - }, - { - "kind": "UNION", - "name": "UnionTestCase", - "description": "", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": null, - "possibleTypes": [ - { - "kind": "OBJECT", - "name": "PartialA", - "ofType": null - }, - { - "kind": "OBJECT", - "name": "PartialB", - "ofType": null - } - ] - }, - { - "kind": "OBJECT", - "name": "PartialA", - "description": "", - "fields": [ - { - "name": "prop", - "description": "", - "args": [], - "type": { + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "mutationType", + "description": "If this server supports mutation, the type that mutation operations will be rooted at.", + "args": [], + "type": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "subscriptionType", + "description": "If this server support subscription, the type that subscription operations will be rooted at.", + "args": [], + "type": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "directives", + "description": "A list of all directives supported by this server.", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { "kind": "NON_NULL", "name": null, "ofType": { - "kind": "SCALAR", - "name": "String", + "kind": "OBJECT", + "name": "__Directive", "ofType": null } - }, - "isDeprecated": false, - "deprecationReason": null + } } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "PartialB", - "description": "", - "fields": [ - { - "name": "prop", - "description": "", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "__Type", + "description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.", + "fields": [{ + "name": "kind", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "__TypeKind", + "ofType": null } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "SCALAR", - "name": "Boolean", - "description": "The `Boolean` scalar type represents `true` or `false`.", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "__Schema", - "description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.", - "fields": [ - { - "name": "types", - "description": "A list of all types supported by this server.", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Type", - "ofType": null - } - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "queryType", - "description": "The type that query operations will be rooted at.", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Type", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "fields", + "description": null, + "args": [{ + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null }, - { - "name": "mutationType", - "description": "If this server supports mutation, the type that mutation operations will be rooted at.", - "args": [], - "type": { + "defaultValue": "false" + }], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", - "name": "__Type", + "name": "__Field", "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "subscriptionType", - "description": "If this server support subscription, the type that subscription operations will be rooted at.", - "args": [], - "type": { + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "interfaces", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "__Type", "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "directives", - "description": "A list of all directives supported by this server.", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Directive", - "ofType": null - } - } - } - }, - "isDeprecated": false, - "deprecationReason": null + } } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "__Type", - "description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.", - "fields": [ - { - "name": "kind", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "ENUM", - "name": "__TypeKind", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "name", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "description", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "fields", - "description": null, - "args": [ - { - "name": "includeDeprecated", - "description": null, - "type": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - }, - "defaultValue": "false" - } - ], - "type": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Field", - "ofType": null - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "interfaces", - "description": null, - "args": [], - "type": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Type", - "ofType": null - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "possibleTypes", - "description": null, - "args": [], - "type": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Type", - "ofType": null - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "enumValues", - "description": null, - "args": [ - { - "name": "includeDeprecated", - "description": null, - "type": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - }, - "defaultValue": "false" - } - ], - "type": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__EnumValue", - "ofType": null - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "inputFields", - "description": null, - "args": [], - "type": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__InputValue", - "ofType": null - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "ofType", - "description": null, - "args": [], - "type": { + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "possibleTypes", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "__Type", "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "ENUM", - "name": "__TypeKind", - "description": "An enum describing what kind of type a given `__Type` is.", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": [ - { - "name": "SCALAR", - "description": "Indicates this type is a scalar.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "OBJECT", - "description": "Indicates this type is an object. `fields` and `interfaces` are valid fields.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "INTERFACE", - "description": "Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "UNION", - "description": "Indicates this type is a union. `possibleTypes` is a valid field.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "ENUM", - "description": "Indicates this type is an enum. `enumValues` is a valid field.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "INPUT_OBJECT", - "description": "Indicates this type is an input object. `inputFields` is a valid field.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "LIST", - "description": "Indicates this type is a list. `ofType` is a valid field.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "NON_NULL", - "description": "Indicates this type is a non-null. `ofType` is a valid field.", - "isDeprecated": false, - "deprecationReason": null + } } - ], - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "__Field", - "description": "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.", - "fields": [ - { - "name": "name", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "description", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "args", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__InputValue", - "ofType": null - } - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "type", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__Type", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "isDeprecated", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "enumValues", + "description": null, + "args": [{ + "name": "includeDeprecated", + "description": null, + "type": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null }, - { - "name": "deprecationReason", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", + "defaultValue": "false" + }], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__EnumValue", "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null + } } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "__InputValue", - "description": "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.", - "fields": [ - { - "name": "name", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "description", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "inputFields", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__InputValue", "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "type", - "description": null, - "args": [], - "type": { + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "ofType", + "description": null, + "args": [], + "type": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "ENUM", + "name": "__TypeKind", + "description": "An enum describing what kind of type a given `__Type` is.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [{ + "name": "SCALAR", + "description": "Indicates this type is a scalar.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "OBJECT", + "description": "Indicates this type is an object. `fields` and `interfaces` are valid fields.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "INTERFACE", + "description": "Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "UNION", + "description": "Indicates this type is a union. `possibleTypes` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "ENUM", + "description": "Indicates this type is an enum. `enumValues` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "INPUT_OBJECT", + "description": "Indicates this type is an input object. `inputFields` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "LIST", + "description": "Indicates this type is a list. `ofType` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "NON_NULL", + "description": "Indicates this type is a non-null. `ofType` is a valid field.", + "isDeprecated": false, + "deprecationReason": null + }], + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "__Field", + "description": "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.", + "fields": [{ + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "args", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { "kind": "NON_NULL", "name": null, "ofType": { "kind": "OBJECT", - "name": "__Type", + "name": "__InputValue", "ofType": null } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "defaultValue", - "description": "A GraphQL-formatted string representing the default value for this input value.", - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null + } } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "__EnumValue", - "description": "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.", - "fields": [ - { - "name": "name", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "description", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "isDeprecated", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "deprecationReason", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "type", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "__Directive", - "description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", - "fields": [ - { - "name": "name", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "description", - "description": null, - "args": [], - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "locations", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "ENUM", - "name": "__DirectiveLocation", - "ofType": null - } - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "args", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "LIST", - "name": null, - "ofType": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "OBJECT", - "name": "__InputValue", - "ofType": null - } - } - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "onOperation", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - } - }, - "isDeprecated": true, - "deprecationReason": "Use `locations`." - }, - { - "name": "onFragment", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - } - }, - "isDeprecated": true, - "deprecationReason": "Use `locations`." - }, - { - "name": "onField", - "description": null, - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "Boolean", - "ofType": null - } - }, - "isDeprecated": true, - "deprecationReason": "Use `locations`." + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null } - ], - "inputFields": null, - "interfaces": [], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "ENUM", - "name": "__DirectiveLocation", - "description": "A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": [ - { - "name": "QUERY", - "description": "Location adjacent to a query operation.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "MUTATION", - "description": "Location adjacent to a mutation operation.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "SUBSCRIPTION", - "description": "Location adjacent to a subscription operation.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "FIELD", - "description": "Location adjacent to a field.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "FRAGMENT_DEFINITION", - "description": "Location adjacent to a fragment definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "FRAGMENT_SPREAD", - "description": "Location adjacent to a fragment spread.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "INLINE_FRAGMENT", - "description": "Location adjacent to an inline fragment.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "SCHEMA", - "description": "Location adjacent to a schema definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "SCALAR", - "description": "Location adjacent to a scalar definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "OBJECT", - "description": "Location adjacent to an object type definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "FIELD_DEFINITION", - "description": "Location adjacent to a field definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "ARGUMENT_DEFINITION", - "description": "Location adjacent to an argument definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "INTERFACE", - "description": "Location adjacent to an interface definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "UNION", - "description": "Location adjacent to a union definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "ENUM", - "description": "Location adjacent to an enum definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "ENUM_VALUE", - "description": "Location adjacent to an enum value definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "INPUT_OBJECT", - "description": "Location adjacent to an input object type definition.", - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "INPUT_FIELD_DEFINITION", - "description": "Location adjacent to an input object field definition.", - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "__InputValue", + "description": "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.", + "fields": [{ + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null } - ], - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "ImplA", - "description": "", - "fields": [ - { - "name": "prop", - "description": "", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "propA", - "description": "", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "type", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "__Type", + "ofType": null } - ], - "inputFields": null, - "interfaces": [ - { - "kind": "INTERFACE", - "name": "InterfaceTestCase", + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "defaultValue", + "description": "A GraphQL-formatted string representing the default value for this input value.", + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "__EnumValue", + "description": "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.", + "fields": [{ + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", "ofType": null } - ], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "OBJECT", - "name": "ImplB", - "description": "", - "fields": [ - { - "name": "prop", - "description": "", - "args": [], - "type": { - "kind": "NON_NULL", - "name": null, - "ofType": { - "kind": "SCALAR", - "name": "String", - "ofType": null - } - }, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "propB", - "description": "", - "args": [], - "type": { - "kind": "SCALAR", - "name": "Int", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "isDeprecated", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null } - ], - "inputFields": null, - "interfaces": [ - { - "kind": "INTERFACE", - "name": "InterfaceTestCase", + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "deprecationReason", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "__Directive", + "description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", + "fields": [{ + "name": "name", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", "ofType": null } - ], - "enumValues": null, - "possibleTypes": null - }, - { - "kind": "SCALAR", - "name": "Int", - "description": "The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1. ", - "fields": null, - "inputFields": null, - "interfaces": null, - "enumValues": null, - "possibleTypes": null - } - ], - "directives": [ - { - "name": "skip", - "description": "Directs the executor to skip this field or fragment when the `if` argument is true.", - "locations": [ - "FIELD", - "FRAGMENT_SPREAD", - "INLINE_FRAGMENT" - ], - "args": [ - { - "name": "if", - "description": "Skipped when true.", - "type": { + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "description", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "locations", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { "kind": "NON_NULL", "name": null, "ofType": { - "kind": "SCALAR", - "name": "Boolean", + "kind": "ENUM", + "name": "__DirectiveLocation", "ofType": null } - }, - "defaultValue": null + } } - ] - }, - { - "name": "include", - "description": "Directs the executor to include this field or fragment only when the `if` argument is true.", - "locations": [ - "FIELD", - "FRAGMENT_SPREAD", - "INLINE_FRAGMENT" - ], - "args": [ - { - "name": "if", - "description": "Included when true.", - "type": { + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "args", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { "kind": "NON_NULL", "name": null, "ofType": { - "kind": "SCALAR", - "name": "Boolean", + "kind": "OBJECT", + "name": "__InputValue", "ofType": null } - }, - "defaultValue": null + } } - ] - }, - { - "name": "deprecated", - "description": "Marks an element of a GraphQL schema as no longer supported.", - "locations": [ - "FIELD_DEFINITION", - "ENUM_VALUE" - ], - "args": [ - { - "name": "reason", - "description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted in [Markdown](https://daringfireball.net/projects/markdown/).", - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "defaultValue": "\"No longer supported\"" + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "onOperation", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "isDeprecated": true, + "deprecationReason": "Use `locations`." + }, { + "name": "onFragment", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "isDeprecated": true, + "deprecationReason": "Use `locations`." + }, { + "name": "onField", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "isDeprecated": true, + "deprecationReason": "Use `locations`." + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "ENUM", + "name": "__DirectiveLocation", + "description": "A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies.", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [{ + "name": "QUERY", + "description": "Location adjacent to a query operation.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "MUTATION", + "description": "Location adjacent to a mutation operation.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "SUBSCRIPTION", + "description": "Location adjacent to a subscription operation.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "FIELD", + "description": "Location adjacent to a field.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "FRAGMENT_DEFINITION", + "description": "Location adjacent to a fragment definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "FRAGMENT_SPREAD", + "description": "Location adjacent to a fragment spread.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "INLINE_FRAGMENT", + "description": "Location adjacent to an inline fragment.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "SCHEMA", + "description": "Location adjacent to a schema definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "SCALAR", + "description": "Location adjacent to a scalar definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "OBJECT", + "description": "Location adjacent to an object type definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "FIELD_DEFINITION", + "description": "Location adjacent to a field definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "ARGUMENT_DEFINITION", + "description": "Location adjacent to an argument definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "INTERFACE", + "description": "Location adjacent to an interface definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "UNION", + "description": "Location adjacent to a union definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "ENUM", + "description": "Location adjacent to an enum definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "ENUM_VALUE", + "description": "Location adjacent to an enum value definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "INPUT_OBJECT", + "description": "Location adjacent to an input object type definition.", + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "INPUT_FIELD_DEFINITION", + "description": "Location adjacent to an input object field definition.", + "isDeprecated": false, + "deprecationReason": null + }], + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "ImplA", + "description": null, + "fields": [{ + "name": "prop", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "propA", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [{ + "kind": "INTERFACE", + "name": "InterfaceTestCase", + "ofType": null + }], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "ImplB", + "description": null, + "fields": [{ + "name": "prop", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "propB", + "description": null, + "args": [], + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [{ + "kind": "INTERFACE", + "name": "InterfaceTestCase", + "ofType": null + }], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "SCALAR", + "name": "Int", + "description": "The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1. ", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }], + "directives": [{ + "name": "skip", + "description": "Directs the executor to skip this field or fragment when the `if` argument is true.", + "locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"], + "args": [{ + "name": "if", + "description": "Skipped when true.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null + } + }, + "defaultValue": null + }] + }, { + "name": "include", + "description": "Directs the executor to include this field or fragment only when the `if` argument is true.", + "locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"], + "args": [{ + "name": "if", + "description": "Included when true.", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "Boolean", + "ofType": null } - ] - } - ] + }, + "defaultValue": null + }] + }, { + "name": "deprecated", + "description": "Marks an element of a GraphQL schema as no longer supported.", + "locations": ["FIELD_DEFINITION", "ENUM_VALUE"], + "args": [{ + "name": "reason", + "description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted in [Markdown](https://daringfireball.net/projects/markdown/).", + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "defaultValue": "\"No longer supported\"" + }] + }] } } } From 2a9a24bc4efd837288a1d0431970eda24b1038cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20B=C3=BCrger?= Date: Wed, 1 Aug 2018 22:33:31 +0200 Subject: [PATCH 4/4] [TS] Refactored getGlobalTypesUsedForOperation --- .../__snapshots__/codeGeneration.ts.snap | 168 ++++++++++++++++ .../src/__tests__/codeGeneration.ts | 94 +++++++-- .../src/codeGeneration.ts | 84 ++++---- .../common-test/fixtures/misc/schema.graphql | 16 ++ .../common-test/fixtures/misc/schema.json | 184 +++++++++++++++++- 5 files changed, 477 insertions(+), 69 deletions(-) diff --git a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap index ac3ac5583f..c2c94a0283 100644 --- a/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap +++ b/packages/apollo-codegen-typescript/src/__tests__/__snapshots__/codeGeneration.ts.snap @@ -646,6 +646,70 @@ export interface HeroNameVariables { } `; +exports[`Typescript codeGeneration local / global duplicates 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { EnumCommentTestCase, Duplicate } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL mutation operation: duplicates +// ==================================================== + +export interface duplicates_duplicates { + __typename: \\"Nesting\\"; + propA: EnumCommentTestCase[][]; + propB: ((EnumCommentTestCase | null)[] | null)[] | null; +} + +export interface duplicates { + duplicates: duplicates_duplicates; +} + +export interface duplicatesVariables { + a: EnumCommentTestCase; + b: EnumCommentTestCase; + c: Duplicate; +}", + }, + "fileName": "duplicates.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global duplicates 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +export enum EnumCommentTestCase { + first = \\"first\\", + second = \\"second\\", +} + +export interface Duplicate { + propA: EnumCommentTestCase; + propB: EnumCommentTestCase[]; +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + exports[`Typescript codeGeneration local / global fragment spreads with inline fragments 1`] = ` Array [ Object { @@ -1325,6 +1389,110 @@ export enum Episode { } `; +exports[`Typescript codeGeneration local / global multiple nested list enum 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { EnumCommentTestCase } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: nesting +// ==================================================== + +export interface nesting_nesting { + __typename: \\"Nesting\\"; + propB: ((EnumCommentTestCase | null)[] | null)[] | null; +} + +export interface nesting { + nesting: nesting_nesting; +}", + }, + "fileName": "nesting.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global multiple nested list enum 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +export enum EnumCommentTestCase { + first = \\"first\\", + second = \\"second\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + +exports[`Typescript codeGeneration local / global multiple nested non-null list enum 1`] = ` +Array [ + Object { + "content": TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +import { EnumCommentTestCase } from \\"../../__generated__/globalTypes\\"; + +// ==================================================== +// GraphQL query operation: nesting +// ==================================================== + +export interface nesting_nesting { + __typename: \\"Nesting\\"; + propA: EnumCommentTestCase[][]; +} + +export interface nesting { + nesting: nesting_nesting; +}", + }, + "fileName": "nesting.ts", + "sourcePath": "GraphQL request", + }, +] +`; + +exports[`Typescript codeGeneration local / global multiple nested non-null list enum 2`] = ` +TypescriptGeneratedFile { + "fileContents": " + +/* tslint:disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +export enum EnumCommentTestCase { + first = \\"first\\", + second = \\"second\\", +} + +//============================================================== +// END Enums and Input Objects +//==============================================================", +} +`; + exports[`Typescript codeGeneration local / global query with fragment spreads 1`] = ` Array [ Object { diff --git a/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts b/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts index 307be3ae17..8ef5f42126 100644 --- a/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/__tests__/codeGeneration.ts @@ -2,6 +2,7 @@ import { parse } from 'graphql'; import { loadSchema } from 'apollo-codegen-core/lib/loading'; const schema = loadSchema(require.resolve('../../../common-test/fixtures/starwars/schema.json')); +const miscSchema = loadSchema(require.resolve('../../../common-test/fixtures/misc/schema.json')); import { compileToIR, @@ -16,12 +17,23 @@ function compile( options: CompilerOptions = { mergeInFieldsFromFragmentSpreads: true, addTypename: true - } + }, ): CompilerContext { const document = parse(source); return compileToIR(schema, document, options); } +function compileMisc( + source: string, + options: CompilerOptions = { + mergeInFieldsFromFragmentSpreads: true, + addTypename: true + }, +): CompilerContext { + const document = parse(source); + return compileToIR(miscSchema, document, options); +} + describe('Typescript codeGeneration', () => { test('multiple files', () => { const context = compile(` @@ -256,9 +268,7 @@ describe('Typescript codeGeneration', () => { }); test('handles multiline graphql comments', () => { - const miscSchema = loadSchema(require.resolve('../../../common-test/fixtures/misc/schema.json')); - - const document = parse(` + const context = compileMisc(` query CustomScalar { commentTest { multiLine @@ -266,13 +276,7 @@ describe('Typescript codeGeneration', () => { } `); - const output = generateSource( - compileToIR(miscSchema, document, { - mergeInFieldsFromFragmentSpreads: true, - addTypename: true - }) - ); - + const output = generateSource(context); expect(output).toMatchSnapshot(); }); }); @@ -544,9 +548,7 @@ describe('Typescript codeGeneration local / global', () => { }); test('handles multiline graphql comments', () => { - const miscSchema = loadSchema(require.resolve('../../../common-test/fixtures/misc/schema.json')); - - const document = parse(` + const context = compileMisc(` query CustomScalar { commentTest { multiLine @@ -554,10 +556,66 @@ describe('Typescript codeGeneration local / global', () => { } `); - const context = compileToIR(miscSchema, document, { - mergeInFieldsFromFragmentSpreads: true, - addTypename: true - }); + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('multiple nested non-null list enum', () => { + const context = compileMisc(` + query nesting { + nesting { + propA + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('multiple nested list enum', () => { + const context = compileMisc(` + query nesting { + nesting { + propB + } + } + `); + + const output = generateLocalSource(context).map((f) => ({ + ...f, + content: f.content({ + outputPath: '/some/file/ComponentA.tsx', + globalSourcePath: '/__generated__/globalTypes.ts' + }), + })); + expect(output).toMatchSnapshot(); + expect(generateGlobalSource(context)).toMatchSnapshot(); + }); + + test('duplicates', () => { + const context = compileMisc(` + mutation duplicates($a: EnumCommentTestCase!, $b: EnumCommentTestCase!, $c: Duplicate!) { + duplicates(a: $a, b: $b, c: $c) { + propA + propB + } + } + `); const output = generateLocalSource(context).map((f) => ({ ...f, diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index a7e91518d6..e7a2a0a19c 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -10,6 +10,7 @@ import { CompilerContext, Operation, Fragment, + Selection, SelectionSet, Field, FragmentSpread @@ -156,7 +157,7 @@ export function generateLocalSource( if (options && options.outputPath && options.globalSourcePath) { printGlobalImport( generator, - generator.getGlobalTypesUsedForOperation(operation, context), + generator.getGlobalTypesUsedForOperation(operation), options.outputPath, options.globalSourcePath ); @@ -176,7 +177,7 @@ export function generateLocalSource( if (options && options.outputPath && options.globalSourcePath) { printGlobalImport( generator, - generator.getGlobalTypesUsedForOperation(fragment, context), + generator.getGlobalTypesUsedForFragment(fragment), options.outputPath, options.globalSourcePath ); @@ -335,66 +336,49 @@ export class TypescriptAPIGenerator extends TypescriptGenerator { this.scopeStackPop(); } - public getGlobalTypesUsedForOperation(doc: Operation | Fragment, context: CompilerContext) { - let docTypesUsed: GraphQLType[] = []; - - if (doc.hasOwnProperty('operationName')) { - const operation = doc as Operation; - docTypesUsed = operation.variables.map(({ type }) => type); - } + public getGlobalTypesUsedForOperation = (doc: Operation) => { + const typesUsed = doc.variables + .reduce((acc: GraphQLType[], { type } : { type : GraphQLType }) => { + const t = this.getUnderlyingType(type); + if (this.isGlobalType(t)) { + return maybePush(acc, t); + } + return acc; + }, []); + return doc.selectionSet.selections.reduce(this.reduceSelection, typesUsed); + } - const reduceTypesForDocument = ( - nested: SelectionSet, - acc: GraphQLType[] - ) => { - acc = nested.selections - .reduce((selectionAcc, selection) => { - switch (selection.kind) { - case 'Field': - case 'TypeCondition': - if (selection.selectionSet) { - selectionAcc = reduceTypesForDocument(selection.selectionSet, selectionAcc); - } else { - selectionAcc = maybePush(selectionAcc, selection.type); - } - break; - case 'FragmentSpread': - selectionAcc = reduceTypesForDocument(selection.selectionSet, selectionAcc); - break; - default: - break; - } + public getGlobalTypesUsedForFragment = (doc: Fragment) => { + return doc.selectionSet.selections.reduce(this.reduceSelection, []); + } - return selectionAcc; - }, acc); + private reduceSelection = (acc: GraphQLType[], selection: Selection): GraphQLType[] => { + if (selection.kind === 'Field' || selection.kind === 'TypeCondition') { + const type = this.getUnderlyingType(selection.type); + if (this.isGlobalType(type)) { + acc = maybePush(acc, type); + } + } - return acc; + if (selection.selectionSet) { + return selection.selectionSet.selections.reduce(this.reduceSelection, acc); } - docTypesUsed = reduceTypesForDocument(doc.selectionSet, docTypesUsed) - .reduce(this.reduceGlobalTypesUsed, []); + return acc; + } - return context.typesUsed.filter((type) => { - return (type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType) - && docTypesUsed.find((typeUsed) => type === typeUsed); - }); + private isGlobalType = (type: GraphQLType) => { + return type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType; } - private reduceGlobalTypesUsed = ( - acc: (GraphQLType | GraphQLOutputType)[], - type: GraphQLType - ) =>{ + private getUnderlyingType = (type: GraphQLType): GraphQLType => { if (type instanceof GraphQLNonNull) { - type = getNullableType(type); + return this.getUnderlyingType(getNullableType(type)); } - if (type instanceof GraphQLList) { - type = type.ofType + return this.getUnderlyingType(type.ofType); } - - acc = maybePush(acc, type); - - return acc; + return type; } public getTypesUsedForOperation(doc: Operation | Fragment, context: CompilerContext) { diff --git a/packages/common-test/fixtures/misc/schema.graphql b/packages/common-test/fixtures/misc/schema.graphql index a670ecb153..c5a86a60ab 100644 --- a/packages/common-test/fixtures/misc/schema.graphql +++ b/packages/common-test/fixtures/misc/schema.graphql @@ -1,5 +1,6 @@ schema { query: Query + mutation: Mutation } scalar Date @@ -60,6 +61,16 @@ type PartialB { prop: String! } +type Nesting { + propA: [[EnumCommentTestCase!]!]! + propB: [[EnumCommentTestCase]] +} + +input Duplicate { + propA: EnumCommentTestCase! + propB: [EnumCommentTestCase!]! +} + union UnionTestCase = PartialA | PartialB type Query { @@ -68,4 +79,9 @@ type Query { interfaceTest: InterfaceTestCase unionTest: UnionTestCase scalarTest: Boolean! + nesting: Nesting! +} + +type Mutation { + duplicates(a: EnumCommentTestCase!, b: EnumCommentTestCase!, c: Duplicate!): Nesting! } diff --git a/packages/common-test/fixtures/misc/schema.json b/packages/common-test/fixtures/misc/schema.json index 1ae69710d8..bd970a82f2 100644 --- a/packages/common-test/fixtures/misc/schema.json +++ b/packages/common-test/fixtures/misc/schema.json @@ -4,7 +4,9 @@ "queryType": { "name": "Query" }, - "mutationType": null, + "mutationType": { + "name": "Mutation" + }, "subscriptionType": null, "types": [{ "kind": "OBJECT", @@ -69,6 +71,21 @@ }, "isDeprecated": false, "deprecationReason": null + }, { + "name": "nesting", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "Nesting", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null }], "inputFields": null, "interfaces": [], @@ -321,6 +338,171 @@ "interfaces": null, "enumValues": null, "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "Nesting", + "description": null, + "fields": [{ + "name": "propA", + "description": null, + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + } + } + } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, { + "name": "propB", + "description": null, + "args": [], + "type": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "OBJECT", + "name": "Mutation", + "description": null, + "fields": [{ + "name": "duplicates", + "description": null, + "args": [{ + "name": "a", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + } + }, + "defaultValue": null + }, { + "name": "b", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + } + }, + "defaultValue": null + }, { + "name": "c", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "INPUT_OBJECT", + "name": "Duplicate", + "ofType": null + } + }, + "defaultValue": null + }], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "OBJECT", + "name": "Nesting", + "ofType": null + } + }, + "isDeprecated": false, + "deprecationReason": null + }], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { + "kind": "INPUT_OBJECT", + "name": "Duplicate", + "description": null, + "fields": null, + "inputFields": [{ + "name": "propA", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + } + }, + "defaultValue": null + }, { + "name": "propB", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "ENUM", + "name": "EnumCommentTestCase", + "ofType": null + } + } + } + }, + "defaultValue": null + }], + "interfaces": null, + "enumValues": null, + "possibleTypes": null }, { "kind": "OBJECT", "name": "__Schema",