From dd737b6379fe39b271d2eb484eba3c66de27d316 Mon Sep 17 00:00:00 2001 From: yorkyao Date: Wed, 21 Nov 2018 18:42:32 +0800 Subject: [PATCH] fix: CI failure; simplify graphql root type --- demo/root-type.ts | 376 ++++++++++++++--------------- src/graphql-root-type-generator.ts | 16 +- 2 files changed, 198 insertions(+), 194 deletions(-) diff --git a/demo/root-type.ts b/demo/root-type.ts index 95ea5c0..3099720 100644 --- a/demo/root-type.ts +++ b/demo/root-type.ts @@ -30,274 +30,274 @@ export interface Root { users(input: {}, context: TContext, info: GraphQLResolveInfo): DeepPromisifyReturnType> | Promise>> } -export interface TypeLiteral { - typeLiteralMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - typeLiteralMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeLiteral { + typeLiteralMember1: number + typeLiteralMember2: string } -export interface Interface { - interfaceMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - interfaceMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface Interface { + interfaceMember1: number + interfaceMember2: string } -export interface TypeUnion1 { - typeLiteralMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - typeLiteralMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - typeUnionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeUnion1 { + typeLiteralMember1: number + typeLiteralMember2: string + typeUnionMember1: number + typeUnionMember2: string } -export interface TypeUnion2 { - kind(input: {}, context: TContext, info: GraphQLResolveInfo): StringEnum | Promise - typeUnionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeUnion2 { + kind: StringEnum + typeUnionMember1: string + typeUnionMember2: string } -export interface TypeUnion3 { - kind(input: {}, context: TContext, info: GraphQLResolveInfo): NumberEnum | Promise - typeUnionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeUnion3 { + kind: NumberEnum + typeUnionMember1: string + typeUnionMember2: string } -export interface TypeUnion4 { - kind(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeUnion4 { + kind: string + typeUnionMember1: string + typeUnionMember2: string } -export type TypeUnion5 = TypeLiteral | Interface +export type TypeUnion5 = TypeLiteral | Interface -export type TypeUnion8 = string | string | null | boolean +export type TypeUnion8 = string | string | null | boolean -export interface TypeUnion { - typeUnionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion1 | Promise> - typeUnionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion2 | Promise> - typeUnionMember3(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion3 | Promise> - typeUnionMember4(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion4 | Promise> - typeUnionMember5(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion5 | Promise> - typeUnionMember6(input: {}, context: TContext, info: GraphQLResolveInfo): string | null | boolean | Promise - typeUnionMember7(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember8(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion8 | Promise> - typeUnionMember9(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion9 | Promise +export interface TypeUnion { + typeUnionMember1: TypeUnion1 + typeUnionMember2: TypeUnion2 + typeUnionMember3: TypeUnion3 + typeUnionMember4: TypeUnion4 + typeUnionMember5: TypeUnion5 + typeUnionMember6: string | null | boolean + typeUnionMember7: string + typeUnionMember8: TypeUnion8 + typeUnionMember9: TypeUnion9 } -export interface InterfaceExtends { - interfaceExtendsMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - interfaceExtendsMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - interfaceMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - interfaceMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface InterfaceExtends { + interfaceExtendsMember1: number + interfaceExtendsMember2: string + interfaceMember1: number + interfaceMember2: string } -export interface TypeIntersection1 { - interfaceMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - interfaceMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeIntersectionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - typeIntersectionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeIntersection1 { + interfaceMember1: number + interfaceMember2: string + typeIntersectionMember1: number + typeIntersectionMember2: string } -export interface TypeIntersection2 { - typeIntersectionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - typeIntersectionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeIntersectionMember3(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - typeIntersectionMember4(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeIntersection2 { + typeIntersectionMember1: number + typeIntersectionMember2: string + typeIntersectionMember3: number + typeIntersectionMember4: string } -export interface TypeIntersection { - typeIntersectionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): TypeIntersection1 | Promise> - typeIntersectionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): TypeIntersection2 | Promise> - typeIntersectionMember3(input: {}, context: TContext, info: GraphQLResolveInfo): any | Promise +export interface TypeIntersection { + typeIntersectionMember1: TypeIntersection1 + typeIntersectionMember2: TypeIntersection2 + typeIntersectionMember3: any } -export interface TypeUnionAndIntersection { - typeIntersectionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - kind(input: {}, context: TContext, info: GraphQLResolveInfo): NumberEnum | Promise - typeUnionMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - typeUnionMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TypeUnionAndIntersection { + typeIntersectionMember1: number + kind: NumberEnum + typeUnionMember1: string + typeUnionMember2: string } -export interface TaggedField { - taggedFieldMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - taggedFieldMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface TaggedField { + taggedFieldMember1: number + taggedFieldMember2: string } -export interface Enum { - stringEnum(input: {}, context: TContext, info: GraphQLResolveInfo): StringEnum | Promise - numberEnum(input: {}, context: TContext, info: GraphQLResolveInfo): NumberEnum | Promise - numberEnum2(input: {}, context: TContext, info: GraphQLResolveInfo): NumberEnum2 | Promise - stringEnum2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface Enum { + stringEnum: StringEnum + numberEnum: NumberEnum + numberEnum2: NumberEnum2 + stringEnum2: string } -export interface NumberType { - numberMember(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - integerMember(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - uint32Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - int32Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - sint32Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - fixed32Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - sfixed32Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - uint64Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - int64Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - sint64Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - fixed64Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - sfixed64Member(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - floatMember(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - doubleMember(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - titleMember(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise +export interface NumberType { + numberMember: number + integerMember: number + uint32Member: number + int32Member: number + sint32Member: number + fixed32Member: number + sfixed32Member: number + uint64Member: number + int64Member: number + sint64Member: number + fixed64Member: number + sfixed64Member: number + floatMember: number + doubleMember: number + titleMember: number } -export interface StringType { - stringMember(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface StringType { + stringMember: string } -export interface ArrayType { - arrayType1(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - arrayType2(input: {}, context: TContext, info: GraphQLResolveInfo): Array> | Promise>> - arrayType3(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - arrayType4(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - arrayType5(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - arrayType6(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - arrayType7(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - arrayType8(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> +export interface ArrayType { + arrayType1: Array + arrayType2: Array> + arrayType3: Array + arrayType4: Array + arrayType5: Array + arrayType6: Array + arrayType7: Array + arrayType8: Array } -export interface MapType7 { - foo(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface MapType7 { + foo: string } -export interface MapType8 { +export interface MapType8 { } -export interface MapType { - mapType(input: {}, context: TContext, info: GraphQLResolveInfo): { [name: string]: number } | Promise<{ [name: string]: number }> - mapType2(input: {}, context: TContext, info: GraphQLResolveInfo): { [name: string]: TypeLiteral } | Promise<{ [name: string]: TypeLiteral }> - mapType3(input: {}, context: TContext, info: GraphQLResolveInfo): { [name: string]: object } | Promise<{ [name: string]: object }> - mapType4(input: {}, context: TContext, info: GraphQLResolveInfo): { [name: string]: number } | Promise<{ [name: string]: number }> - mapType5(input: {}, context: TContext, info: GraphQLResolveInfo): { [name: string]: any } | Promise<{ [name: string]: any }> - mapType6(input: {}, context: TContext, info: GraphQLResolveInfo): object | Promise - mapType7(input: {}, context: TContext, info: GraphQLResolveInfo): MapType7 | Promise> - mapType8(input: {}, context: TContext, info: GraphQLResolveInfo): MapType8 | Promise> +export interface MapType { + mapType: { [name: string]: number } + mapType2: { [name: string]: TypeLiteral } + mapType3: { [name: string]: object } + mapType4: { [name: string]: number } + mapType5: { [name: string]: any } + mapType6: object + mapType7: MapType7 + mapType8: MapType8 } -export interface Parameter { +export interface Parameter { member1(input: { name: string, age: number }, context: TContext, info: GraphQLResolveInfo): string | Promise member2(input: { name?: string }, context: TContext, info: GraphQLResolveInfo): string | Promise } -export interface DefaultValue { - stringMember(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - numberMember(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - booleanMember(input: {}, context: TContext, info: GraphQLResolveInfo): boolean | Promise - stringMember2(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - stringMember3(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - arrayMember(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - objectMember(input: {}, context: TContext, info: GraphQLResolveInfo): object | Promise - numberMember1(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - objectMember2(input: {}, context: TContext, info: GraphQLResolveInfo): TypeLiteral | Promise> +export interface DefaultValue { + stringMember: string + numberMember: number + booleanMember: boolean + stringMember2: string + stringMember3: string + arrayMember: Array + objectMember: object + numberMember1: number + objectMember2: TypeLiteral } -export type TypeReferenceMember2 = TypeLiteral +export type TypeReferenceMember2 = TypeLiteral -export interface ReferenceType { - typeReferenceMember1(input: {}, context: TContext, info: GraphQLResolveInfo): TypeLiteral | Promise> - typeReferenceMember2(input: {}, context: TContext, info: GraphQLResolveInfo): TypeReferenceMember2 | Promise> +export interface ReferenceType { + typeReferenceMember1: TypeLiteral + typeReferenceMember2: TypeReferenceMember2 } -export interface ClassType1 { - classMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - classMember2(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise +export interface ClassType1 { + classMember1: string + classMember2: number } -export interface ClassType2 { - classMember3(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - classMember4(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - classMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - classMember2(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise +export interface ClassType2 { + classMember3: string + classMember4: number + classMember1: string + classMember2: number } -export interface ClassType3 { - classMember1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - classMember2(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - classMember3(input: {}, context: TContext, info: GraphQLResolveInfo): boolean | Promise - classMember4(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - classMember5(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - classMember6(input: {}, context: TContext, info: GraphQLResolveInfo): object | Promise +export interface ClassType3 { + classMember1: string + classMember2: number + classMember3: boolean + classMember4: string + classMember5: Array + classMember6: object } -export interface ClassType { - classType1(input: {}, context: TContext, info: GraphQLResolveInfo): ClassType1 | Promise> - classType2(input: {}, context: TContext, info: GraphQLResolveInfo): ClassType2 | Promise> - classType3(input: {}, context: TContext, info: GraphQLResolveInfo): ClassType3 | Promise> +export interface ClassType { + classType1: ClassType1 + classType2: ClassType2 + classType3: ClassType3 } -export interface Circular { - children(input: {}, context: TContext, info: GraphQLResolveInfo): Array> | Promise>> +export interface Circular { + children: Array> } -export interface EntryType { - optionalMember(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - booleanMember(input: {}, context: TContext, info: GraphQLResolveInfo): boolean | Promise - stringMember(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - numberType(input: {}, context: TContext, info: GraphQLResolveInfo): NumberType | Promise> - arrayType(input: {}, context: TContext, info: GraphQLResolveInfo): ArrayType | Promise> - typeLiteral(input: {}, context: TContext, info: GraphQLResolveInfo): object | Promise - referenceType(input: {}, context: TContext, info: GraphQLResolveInfo): ReferenceType | Promise> - interfaceType(input: {}, context: TContext, info: GraphQLResolveInfo): Interface | Promise> - typeUnion(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnion | Promise> - interfaceExtends(input: {}, context: TContext, info: GraphQLResolveInfo): InterfaceExtends | Promise> - typeIntersection(input: {}, context: TContext, info: GraphQLResolveInfo): TypeIntersection | Promise> - typeUnionAndIntersection(input: {}, context: TContext, info: GraphQLResolveInfo): TypeUnionAndIntersection | Promise> - mapType(input: {}, context: TContext, info: GraphQLResolveInfo): MapType | Promise> - taggedField(input: {}, context: TContext, info: GraphQLResolveInfo): TaggedField | Promise> - enum(input: {}, context: TContext, info: GraphQLResolveInfo): Enum | Promise> - stringNumber(input: {}, context: TContext, info: GraphQLResolveInfo): StringType | Promise> - id(input: {}, context: TContext, info: GraphQLResolveInfo): ID | Promise> - parameter(input: {}, context: TContext, info: GraphQLResolveInfo): Parameter | Promise> - optionalArrayMember(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - tupleType(input: {}, context: TContext, info: GraphQLResolveInfo): Array | Promise> - defaultType(input: {}, context: TContext, info: GraphQLResolveInfo): DefaultValue | Promise> - anyType(input: {}, context: TContext, info: GraphQLResolveInfo): any | Promise - classType(input: {}, context: TContext, info: GraphQLResolveInfo): ClassType | Promise> - circular(input: {}, context: TContext, info: GraphQLResolveInfo): Circular | Promise> - outerType(input: {}, context: TContext, info: GraphQLResolveInfo): OuterType | Promise> - typeAlias(input: {}, context: TContext, info: GraphQLResolveInfo): TypeAlias | Promise> +export interface EntryType { + optionalMember: string + booleanMember: boolean + stringMember: string + numberType: NumberType + arrayType: ArrayType + typeLiteral: object + referenceType: ReferenceType + interfaceType: Interface + typeUnion: TypeUnion + interfaceExtends: InterfaceExtends + typeIntersection: TypeIntersection + typeUnionAndIntersection: TypeUnionAndIntersection + mapType: MapType + taggedField: TaggedField + enum: Enum + stringNumber: StringType + id: ID + parameter: Parameter + optionalArrayMember: Array + tupleType: Array + defaultType: DefaultValue + anyType: any + classType: ClassType + circular: Circular + outerType: OuterType + typeAlias: TypeAlias } -export interface CreateInput { - member1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise - member2(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise - member3(input: {}, context: TContext, info: GraphQLResolveInfo): CreateInputMember3 | Promise> +export interface CreateInput { + member1: string + member2: number + member3: CreateInputMember3 } -export interface MutationResult { - result(input: {}, context: TContext, info: GraphQLResolveInfo): boolean | Promise +export interface MutationResult { + result: boolean } -export interface GetResult { - result(input: {}, context: TContext, info: GraphQLResolveInfo): Result | Promise> +export interface GetResult { + result: Result } -export interface Result { - member1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface Result { + member1: string member2(input: { input: string }, context: TContext, info: GraphQLResolveInfo): string | Promise } -export interface CreateInputMember3 { - member1(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface CreateInputMember3 { + member1: string } -export interface TypeAlias { - result(input: {}, context: TContext, info: GraphQLResolveInfo): Result2 | Promise> +export interface TypeAlias { + result: Result2 } -export type Result2 = Result3 +export type Result2 = Result3 -export interface Result3 { - result3(input: {}, context: TContext, info: GraphQLResolveInfo): string | Promise +export interface Result3 { + result3: string } -export interface OuterType { - outerType(input: {}, context: TContext, info: GraphQLResolveInfo): number | Promise +export interface OuterType { + outerType: number } export interface ApolloResolvers { diff --git a/src/graphql-root-type-generator.ts b/src/graphql-root-type-generator.ts index 28bdf65..474abff 100644 --- a/src/graphql-root-type-generator.ts +++ b/src/graphql-root-type-generator.ts @@ -23,10 +23,14 @@ export function generateGraphqlRootType(declarations: TypeDeclaration[], graphql const nonRootTypeMembers: string[] = [] for (const member of typeDeclaration.members) { const memberType = getMemberType(member.type, referenceTypes, declarations) - const parameters = getMemberParameters(referenceTypes, declarations, member.parameters) - nonRootTypeMembers.push(` ${member.name}(${parameters}, context: TContext, info: GraphQLResolveInfo): ${memberType} | Promise<${memberType}>`) + if (member.parameters) { + const parameters = getMemberParameters(referenceTypes, declarations, member.parameters) + nonRootTypeMembers.push(` ${member.name}(${parameters}, context: TContext, info: GraphQLResolveInfo): ${memberType} | Promise<${memberType}>`) + } else { + nonRootTypeMembers.push(` ${member.name}: ${memberType}`) + } } - nonRootTypes.push(`export interface ${typeDeclaration.name} { + nonRootTypes.push(`export interface ${typeDeclaration.name} { ${nonRootTypeMembers.join('\n')} }`) } @@ -40,10 +44,10 @@ ${nonRootTypeMembers.join('\n')} ${resolverFields.join('\n')} },`) } else if (typeDeclaration.kind === 'reference') { - nonRootTypes.push(`export type ${typeDeclaration.newName} = ${typeDeclaration.name}`) + nonRootTypes.push(`export type ${typeDeclaration.newName} = ${typeDeclaration.name}`) } else if (typeDeclaration.kind === 'union') { const memberType = getMemberType(typeDeclaration, referenceTypes, declarations) - nonRootTypes.push(`export type ${typeDeclaration.name} = ${memberType}`) + nonRootTypes.push(`export type ${typeDeclaration.name} = ${memberType}`) } else if (typeDeclaration.kind === 'string') { if (typeDeclaration.enums && !isNativeType(typeDeclaration.name)) { referenceTypes.push(typeDeclaration) @@ -99,7 +103,7 @@ ${resolveResults.join('\n')} type ReferenceType = EnumType | StringDeclaration | EnumDeclaration -export function getReferenceTypeImports(referenceTypes: ReferenceType[], graphqlRootTypePath: string) { +function getReferenceTypeImports(referenceTypes: ReferenceType[], graphqlRootTypePath: string) { const map: { [name: string]: string[] } = {} for (const referenceType of referenceTypes) { const file = referenceType.position.file