apollo-codegen-typescript
Advanced tools
Comparing version
@@ -1,7 +0,7 @@ | ||
import { GraphQLEnumType, GraphQLInputObjectType } from 'graphql'; | ||
import { CompilerContext, Operation, Fragment } from 'apollo-codegen-core/lib/compiler'; | ||
import { BasicGeneratedFile } from 'apollo-codegen-core/lib/utilities/CodeGenerator'; | ||
import TypescriptGenerator from './language'; | ||
import Printer from './printer'; | ||
import { GraphQLType } from 'graphql/type/definition'; | ||
import { GraphQLEnumType, GraphQLInputObjectType } from "graphql"; | ||
import { CompilerContext, Operation, Fragment } from "apollo-codegen-core/lib/compiler"; | ||
import { BasicGeneratedFile } from "apollo-codegen-core/lib/utilities/CodeGenerator"; | ||
import TypescriptGenerator from "./language"; | ||
import Printer from "./printer"; | ||
import { GraphQLType } from "graphql/type/definition"; | ||
declare class TypescriptGeneratedFile implements BasicGeneratedFile { | ||
@@ -8,0 +8,0 @@ fileContents: string; |
@@ -28,4 +28,5 @@ "use strict"; | ||
typesUsed | ||
.filter(type => (type instanceof graphql_1.GraphQLEnumType)) | ||
.forEach((enumType) => { | ||
.filter(type => type instanceof graphql_1.GraphQLEnumType) | ||
.sort() | ||
.forEach(enumType => { | ||
generator.typeAliasForEnumType(enumType); | ||
@@ -35,3 +36,4 @@ }); | ||
.filter(type => type instanceof graphql_1.GraphQLInputObjectType) | ||
.forEach((inputObjectType) => { | ||
.sort() | ||
.forEach(inputObjectType => { | ||
generator.typeAliasForInputObjectType(inputObjectType); | ||
@@ -47,4 +49,4 @@ }); | ||
if (typesUsed.length > 0) { | ||
const relative = path.relative(path.dirname(outputPath), path.join(path.dirname(globalSourcePath), path.basename(globalSourcePath, '.ts'))); | ||
generator.printer.enqueue(generator.import(typesUsed, relative)); | ||
const relative = path.relative(path.dirname(outputPath), path.join(path.dirname(globalSourcePath), path.basename(globalSourcePath, ".ts"))); | ||
generator.printer.enqueue(generator.import(typesUsed, "./" + relative)); | ||
} | ||
@@ -55,4 +57,3 @@ } | ||
const generatedFiles = []; | ||
Object.values(context.operations) | ||
.forEach((operation) => { | ||
Object.values(context.operations).forEach(operation => { | ||
generator.fileHeader(); | ||
@@ -67,4 +68,3 @@ generator.interfacesForOperation(operation); | ||
}); | ||
Object.values(context.fragments) | ||
.forEach((fragment) => { | ||
Object.values(context.fragments).forEach(fragment => { | ||
generator.fileHeader(); | ||
@@ -90,4 +90,3 @@ generator.interfacesForFragment(fragment); | ||
const generator = new TypescriptAPIGenerator(context); | ||
const operations = Object.values(context.operations) | ||
.map((operation) => ({ | ||
const operations = Object.values(context.operations).map(operation => ({ | ||
sourcePath: operation.filePath, | ||
@@ -103,6 +102,5 @@ fileName: `${operation.operationName}.ts`, | ||
return new TypescriptGeneratedFile(output); | ||
}, | ||
} | ||
})); | ||
const fragments = Object.values(context.fragments) | ||
.map((fragment) => ({ | ||
const fragments = Object.values(context.fragments).map(fragment => ({ | ||
sourcePath: fragment.filePath, | ||
@@ -118,3 +116,3 @@ fileName: `${fragment.fragmentName}.ts`, | ||
return new TypescriptGeneratedFile(output); | ||
}, | ||
} | ||
})); | ||
@@ -136,4 +134,3 @@ return operations.concat(fragments); | ||
this.getGlobalTypesUsedForOperation = (doc) => { | ||
const typesUsed = doc.variables | ||
.reduce((acc, { type }) => { | ||
const typesUsed = doc.variables.reduce((acc, { type }) => { | ||
const t = this.getUnderlyingType(type); | ||
@@ -151,3 +148,3 @@ if (this.isGlobalType(t)) { | ||
this.reduceSelection = (acc, selection) => { | ||
if (selection.kind === 'Field' || selection.kind === 'TypeCondition') { | ||
if (selection.kind === "Field" || selection.kind === "TypeCondition") { | ||
const type = this.getUnderlyingType(selection.type); | ||
@@ -164,3 +161,3 @@ if (this.isGlobalType(type)) { | ||
this.isGlobalType = (type) => { | ||
return type instanceof graphql_1.GraphQLEnumType || type instanceof graphql_1.GraphQLInputObjectType; | ||
return (type instanceof graphql_1.GraphQLEnumType || type instanceof graphql_1.GraphQLInputObjectType); | ||
}; | ||
@@ -183,10 +180,9 @@ this.getUnderlyingType = (type) => { | ||
} | ||
if (type instanceof graphql_1.GraphQLInputObjectType | ||
|| type instanceof graphql_2.GraphQLObjectType) { | ||
if (type instanceof graphql_1.GraphQLInputObjectType || | ||
type instanceof graphql_2.GraphQLObjectType) { | ||
acc = array_1.maybePush(acc, type); | ||
const fields = type.getFields(); | ||
acc = Object.keys(fields) | ||
.map((key) => fields[key] && fields[key].type) | ||
.map(key => fields[key] && fields[key].type) | ||
.reduce(this.reduceTypesUsed, acc); | ||
; | ||
} | ||
@@ -229,5 +225,5 @@ else { | ||
if (variables.length > 0) { | ||
const interfaceName = operationName + 'Variables'; | ||
const interfaceName = operationName + "Variables"; | ||
this.scopeStackPush(interfaceName); | ||
this.printer.enqueue(this.exportDeclaration(this.interface(interfaceName, variables.map((variable) => ({ | ||
this.printer.enqueue(this.exportDeclaration(this.interface(interfaceName, variables.map(variable => ({ | ||
name: variable.name, | ||
@@ -265,3 +261,3 @@ type: this.typeFromGraphQLType(variable.type) | ||
}); | ||
this.printer.enqueue(this.exportDeclaration(this.typeAliasGenericUnion(this.nameFromScopeStack(this.scopeStack), unionMembers.map((id) => t.TSTypeReference(id))))); | ||
this.printer.enqueue(this.exportDeclaration(this.typeAliasGenericUnion(this.nameFromScopeStack(this.scopeStack), unionMembers.map(id => t.TSTypeReference(id))))); | ||
} | ||
@@ -272,3 +268,3 @@ this.scopeStackPop(); | ||
let docTypesUsed = []; | ||
if (doc.hasOwnProperty('operationName')) { | ||
if (doc.hasOwnProperty("operationName")) { | ||
const operation = doc; | ||
@@ -278,12 +274,11 @@ docTypesUsed = operation.variables.map(({ type }) => type); | ||
const reduceTypesForDocument = (nestDoc, acc) => { | ||
const { selectionSet: { possibleTypes, selections, }, } = nestDoc; | ||
const { selectionSet: { possibleTypes, selections } } = nestDoc; | ||
acc = possibleTypes.reduce(array_1.maybePush, acc); | ||
acc = selections | ||
.reduce((selectionAcc, selection) => { | ||
acc = selections.reduce((selectionAcc, selection) => { | ||
switch (selection.kind) { | ||
case 'Field': | ||
case 'TypeCondition': | ||
case "Field": | ||
case "TypeCondition": | ||
selectionAcc = array_1.maybePush(selectionAcc, selection.type); | ||
break; | ||
case 'FragmentSpread': | ||
case "FragmentSpread": | ||
selectionAcc = reduceTypesForDocument(selection, selectionAcc); | ||
@@ -298,7 +293,5 @@ break; | ||
}; | ||
docTypesUsed = reduceTypesForDocument(doc, docTypesUsed) | ||
.reduce(this.reduceTypesUsed, []); | ||
return context.typesUsed | ||
.filter((type) => { | ||
return docTypesUsed.find((typeUsed) => type === typeUsed); | ||
docTypesUsed = reduceTypesForDocument(doc, docTypesUsed).reduce(this.reduceTypesUsed, []); | ||
return context.typesUsed.filter(type => { | ||
return docTypesUsed.find(typeUsed => type === typeUsed); | ||
}); | ||
@@ -359,5 +352,4 @@ } | ||
let res; | ||
if (field.name === '__typename') { | ||
const types = variant.possibleTypes | ||
.map(type => { | ||
if (field.name === "__typename") { | ||
const types = variant.possibleTypes.map(type => { | ||
return t.TSLiteralType(t.stringLiteral(type.toString())); | ||
@@ -364,0 +356,0 @@ }); |
{ | ||
"name": "apollo-codegen-typescript", | ||
"description": "TypeScript generator module for Apollo Codegen", | ||
"version": "0.27.0", | ||
"version": "0.27.1", | ||
"main": "./lib/index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
import * as path from "path"; | ||
import * as t from '@babel/types'; | ||
import { stripIndent } from 'common-tags'; | ||
import { | ||
GraphQLEnumType, | ||
GraphQLInputObjectType, | ||
} from 'graphql'; | ||
import * as t from "@babel/types"; | ||
import { stripIndent } from "common-tags"; | ||
import { GraphQLEnumType, GraphQLInputObjectType } from "graphql"; | ||
@@ -17,3 +14,3 @@ import { | ||
FragmentSpread | ||
} from 'apollo-codegen-core/lib/compiler'; | ||
} from "apollo-codegen-core/lib/compiler"; | ||
@@ -23,15 +20,22 @@ import { | ||
Variant | ||
} from 'apollo-codegen-core/lib/compiler/visitors/typeCase'; | ||
} from "apollo-codegen-core/lib/compiler/visitors/typeCase"; | ||
import { collectAndMergeFields } from "apollo-codegen-core/lib/compiler/visitors/collectAndMergeFields"; | ||
import { BasicGeneratedFile } from "apollo-codegen-core/lib/utilities/CodeGenerator"; | ||
import TypescriptGenerator, { | ||
ObjectProperty, | ||
TypescriptCompilerOptions | ||
} from "./language"; | ||
import Printer from "./printer"; | ||
import { GraphQLType } from "graphql/type/definition"; | ||
import { | ||
collectAndMergeFields | ||
} from 'apollo-codegen-core/lib/compiler/visitors/collectAndMergeFields'; | ||
GraphQLNonNull, | ||
GraphQLOutputType, | ||
getNullableType, | ||
GraphQLList, | ||
GraphQLObjectType | ||
} from "graphql"; | ||
import { maybePush } from "apollo-codegen-core/lib/utilities/array"; | ||
import { BasicGeneratedFile } from 'apollo-codegen-core/lib/utilities/CodeGenerator'; | ||
import TypescriptGenerator, { ObjectProperty, TypescriptCompilerOptions, } from './language'; | ||
import Printer from './printer'; | ||
import { GraphQLType } from 'graphql/type/definition'; | ||
import { GraphQLNonNull, GraphQLOutputType, getNullableType, GraphQLList, GraphQLObjectType } from 'graphql'; | ||
import { maybePush } from 'apollo-codegen-core/lib/utilities/array'; | ||
class TypescriptGeneratedFile implements BasicGeneratedFile { | ||
@@ -44,7 +48,10 @@ fileContents: string; | ||
get output() { | ||
return this.fileContents | ||
return this.fileContents; | ||
} | ||
} | ||
function printEnumsAndInputObjects(generator: TypescriptAPIGenerator, typesUsed: GraphQLType[]) { | ||
function printEnumsAndInputObjects( | ||
generator: TypescriptAPIGenerator, | ||
typesUsed: GraphQLType[] | ||
) { | ||
generator.printer.enqueue(stripIndent` | ||
@@ -57,4 +64,5 @@ //============================================================== | ||
typesUsed | ||
.filter(type => (type instanceof GraphQLEnumType)) | ||
.forEach((enumType) => { | ||
.filter(type => type instanceof GraphQLEnumType) | ||
.sort() | ||
.forEach(enumType => { | ||
generator.typeAliasForEnumType(enumType as GraphQLEnumType); | ||
@@ -65,4 +73,7 @@ }); | ||
.filter(type => type instanceof GraphQLInputObjectType) | ||
.forEach((inputObjectType) => { | ||
generator.typeAliasForInputObjectType(inputObjectType as GraphQLInputObjectType); | ||
.sort() | ||
.forEach(inputObjectType => { | ||
generator.typeAliasForInputObjectType( | ||
inputObjectType as GraphQLInputObjectType | ||
); | ||
}); | ||
@@ -74,3 +85,3 @@ | ||
//============================================================== | ||
`) | ||
`); | ||
} | ||
@@ -82,3 +93,3 @@ | ||
outputPath: string, | ||
globalSourcePath: string, | ||
globalSourcePath: string | ||
) { | ||
@@ -90,6 +101,6 @@ if (typesUsed.length > 0) { | ||
path.dirname(globalSourcePath), | ||
path.basename(globalSourcePath, '.ts') | ||
path.basename(globalSourcePath, ".ts") | ||
) | ||
); | ||
generator.printer.enqueue(generator.import(typesUsed, relative)); | ||
generator.printer.enqueue(generator.import(typesUsed, "./" + relative)); | ||
} | ||
@@ -99,35 +110,35 @@ } | ||
// TODO: deprecate this, use generateLocalSource and generateGlobalSource instead. | ||
export function generateSource( | ||
context: CompilerContext, | ||
) { | ||
export function generateSource(context: CompilerContext) { | ||
const generator = new TypescriptAPIGenerator(context); | ||
const generatedFiles: { sourcePath: string, fileName: string, content: TypescriptGeneratedFile }[] = []; | ||
const generatedFiles: { | ||
sourcePath: string; | ||
fileName: string; | ||
content: TypescriptGeneratedFile; | ||
}[] = []; | ||
Object.values(context.operations) | ||
.forEach((operation) => { | ||
generator.fileHeader(); | ||
generator.interfacesForOperation(operation); | ||
Object.values(context.operations).forEach(operation => { | ||
generator.fileHeader(); | ||
generator.interfacesForOperation(operation); | ||
const output = generator.printer.printAndClear(); | ||
const output = generator.printer.printAndClear(); | ||
generatedFiles.push({ | ||
sourcePath: operation.filePath, | ||
fileName: `${operation.operationName}.ts`, | ||
content: new TypescriptGeneratedFile(output) | ||
}); | ||
generatedFiles.push({ | ||
sourcePath: operation.filePath, | ||
fileName: `${operation.operationName}.ts`, | ||
content: new TypescriptGeneratedFile(output) | ||
}); | ||
}); | ||
Object.values(context.fragments) | ||
.forEach((fragment) => { | ||
generator.fileHeader(); | ||
generator.interfacesForFragment(fragment); | ||
Object.values(context.fragments).forEach(fragment => { | ||
generator.fileHeader(); | ||
generator.interfacesForFragment(fragment); | ||
const output = generator.printer.printAndClear(); | ||
const output = generator.printer.printAndClear(); | ||
generatedFiles.push({ | ||
sourcePath: fragment.filePath, | ||
fileName: `${fragment.fragmentName}.ts`, | ||
content: new TypescriptGeneratedFile(output) | ||
}); | ||
generatedFiles.push({ | ||
sourcePath: fragment.filePath, | ||
fileName: `${fragment.fragmentName}.ts`, | ||
content: new TypescriptGeneratedFile(output) | ||
}); | ||
}); | ||
@@ -156,45 +167,43 @@ generator.fileHeader(); | ||
export function generateLocalSource( | ||
context: CompilerContext, | ||
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.getGlobalTypesUsedForOperation(operation), | ||
options.outputPath, | ||
options.globalSourcePath | ||
); | ||
} | ||
generator.interfacesForOperation(operation); | ||
const output = generator.printer.printAndClear(); | ||
return new TypescriptGeneratedFile(output); | ||
}, | ||
})); | ||
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.getGlobalTypesUsedForOperation(operation), | ||
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.getGlobalTypesUsedForFragment(fragment), | ||
options.outputPath, | ||
options.globalSourcePath | ||
); | ||
} | ||
generator.interfacesForFragment(fragment); | ||
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.getGlobalTypesUsedForFragment(fragment), | ||
options.outputPath, | ||
options.globalSourcePath | ||
); | ||
} | ||
generator.interfacesForFragment(fragment); | ||
const output = generator.printer.printAndClear(); | ||
return new TypescriptGeneratedFile(output); | ||
} | ||
})); | ||
@@ -205,3 +214,3 @@ return operations.concat(fragments); | ||
export function generateGlobalSource( | ||
context: CompilerContext, | ||
context: CompilerContext | ||
): TypescriptGeneratedFile { | ||
@@ -216,5 +225,5 @@ const generator = new TypescriptAPIGenerator(context); | ||
export class TypescriptAPIGenerator extends TypescriptGenerator { | ||
context: CompilerContext | ||
printer: Printer | ||
scopeStack: string[] | ||
context: CompilerContext; | ||
printer: Printer; | ||
scopeStack: string[]; | ||
@@ -247,8 +256,3 @@ constructor(context: CompilerContext) { | ||
public interfacesForOperation(operation: Operation) { | ||
const { | ||
operationType, | ||
operationName, | ||
variables, | ||
selectionSet | ||
} = operation; | ||
const { operationType, operationName, variables, selectionSet } = operation; | ||
@@ -261,3 +265,3 @@ this.scopeStackPush(operationName); | ||
// ==================================================== | ||
`) | ||
`); | ||
@@ -280,10 +284,16 @@ // The root operation only has one variant | ||
if (variables.length > 0) { | ||
const interfaceName = operationName + 'Variables'; | ||
const interfaceName = operationName + "Variables"; | ||
this.scopeStackPush(interfaceName); | ||
this.printer.enqueue(this.exportDeclaration( | ||
this.interface(interfaceName, variables.map((variable) => ({ | ||
name: variable.name, | ||
type: this.typeFromGraphQLType(variable.type) | ||
})), { keyInheritsNullability: true }) | ||
)); | ||
this.printer.enqueue( | ||
this.exportDeclaration( | ||
this.interface( | ||
interfaceName, | ||
variables.map(variable => ({ | ||
name: variable.name, | ||
type: this.typeFromGraphQLType(variable.type) | ||
})), | ||
{ keyInheritsNullability: true } | ||
) | ||
) | ||
); | ||
this.scopeStackPop(); | ||
@@ -294,6 +304,3 @@ } | ||
public interfacesForFragment(fragment: Fragment) { | ||
const { | ||
fragmentName, | ||
selectionSet | ||
} = fragment; | ||
const { fragmentName, selectionSet } = fragment; | ||
this.scopeStackPush(fragmentName); | ||
@@ -314,6 +321,3 @@ | ||
const exportedTypeAlias = this.exportDeclaration( | ||
this.interface( | ||
name, | ||
properties | ||
) | ||
this.interface(name, properties) | ||
); | ||
@@ -330,6 +334,3 @@ | ||
const exportedTypeAlias = this.exportDeclaration( | ||
this.interface( | ||
name, | ||
properties | ||
) | ||
this.interface(name, properties) | ||
); | ||
@@ -339,3 +340,5 @@ | ||
unionMembers.push(t.identifier(this.nameFromScopeStack(this.scopeStack))); | ||
unionMembers.push( | ||
t.identifier(this.nameFromScopeStack(this.scopeStack)) | ||
); | ||
@@ -349,3 +352,3 @@ this.scopeStackPop(); | ||
this.nameFromScopeStack(this.scopeStack), | ||
unionMembers.map((id) => t.TSTypeReference(id)) | ||
unionMembers.map(id => t.TSTypeReference(id)) | ||
) | ||
@@ -360,4 +363,4 @@ ) | ||
public getGlobalTypesUsedForOperation = (doc: Operation) => { | ||
const typesUsed = doc.variables | ||
.reduce((acc: GraphQLType[], { type } : { type : GraphQLType }) => { | ||
const typesUsed = doc.variables.reduce( | ||
(acc: GraphQLType[], { type }: { type: GraphQLType }) => { | ||
const t = this.getUnderlyingType(type); | ||
@@ -368,12 +371,17 @@ if (this.isGlobalType(t)) { | ||
return acc; | ||
}, []); | ||
}, | ||
[] | ||
); | ||
return doc.selectionSet.selections.reduce(this.reduceSelection, typesUsed); | ||
} | ||
}; | ||
public getGlobalTypesUsedForFragment = (doc: Fragment) => { | ||
return doc.selectionSet.selections.reduce(this.reduceSelection, []); | ||
} | ||
}; | ||
private reduceSelection = (acc: GraphQLType[], selection: Selection): GraphQLType[] => { | ||
if (selection.kind === 'Field' || selection.kind === 'TypeCondition') { | ||
private reduceSelection = ( | ||
acc: GraphQLType[], | ||
selection: Selection | ||
): GraphQLType[] => { | ||
if (selection.kind === "Field" || selection.kind === "TypeCondition") { | ||
const type = this.getUnderlyingType(selection.type); | ||
@@ -386,11 +394,16 @@ if (this.isGlobalType(type)) { | ||
if (selection.selectionSet) { | ||
return selection.selectionSet.selections.reduce(this.reduceSelection, acc); | ||
return selection.selectionSet.selections.reduce( | ||
this.reduceSelection, | ||
acc | ||
); | ||
} | ||
return acc; | ||
} | ||
}; | ||
private isGlobalType = (type: GraphQLType) => { | ||
return type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType; | ||
} | ||
return ( | ||
type instanceof GraphQLEnumType || type instanceof GraphQLInputObjectType | ||
); | ||
}; | ||
@@ -405,8 +418,11 @@ private getUnderlyingType = (type: GraphQLType): GraphQLType => { | ||
return type; | ||
} | ||
}; | ||
public getTypesUsedForOperation(doc: Operation | Fragment, context: CompilerContext) { | ||
public getTypesUsedForOperation( | ||
doc: Operation | Fragment, | ||
context: CompilerContext | ||
) { | ||
let docTypesUsed: GraphQLType[] = []; | ||
if (doc.hasOwnProperty('operationName')) { | ||
if (doc.hasOwnProperty("operationName")) { | ||
const operation = doc as Operation; | ||
@@ -421,6 +437,3 @@ docTypesUsed = operation.variables.map(({ type }) => type); | ||
const { | ||
selectionSet: { | ||
possibleTypes, | ||
selections, | ||
}, | ||
selectionSet: { possibleTypes, selections } | ||
} = nestDoc; | ||
@@ -430,29 +443,29 @@ | ||
acc = selections | ||
.reduce((selectionAcc, selection) => { | ||
switch (selection.kind) { | ||
case 'Field': | ||
case 'TypeCondition': | ||
selectionAcc = maybePush(selectionAcc, selection.type); | ||
break; | ||
case 'FragmentSpread': | ||
selectionAcc = reduceTypesForDocument(selection, selectionAcc); | ||
break; | ||
default: | ||
break; | ||
} | ||
acc = selections.reduce((selectionAcc, selection) => { | ||
switch (selection.kind) { | ||
case "Field": | ||
case "TypeCondition": | ||
selectionAcc = maybePush(selectionAcc, selection.type); | ||
break; | ||
case "FragmentSpread": | ||
selectionAcc = reduceTypesForDocument(selection, selectionAcc); | ||
break; | ||
default: | ||
break; | ||
} | ||
return selectionAcc; | ||
}, acc); | ||
return selectionAcc; | ||
}, acc); | ||
return acc; | ||
} | ||
}; | ||
docTypesUsed = reduceTypesForDocument(doc, docTypesUsed) | ||
.reduce(this.reduceTypesUsed, []); | ||
docTypesUsed = reduceTypesForDocument(doc, docTypesUsed).reduce( | ||
this.reduceTypesUsed, | ||
[] | ||
); | ||
return context.typesUsed | ||
.filter((type) => { | ||
return docTypesUsed.find((typeUsed) => type === typeUsed); | ||
}); | ||
return context.typesUsed.filter(type => { | ||
return docTypesUsed.find(typeUsed => type === typeUsed); | ||
}); | ||
} | ||
@@ -463,3 +476,3 @@ | ||
type: GraphQLType | ||
) =>{ | ||
) => { | ||
if (type instanceof GraphQLNonNull) { | ||
@@ -470,8 +483,8 @@ type = getNullableType(type); | ||
if (type instanceof GraphQLList) { | ||
type = type.ofType | ||
type = type.ofType; | ||
} | ||
if ( | ||
type instanceof GraphQLInputObjectType | ||
|| type instanceof GraphQLObjectType | ||
type instanceof GraphQLInputObjectType || | ||
type instanceof GraphQLObjectType | ||
) { | ||
@@ -481,5 +494,4 @@ acc = maybePush(acc, type); | ||
acc = Object.keys(fields) | ||
.map((key) => fields[key] && fields[key].type) | ||
.map(key => fields[key] && fields[key].type) | ||
.reduce(this.reduceTypesUsed, acc); | ||
; | ||
} else { | ||
@@ -490,3 +502,3 @@ acc = maybePush(acc, type); | ||
return acc; | ||
} | ||
}; | ||
@@ -520,6 +532,3 @@ private getVariantsForSelectionSet(selectionSet: SelectionSet) { | ||
} else { | ||
res = this.handleFieldValue( | ||
field, | ||
variant | ||
); | ||
res = this.handleFieldValue(field, variant); | ||
} | ||
@@ -532,3 +541,6 @@ | ||
private handleFieldSelectionSetValue(generatedIdentifier: t.Identifier, field: Field): ObjectProperty { | ||
private handleFieldSelectionSetValue( | ||
generatedIdentifier: t.Identifier, | ||
field: Field | ||
): ObjectProperty { | ||
const { selectionSet } = field; | ||
@@ -538,3 +550,5 @@ | ||
const typeCase = this.getTypeCasesForSelectionSet(selectionSet as SelectionSet); | ||
const typeCase = this.getTypeCasesForSelectionSet( | ||
selectionSet as SelectionSet | ||
); | ||
const variants = typeCase.exhaustiveVariants; | ||
@@ -547,19 +561,13 @@ | ||
exportedTypeAlias = this.exportDeclaration( | ||
this.interface( | ||
this.nameFromScopeStack(this.scopeStack), | ||
properties | ||
) | ||
this.interface(this.nameFromScopeStack(this.scopeStack), properties) | ||
); | ||
} else { | ||
const identifiers = variants.map(variant => { | ||
this.scopeStackPush(variant.possibleTypes[0].toString()) | ||
this.scopeStackPush(variant.possibleTypes[0].toString()); | ||
const properties = this.getPropertiesForVariant(variant); | ||
const identifierName = this.nameFromScopeStack(this.scopeStack); | ||
this.printer.enqueue(this.exportDeclaration( | ||
this.interface( | ||
identifierName, | ||
properties | ||
) | ||
)); | ||
this.printer.enqueue( | ||
this.exportDeclaration(this.interface(identifierName, properties)) | ||
); | ||
@@ -589,7 +597,6 @@ this.scopeStackPop(); | ||
let res: ObjectProperty; | ||
if (field.name === '__typename') { | ||
const types = variant.possibleTypes | ||
.map(type => { | ||
return t.TSLiteralType(t.stringLiteral(type.toString())); | ||
}); | ||
if (field.name === "__typename") { | ||
const types = variant.possibleTypes.map(type => { | ||
return t.TSLiteralType(t.stringLiteral(type.toString())); | ||
}); | ||
@@ -622,6 +629,5 @@ res = { | ||
scopeStackPop() { | ||
const popped = this.scopeStack.pop() | ||
const popped = this.scopeStack.pop(); | ||
return popped; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
156954
-0.18%2491
-0.04%