diff --git a/packages/graphql-codegen-cli/tests/codegen.spec.ts b/packages/graphql-codegen-cli/tests/codegen.spec.ts index dd4ad5bae1f3..99d7e662192a 100644 --- a/packages/graphql-codegen-cli/tests/codegen.spec.ts +++ b/packages/graphql-codegen-cli/tests/codegen.spec.ts @@ -218,7 +218,7 @@ describe('Codegen Executor', () => { expect(output.length).toBe(1); expect(output[0].filename).toBe('out.ts'); - expect(output[0].content).toContain(`hello?: Maybe`); + expect(output[0].content).toContain(`hello?: Maybe`); }); }); diff --git a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts index 819c44e8ea10..6e4d4f6f0aba 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts @@ -689,7 +689,7 @@ export class BaseTypesVisitor< if (this.config.onlyOperationTypes || this.config.onlyEnums) return ''; const originalNode = parent[key] as UnionTypeDefinitionNode; const possibleTypes = originalNode.types - .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t))) + .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t))) .join(' | '); return new DeclarationBlock(this._declarationBlockConfig) @@ -957,8 +957,8 @@ export class BaseTypesVisitor< .join('\n\n'); } - protected _getScalar(name: string): string { - return `Scalars['${name}']`; + protected _getScalar(name: string, type: 'input' | 'output'): string { + return `Scalars['${name}']['${type}']`; } protected _getDirectiveArgumentNadInputFieldMapping(name: string): string { @@ -980,11 +980,11 @@ export class BaseTypesVisitor< return type || null; } - protected _getTypeForNode(node: NamedTypeNode): string { + protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string { const typeAsString = node.name as any as string; if (this.scalars[typeAsString]) { - return this._getScalar(typeAsString); + return this._getScalar(typeAsString, isVisitingInputType ? 'input' : 'output'); } if (this.config.enumValues[typeAsString]) { return this.config.enumValues[typeAsString].typeIdentifier; @@ -1002,7 +1002,7 @@ export class BaseTypesVisitor< NamedType(node: NamedTypeNode, key, parent, path, ancestors): string { const currentVisitContext = this.getVisitorKindContextFromAncestors(ancestors); const isVisitingInputType = currentVisitContext.includes(Kind.INPUT_OBJECT_TYPE_DEFINITION); - const typeToUse = this._getTypeForNode(node); + const typeToUse = this._getTypeForNode(node, isVisitingInputType); if (!isVisitingInputType && this.config.fieldWrapperValue && this.config.wrapFieldDefinitions) { return `FieldWrapper<${typeToUse}>`; diff --git a/packages/plugins/other/visitor-plugin-common/src/variables-to-object.ts b/packages/plugins/other/visitor-plugin-common/src/variables-to-object.ts index f5723cb3252f..78f8331bef41 100644 --- a/packages/plugins/other/visitor-plugin-common/src/variables-to-object.ts +++ b/packages/plugins/other/visitor-plugin-common/src/variables-to-object.ts @@ -60,7 +60,7 @@ export class OperationVariablesToObject { protected getScalar(name: string): string { const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : ''; - return `${prefix}Scalars['${name}']`; + return `${prefix}Scalars['${name}']['input']`; } protected getDirectiveMapping(name: string): string { diff --git a/packages/plugins/typescript/operations/tests/__snapshots__/ts-documents.spec.ts.snap b/packages/plugins/typescript/operations/tests/__snapshots__/ts-documents.spec.ts.snap index f6d3c0bb6eb6..8345468ea6cf 100644 --- a/packages/plugins/typescript/operations/tests/__snapshots__/ts-documents.spec.ts.snap +++ b/packages/plugins/typescript/operations/tests/__snapshots__/ts-documents.spec.ts.snap @@ -5,15 +5,15 @@ exports[`TypeScript Operations Plugin Config should include fragment variable de export type TextNotificationFragmentFragmentVariables = Exact<{ - skip: Scalars['Boolean']; + skip: Scalars['Boolean']['input']; }>; " `; exports[`TypeScript Operations Plugin Issues #2699 - Issues with multiple interfaces and unions 1`] = ` "export type GetEntityBrandDataQueryVariables = Exact<{ - gid: Scalars['ID']; - brand: Scalars['ID']; + gid: Scalars['ID']['input']; + brand: Scalars['ID']['input']; }>; @@ -81,32 +81,32 @@ export type Scalars = { }; export type Venue = { - id: Scalars['String']; - name: Scalars['String']; + id: Scalars['String']['output']; + name: Scalars['String']['output']; }; export type GpsPosition = { __typename?: 'GPSPosition'; - lat: Scalars['Float']; - lng: Scalars['Float']; + lat: Scalars['Float']['output']; + lng: Scalars['Float']['output']; }; export type VenueWithPosition = { - id: Scalars['String']; + id: Scalars['String']['output']; gpsPosition: GpsPosition; }; export type Hotel = VenueWithPosition & Venue & { __typename?: 'Hotel'; - id: Scalars['String']; + id: Scalars['String']['output']; gpsPosition: GpsPosition; - name: Scalars['String']; + name: Scalars['String']['output']; }; export type Transport = Venue & { __typename?: 'Transport'; - id: Scalars['String']; - name: Scalars['String']; + id: Scalars['String']['output']; + name: Scalars['String']['output']; }; export type Query = { @@ -377,18 +377,18 @@ export type Query = { }; export type Concept = { - id?: Maybe; + id?: Maybe; }; export type Dimension = Concept & { __typename?: 'Dimension'; - id?: Maybe; + id?: Maybe; }; export type DimValue = { __typename?: 'DimValue'; dimension?: Maybe; - value: Scalars['String']; + value: Scalars['String']['output']; }; export type Searchable = Dimension | DimValue; @@ -490,35 +490,35 @@ export type Scalars = { }; export type Error = { - message: Scalars['String']; + message: Scalars['String']['output']; }; export type Error1 = Error & { __typename?: 'Error1'; - message: Scalars['String']; + message: Scalars['String']['output']; }; export type Error2 = Error & { __typename?: 'Error2'; - message: Scalars['String']; + message: Scalars['String']['output']; }; export type Error3 = Error & { __typename?: 'Error3'; - message: Scalars['String']; + message: Scalars['String']['output']; info?: Maybe; }; export type AdditionalInfo = { __typename?: 'AdditionalInfo'; - message: Scalars['String']; - message2: Scalars['String']; + message: Scalars['String']['output']; + message2: Scalars['String']['output']; }; export type User = { __typename?: 'User'; - id: Scalars['ID']; - login: Scalars['String']; + id: Scalars['ID']['output']; + login: Scalars['String']['output']; }; export type UserResult = User | Error2 | Error3; @@ -585,37 +585,37 @@ export type Scalars = { }; export type Error = { - message: Scalars['String']; + message: Scalars['String']['output']; }; export type Error1 = Error & { __typename?: 'Error1'; - message: Scalars['String']; + message: Scalars['String']['output']; }; export type Error2 = Error & { __typename?: 'Error2'; - message: Scalars['String']; + message: Scalars['String']['output']; }; export type Error3 = Error & { __typename?: 'Error3'; - message: Scalars['String']; + message: Scalars['String']['output']; info?: Maybe; }; export type AdditionalInfo = { __typename?: 'AdditionalInfo'; - message: Scalars['String']; - message2: Scalars['String']; + message: Scalars['String']['output']; + message2: Scalars['String']['output']; }; export type User = { __typename?: 'User'; - id: Scalars['ID']; - login: Scalars['String']; - test?: Maybe; - test2?: Maybe; + id: Scalars['ID']['output']; + login: Scalars['String']['output']; + test?: Maybe; + test2?: Maybe; }; export type UserResult = User | Error2 | Error3; diff --git a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts index 0d29455f7e34..ad933cd8ce51 100644 --- a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts +++ b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts @@ -2508,7 +2508,7 @@ describe('TypeScript Operations Plugin', () => { expect(content).toBeSimilarStringTo( `export type MeQueryVariables = Exact<{ - repoFullName: Scalars['String']; + repoFullName: Scalars['String']['input']; }>;` ); expect(content).toBeSimilarStringTo(` @@ -2823,14 +2823,14 @@ describe('TypeScript Operations Plugin', () => { expect(content).toBeSimilarStringTo( `export type TestQueryQueryVariables = Exact<{ - username?: InputMaybe; - email?: InputMaybe; - password: Scalars['String']; + username?: InputMaybe; + email?: InputMaybe; + password: Scalars['String']['input']; input?: InputMaybe; mandatoryInput: InputType; - testArray?: InputMaybe> | InputMaybe>; - requireString: Array> | InputMaybe; - innerRequired: Array | Scalars['String']; + testArray?: InputMaybe> | InputMaybe>; + requireString: Array> | InputMaybe; + innerRequired: Array | Scalars['String']['input']; }>;` ); await validate(content, config, schema); @@ -2849,7 +2849,7 @@ describe('TypeScript Operations Plugin', () => { expect(content).toBeSimilarStringTo( `export type TestQueryQueryVariables = Exact<{ - test?: InputMaybe; + test?: InputMaybe; }>;` ); await validate(content, config); @@ -3203,7 +3203,7 @@ describe('TypeScript Operations Plugin', () => { expect(content).toBeSimilarStringTo(` export type UsersQueryVariables = Exact<{ - reverse?: InputMaybe; + reverse?: InputMaybe; }>; `); }); @@ -4066,18 +4066,18 @@ describe('TypeScript Operations Plugin', () => { }; export type Concept = { - id?: Maybe; + id?: Maybe; }; export type Dimension = Concept & { __typename?: 'Dimension'; - id?: Maybe; + id?: Maybe; }; export type DimValue = { __typename?: 'DimValue'; dimension?: Maybe; - value: Scalars['String']; + value: Scalars['String']['output']; }; export type Searchable = Dimension | DimValue; @@ -4177,7 +4177,7 @@ describe('TypeScript Operations Plugin', () => { export type Dimension = { __typename?: 'Dimension'; - id?: Maybe; + id?: Maybe; }; export type SearchableFragmentFragment = { __typename?: 'Dimension', id?: string | null }; @@ -4265,7 +4265,7 @@ describe('TypeScript Operations Plugin', () => { export type Dimension = { __typename?: 'Dimension'; - id?: Maybe; + id?: Maybe; }; export type SearchPopularQueryVariables = Exact<{ [key: string]: never; }>; @@ -5465,9 +5465,9 @@ function test(q: GetEntityBrandDataQuery): void { expect(content).toBeSimilarStringTo(` export type UserQueryVariables = Exact<{ - testArray?: InputMaybe> | InputMaybe>; - requireString: Array> | InputMaybe; - innerRequired: Array | Scalars['String']; + testArray?: InputMaybe> | InputMaybe>; + requireString: Array> | InputMaybe; + innerRequired: Array | Scalars['String']['input']; }>;`); await validate(content, config); }); @@ -5497,9 +5497,9 @@ function test(q: GetEntityBrandDataQuery): void { expect(content).toBeSimilarStringTo(` export type UserQueryVariables = Exact<{ - testArray?: InputMaybe>>; - requireString: Array>; - innerRequired: Array; + testArray?: InputMaybe>>; + requireString: Array>; + innerRequired: Array; }>;`); await validate(content, config); }); @@ -6040,7 +6040,7 @@ function test(q: GetEntityBrandDataQuery): void { expect(content).toBeSimilarStringTo(` export type UserQueryVariables = Exact<{ - showAddress: Scalars['Boolean']; + showAddress: Scalars['Boolean']['input']; }>; export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', name: string, address?: string, nicknames?: Array | null, parents?: Array } };`); @@ -6093,8 +6093,8 @@ function test(q: GetEntityBrandDataQuery): void { expect(content).toBeSimilarStringTo(` export type UserQueryVariables = Exact<{ - showAddress: Scalars['Boolean']; - showName: Scalars['Boolean']; + showAddress: Scalars['Boolean']['input']; + showName: Scalars['Boolean']['input']; }>; export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', id: string, name?: string, address?: { __typename?: 'Address', city: string }, friends?: Array<{ __typename?: 'User', id: string }> } };`); }); @@ -6141,8 +6141,8 @@ function test(q: GetEntityBrandDataQuery): void { expect(content).toBeSimilarStringTo(` export type UserQueryVariables = Exact<{ - showAddress: Scalars['Boolean']; - showName: Scalars['Boolean']; + showAddress: Scalars['Boolean']['input']; + showName: Scalars['Boolean']['input']; }>; export type UserQuery = ( diff --git a/packages/plugins/typescript/resolvers/tests/__snapshots__/ts-resolvers.spec.ts.snap b/packages/plugins/typescript/resolvers/tests/__snapshots__/ts-resolvers.spec.ts.snap index a5e7886b2e94..108facba83a0 100644 --- a/packages/plugins/typescript/resolvers/tests/__snapshots__/ts-resolvers.spec.ts.snap +++ b/packages/plugins/typescript/resolvers/tests/__snapshots__/ts-resolvers.spec.ts.snap @@ -23,27 +23,27 @@ export type Scalars = { export type MyType = { __typename?: 'MyType'; - foo: Scalars['String']; + foo: Scalars['String']['output']; otherType?: Maybe; - withArgs?: Maybe; + withArgs?: Maybe; unionChild?: Maybe; }; export type MyTypeWithArgsArgs = { - arg?: InputMaybe; - arg2: Scalars['String']; + arg?: InputMaybe; + arg2: Scalars['String']['input']; }; export type Child = { __typename?: 'Child'; - bar: Scalars['String']; + bar: Scalars['String']['output']; parent?: Maybe; }; export type MyOtherType = { __typename?: 'MyOtherType'; - bar: Scalars['String']; + bar: Scalars['String']['output']; }; export type ChildUnion = Child | MyOtherType; @@ -59,16 +59,16 @@ export type Subscription = { }; export type Node = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type SomeNode = Node & { __typename?: 'SomeNode'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type AnotherNode = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type WithChild = { @@ -83,14 +83,14 @@ export type WithChildren = { export type AnotherNodeWithChild = AnotherNode & WithChild & { __typename?: 'AnotherNodeWithChild'; - id: Scalars['ID']; + id: Scalars['ID']['output']; unionChild?: Maybe; interfaceChild?: Maybe; }; export type AnotherNodeWithAll = AnotherNode & WithChild & WithChildren & { __typename?: 'AnotherNodeWithAll'; - id: Scalars['ID']; + id: Scalars['ID']['output']; unionChild?: Maybe; unionChildren: Array; interfaceChild?: Maybe; @@ -227,9 +227,9 @@ export type ResolversParentTypes = ResolversObject<{ }>; export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; export type MyDirectiveDirectiveResolver = DirectiveResolverFn; @@ -486,9 +486,9 @@ export type ResolversParentTypes = ResolversObject<{ }>; export type MyDirectiveDirectiveArgs = { - arg: Types.Scalars['Int']; - arg2: Types.Scalars['String']; - arg3: Types.Scalars['Boolean']; + arg: Types.Scalars['Int']['input']; + arg2: Types.Scalars['String']['input']; + arg3: Types.Scalars['Boolean']['input']; }; export type MyDirectiveDirectiveResolver = DirectiveResolverFn; @@ -627,27 +627,27 @@ export type Scalars = { export type MyType = { __typename?: 'MyType'; - foo: Scalars['String']; + foo: Scalars['String']['output']; otherType?: Maybe; - withArgs?: Maybe; + withArgs?: Maybe; unionChild?: Maybe; }; export type MyTypeWithArgsArgs = { - arg?: InputMaybe; - arg2: Scalars['String']; + arg?: InputMaybe; + arg2: Scalars['String']['input']; }; export type Child = { __typename?: 'Child'; - bar: Scalars['String']; + bar: Scalars['String']['output']; parent?: Maybe; }; export type MyOtherType = { __typename?: 'MyOtherType'; - bar: Scalars['String']; + bar: Scalars['String']['output']; }; export type ChildUnion = Child | MyOtherType; @@ -663,16 +663,16 @@ export type Subscription = { }; export type Node = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type SomeNode = Node & { __typename?: 'SomeNode'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type AnotherNode = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type WithChild = { @@ -687,14 +687,14 @@ export type WithChildren = { export type AnotherNodeWithChild = AnotherNode & WithChild & { __typename?: 'AnotherNodeWithChild'; - id: Scalars['ID']; + id: Scalars['ID']['output']; unionChild?: Maybe; interfaceChild?: Maybe; }; export type AnotherNodeWithAll = AnotherNode & WithChild & WithChildren & { __typename?: 'AnotherNodeWithAll'; - id: Scalars['ID']; + id: Scalars['ID']['output']; unionChild?: Maybe; unionChildren: Array; interfaceChild?: Maybe; @@ -831,9 +831,9 @@ export type ResolversParentTypes = ResolversObject<{ }>; export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; export type MyDirectiveDirectiveResolver = DirectiveResolverFn; diff --git a/packages/plugins/typescript/resolvers/tests/mapping.spec.ts b/packages/plugins/typescript/resolvers/tests/mapping.spec.ts index 61dfb0b84361..5d7a8382b7fa 100644 --- a/packages/plugins/typescript/resolvers/tests/mapping.spec.ts +++ b/packages/plugins/typescript/resolvers/tests/mapping.spec.ts @@ -1225,9 +1225,9 @@ describe('ResolversTypes', () => { expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); expect(result.content).toBeSimilarStringTo(` @@ -1308,9 +1308,9 @@ describe('ResolversTypes', () => { expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); expect(result.content).toBeSimilarStringTo(` @@ -1389,9 +1389,9 @@ describe('ResolversTypes', () => { expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); expect(result.content).toBeSimilarStringTo(` diff --git a/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts b/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts index bd52c93cd9e6..285735850441 100644 --- a/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts +++ b/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts @@ -861,9 +861,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); @@ -937,9 +937,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; };`); expect(result.content).toBeSimilarStringTo(` @@ -1014,9 +1014,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); @@ -1105,9 +1105,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); @@ -1180,9 +1180,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); @@ -1254,9 +1254,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); @@ -1329,9 +1329,9 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { - arg: Scalars['Int']; - arg2: Scalars['String']; - arg3: Scalars['Boolean']; + arg: Scalars['Int']['input']; + arg2: Scalars['String']['input']; + arg3: Scalars['Boolean']['input']; }; `); @@ -1639,13 +1639,13 @@ export type ResolverFn = ( expect(tsContent.content).toBeSimilarStringTo(` export type CccFoo = { __typename?: 'CCCFoo'; - foo: Scalars['String']; + foo: Scalars['String']['output']; }; `); expect(tsContent.content).toBeSimilarStringTo(` export type CccBar = { __typename?: 'CCCBar'; - bar: Scalars['String']; + bar: Scalars['String']['output']; }; `); diff --git a/packages/plugins/typescript/typescript/src/visitor.ts b/packages/plugins/typescript/typescript/src/visitor.ts index cb8d08b81df9..0ab076816ac0 100644 --- a/packages/plugins/typescript/typescript/src/visitor.ts +++ b/packages/plugins/typescript/typescript/src/visitor.ts @@ -105,7 +105,7 @@ export class TsVisitor< }); } - protected _getTypeForNode(node: NamedTypeNode): string { + protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string { const typeAsString = node.name as any as string; if (this.config.useImplementingTypes) { @@ -128,7 +128,7 @@ export class TsVisitor< } } - const typeString = super._getTypeForNode(node); + const typeString = super._getTypeForNode(node, isVisitingInputType); const schemaType = this._schema.getType(node.name as any as string); if (isEnumType(schemaType)) { @@ -252,7 +252,7 @@ export class TsVisitor< } const originalNode = parent[key] as UnionTypeDefinitionNode; const possibleTypes = originalNode.types - .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t))) + .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t))) .concat(...withFutureAddedValue) .join(' | '); diff --git a/packages/plugins/typescript/typescript/tests/__snapshots__/typescript.spec.ts.snap b/packages/plugins/typescript/typescript/tests/__snapshots__/typescript.spec.ts.snap index 3a72b30a1241..824f8f15dcf7 100644 --- a/packages/plugins/typescript/typescript/tests/__snapshots__/typescript.spec.ts.snap +++ b/packages/plugins/typescript/typescript/tests/__snapshots__/typescript.spec.ts.snap @@ -16,26 +16,26 @@ export type Matrix = { }; export type Pill = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type RedPill = Pill & { __typename?: 'RedPill'; - red: Scalars['String']; + red: Scalars['String']['output']; }; export type GreenPill = Pill & { __typename?: 'GreenPill'; - green: Scalars['String']; + green: Scalars['String']['output']; }; export type Foo = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; export type Bar = Foo & { __typename?: 'Bar'; - lol: Scalars['String']; + lol: Scalars['String']['output']; }; export type Hello = { @@ -49,17 +49,17 @@ export type NoInterface = { }; export type NestedInterface = { - field: Scalars['String']; + field: Scalars['String']['output']; }; export type NestedType1 = NestedInterface & { __typename?: 'NestedType1'; - hi: Scalars['String']; + hi: Scalars['String']['output']; }; export type NestedType2 = NestedInterface & { __typename?: 'NestedType2'; - ho: Scalars['String']; + ho: Scalars['String']['output']; }; export type NestedField = { diff --git a/packages/plugins/typescript/typescript/tests/typescript.spec.ts b/packages/plugins/typescript/typescript/tests/typescript.spec.ts index eeb7cebcf66f..732a4f6e1ea9 100644 --- a/packages/plugins/typescript/typescript/tests/typescript.spec.ts +++ b/packages/plugins/typescript/typescript/tests/typescript.spec.ts @@ -43,9 +43,12 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], {}, { outputFile: '' }); + expect(result.prepend).toBeSimilarStringTo('export type InputMaybe = Maybe;'); expect(result.content).toBeSimilarStringTo(` /** MyInput */ - export type MyInput`); + export type MyInput = { + f?: InputMaybe; + }`); }); it('Should add description for input fields', async () => { @@ -62,7 +65,7 @@ describe('TypeScript', () => { /** MyInput */ export type MyInput = { /** f is something */ - f: Scalars['String']; + f: Scalars['String']['input']; }`); }); @@ -83,7 +86,9 @@ describe('TypeScript', () => { * MyInput * multiline */ - export type MyInput`); + export type MyInput = { + f: Scalars['String']['input']; + }`); }); it('Should work with unions', async () => { @@ -102,7 +107,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` /** my union */ - export type A = `); + export type A = B | C`); }); it('Should work with types', async () => { @@ -120,11 +125,17 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` /** this is b */ - export type B = `); + export type B = { + __typename?: 'B'; + id?: Maybe; + }`); expect(result.content).toBeSimilarStringTo(` /** this is c */ - export type C = `); + export type C = { + __typename?: 'C'; + id?: Maybe; + }`); }); it('Should work with type fields', async () => { @@ -140,7 +151,7 @@ describe('TypeScript', () => { export type B = { __typename?: 'B'; /** the id */ - id?: Maybe; + id?: Maybe; };`); }); @@ -156,7 +167,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type Node = { /** the id */ - id: Scalars['ID']; + id: Scalars['ID']['output']; };`); }); @@ -322,6 +333,7 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo(`/** My custom scalar */`); expect(result.content).toBeSimilarStringTo(` export type Scalars = { ID: { input: string | number; output: string; } @@ -343,8 +355,11 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** MyInput */'); expect(result.content).toBeSimilarStringTo(` - export type MyInput`); + export type MyInput = { + f?: InputMaybe; + }`); }); it('Should not add description for input fields', async () => { @@ -357,9 +372,11 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** MyInput */'); + expect(result.content).not.toBeSimilarStringTo('/** f is something */'); expect(result.content).toBeSimilarStringTo(` export type MyInput = { - f: Scalars['String']; + f: Scalars['String']['input']; }`); }); @@ -375,8 +392,16 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo(` + /** + * MyInput + * multiline + */ + `); expect(result.content).toBeSimilarStringTo(` - export type MyInput`); + export type MyInput = { + f: Scalars['String']['input']; + }`); }); it('Should work with unions', async () => { @@ -393,8 +418,9 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** my union */'); expect(result.content).toBeSimilarStringTo(` - export type A = `); + export type A = B | C`); }); it('Should work with types', async () => { @@ -410,11 +436,19 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** this is b */'); expect(result.content).toBeSimilarStringTo(` - export type B = `); + export type B = { + __typename?: 'B'; + id?: Maybe; + }`); + expect(result.content).not.toBeSimilarStringTo('/** this is c */'); expect(result.content).toBeSimilarStringTo(` - export type C = `); + export type C = { + __typename?: 'C'; + id?: Maybe; + }`); }); it('Should work with type fields', async () => { @@ -426,10 +460,11 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** the id */'); expect(result.content).toBeSimilarStringTo(` export type B = { __typename?: 'B'; - id?: Maybe; + id?: Maybe; };`); }); @@ -442,9 +477,10 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** the id */'); expect(result.content).toBeSimilarStringTo(` export type Node = { - id: Scalars['ID']; + id: Scalars['ID']['output']; };`); }); @@ -460,6 +496,9 @@ describe('TypeScript', () => { `); const result = await plugin(schema, [], { disableDescriptions: true }, { outputFile: '' }); + expect(result.content).not.toBeSimilarStringTo('/** custom enum */'); + expect(result.content).not.toBeSimilarStringTo('/** this is a */'); + expect(result.content).not.toBeSimilarStringTo('/** this is b */'); expect(result.content).toBeSimilarStringTo(` export enum MyEnum { A = 'A', @@ -484,6 +523,9 @@ describe('TypeScript', () => { { outputFile: '' } )) as Types.ComplexPluginOutput; + expect(result.content).not.toBeSimilarStringTo('/** custom enum */'); + expect(result.content).not.toBeSimilarStringTo('/** this is a */'); + expect(result.content).not.toBeSimilarStringTo('/** this is b */'); expect(result.content).toBeSimilarStringTo(` export type MyEnum = | 'A' @@ -544,11 +586,11 @@ describe('TypeScript', () => { const output = mergeOutputs([result]); expect(output).toContain(`export type InputMaybe = T | null | undefined;`); expect(output).toContain(`export type Maybe = T | null;`); - expect(output).toContain(`test?: Maybe;`); - expect(output).toContain(`id?: InputMaybe;`); + expect(output).toContain(`test?: Maybe;`); + expect(output).toContain(`id?: InputMaybe;`); expect(output).toContain(`filter?: InputMaybe;`); - expect(output).toContain(`a?: InputMaybe;`); - expect(output).toContain(`b?: InputMaybe;`); + expect(output).toContain(`a?: InputMaybe;`); + expect(output).toContain(`b?: InputMaybe;`); }); it('#5643 - Incorrect combinations of declartionKinds leads to syntax error', async () => { @@ -891,7 +933,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(`export type ITest = { __typename?: 'Test'; t?: Maybe; - test?: Maybe; + test?: Maybe; };`); expect(result.content).toBeSimilarStringTo(`export type ITestTestArgs = { @@ -934,10 +976,11 @@ describe('TypeScript', () => { }); it('#1488 - Should generate readonly also in input types when immutableTypes is set', async () => { - const schema = buildSchema(` - input MyInput { - f: String! - }`); + const schema = buildSchema(/* GraphQL */ ` + input MyInput { + f: String! + } + `); const result = (await plugin( schema, @@ -948,25 +991,26 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInput = { - readonly f: Scalars['String']; + readonly f: Scalars['String']['input']; };`); validateTs(result); }); it('#3141 - @deprecated directive support', async () => { - const schema = buildSchema(` - type User { - fullName: String! - firstName: String! @deprecated(reason: "Field \`fullName\` has been superseded by \`firstName\`.") - }`); + const schema = buildSchema(/* GraphQL */ ` + type User { + fullName: String! + firstName: String! @deprecated(reason: "Field \`fullName\` has been superseded by \`firstName\`.") + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type User = { __typename?: 'User'; - fullName: Scalars['String']; + fullName: Scalars['String']['output']; /** @deprecated Field \`fullName\` has been superseded by \`firstName\`. */ - firstName: Scalars['String']; + firstName: Scalars['String']['output']; };`); validateTs(result); }); @@ -989,42 +1033,42 @@ describe('TypeScript', () => { }); it('#7766 - input value @deprecated directive support', async () => { - const schema = buildSchema(` - input MyInput { - A: Int - B: Int @deprecated(reason: "input value \`B\` has been deprecated.") - }`); + const schema = buildSchema(/* GraphQL */ ` + input MyInput { + A: Int + B: Int @deprecated(reason: "input value \`B\` has been deprecated.") + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyInput = { - A?: InputMaybe; + A?: InputMaybe; /** @deprecated input value \`B\` has been deprecated. */ - B?: InputMaybe; + B?: InputMaybe; };`); validateTs(result); }); it('#1462 - Union of scalars and argument of directive', async () => { - const schema = buildSchema(` - union Any = String | Int | Float | ID + const schema = buildSchema(/* GraphQL */ ` + union Any = String | Int | Float | ID - directive @default( - value: Any, - ) on ENUM_VALUE | FIELD_DEFINITION + directive @default(value: Any) on ENUM_VALUE | FIELD_DEFINITION - type CardEdge { - count: Int! @default(value: 1) - }`); + type CardEdge { + count: Int! @default(value: 1) + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo( - `export type Any = Scalars['String'] | Scalars['Int'] | Scalars['Float'] | Scalars['ID'];` + `export type Any = Scalars['String']['output'] | Scalars['Int']['output'] | Scalars['Float']['output'] | Scalars['ID']['output'];` ); expect(result.content).toBeSimilarStringTo(` export type CardEdge = { __typename?: 'CardEdge'; - count: Scalars['Int']; + count: Scalars['Int']['output']; };`); validateTs(result); }); @@ -1082,10 +1126,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface ISuggestion { - id: Scalars['ID']; - userId: Scalars['ID']; + id: Scalars['ID']['output']; + userId: Scalars['ID']['output']; suggestionType: SuggestionType; - text: Scalars['String']; + text: Scalars['String']['output']; } `); expect(result.content).toBeSimilarStringTo(` @@ -1103,7 +1147,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface IRootQueryTypeSuggestionsForUserArgs { - userId: Scalars['ID']; + userId: Scalars['ID']['input']; suggestionType: SuggestionType; } `); @@ -1112,7 +1156,7 @@ describe('TypeScript', () => { describe('Config', () => { it('Should build type correctly when specified with avoidOptionals config', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type MyType { foo: String bar: String! @@ -1128,15 +1172,15 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyType = { __typename?: 'MyType'; - foo: Maybe; - bar: Scalars['String']; + foo: Maybe; + bar: Scalars['String']['output']; }; `); validateTs(result); }); it('Should build input type correctly when specified with avoidInputOptionals config', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` input MyInput { foo: String bar: String! @@ -1151,8 +1195,8 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInput = { - foo: InputMaybe; - bar: Scalars['String']; + foo: InputMaybe; + bar: Scalars['String']['input']; } `); @@ -1160,10 +1204,11 @@ describe('TypeScript', () => { }); it('Should build type correctly when specified with immutableTypes config', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type MyType { foo: [String!]! - }`); + } + `); const result = (await plugin( schema, [], @@ -1174,7 +1219,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyType = { readonly __typename?: 'MyType'; - readonly foo: ReadonlyArray; + readonly foo: ReadonlyArray; }; `); validateTs(result); @@ -1375,10 +1420,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type mytypefooargs = { - a: Scalars['String']; - b?: InputMaybe; - c?: InputMaybe>>; - d: Array; + a: Scalars['String']['input']; + b?: InputMaybe; + c?: InputMaybe>>; + d: Array; }; `); expect(result.content).toBeSimilarStringTo(` @@ -1450,10 +1495,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyTypefooArgs = { - a: Scalars['String']; - b?: InputMaybe; - c?: InputMaybe>>; - d: Array; + a: Scalars['String']['input']; + b?: InputMaybe; + c?: InputMaybe>>; + d: Array; }; `); @@ -1524,7 +1569,7 @@ describe('TypeScript', () => { }); it('Should use class correctly when declarationKind: class is set', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` input MyInput { id: ID! displayName: String @@ -1546,16 +1591,16 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export class MyInput { - id: Scalars['ID']; - displayName?: InputMaybe; + id: Scalars['ID']['input']; + displayName?: InputMaybe; } `); expect(result.content).toBeSimilarStringTo(` export class MyType { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; } `); @@ -1563,7 +1608,7 @@ describe('TypeScript', () => { }); it('Should use interface for type when declarationKind for types is set', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` input MyInput { id: ID! displayName: String @@ -1587,23 +1632,23 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInput = { - id: Scalars['ID']; - displayName?: InputMaybe; + id: Scalars['ID']['input']; + displayName?: InputMaybe; } `); expect(result.content).toBeSimilarStringTo(` export interface MyType { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; } `); validateTs(result); }); it('Should use interface for input when declarationKind for inputs is set', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` input MyInput { id: ID! displayName: String @@ -1627,23 +1672,23 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface MyInput { - id: Scalars['ID']; - displayName?: InputMaybe; + id: Scalars['ID']['input']; + displayName?: InputMaybe; } `); expect(result.content).toBeSimilarStringTo(` export type MyType = { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; } `); validateTs(result); }); it('Should use interface for arguments when declarationKind for arguments is set', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type MyType { id: ID! displayName: String @@ -1664,22 +1709,22 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyType = { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; child?: Maybe; } `); expect(result.content).toBeSimilarStringTo(` export interface MyTypeChildArgs { - id: Scalars['ID']; + id: Scalars['ID']['input']; } `); validateTs(result); }); it('Should use interface for all objects when declarationKind is interface', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` input MyInput { id: ID! displayName: String @@ -1701,16 +1746,16 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface MyInput { - id: Scalars['ID']; - displayName?: InputMaybe; + id: Scalars['ID']['input']; + displayName?: InputMaybe; } `); expect(result.content).toBeSimilarStringTo(` export interface MyType { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; } `); validateTs(result); @@ -1744,7 +1789,7 @@ describe('TypeScript', () => { }); it('Should extend one interface from another', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` interface MyInterface { id: ID! displayName: String @@ -1767,24 +1812,24 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface MyInterface { - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; } `); expect(result.content).toBeSimilarStringTo(` export interface MyType extends MyInterface { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; - value?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; + value?: Maybe; } `); validateTs(result); }); it('Should extend mutiple interfaces', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` interface MyInterface1 { id: ID! displayName: String @@ -1811,23 +1856,23 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface MyInterface1 { - id: Scalars['ID']; - displayName?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; } `); expect(result.content).toBeSimilarStringTo(` export interface MyInterface2 { - value?: Maybe; + value?: Maybe; } `); expect(result.content).toBeSimilarStringTo(` export interface MyType extends MyInterface1, MyInterface2 { __typename?: 'MyType'; - id: Scalars['ID']; - displayName?: Maybe; - value?: Maybe; + id: Scalars['ID']['output']; + displayName?: Maybe; + value?: Maybe; } `); validateTs(result); @@ -1836,11 +1881,12 @@ describe('TypeScript', () => { describe('Scalars', () => { it('Should generate a scalars mapping correctly for built-in scalars', async () => { - const schema = buildSchema(` - type MyType { - foo: String - bar: String! - }`); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo: String + bar: String! + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` @@ -1855,20 +1901,23 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyType = { __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['String']; + foo?: Maybe; + bar: Scalars['String']['output']; };`); validateTs(result); }); it('Should generate a scalars mapping correctly when using scalars as path', async () => { - const schema = buildSchema(` - scalar MyScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar + scalar MyScalarInput - type MyType { - foo: String - bar: MyScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + baz(input: MyScalarInput): MyScalarInput + } + `); const result = (await plugin( schema, [], @@ -1879,39 +1928,48 @@ describe('TypeScript', () => { )) as Types.ComplexPluginOutput; expect(result.prepend).toContain(`import { MyScalar } from '../../scalars';`); + expect(result.prepend).toContain(`import { MyScalarInput } from '../../scalars';`); expect(result.prepend).toContain(`import { String } from '../../scalars';`); expect(result.prepend).toContain(`import { Boolean } from '../../scalars';`); expect(result.content).toBeSimilarStringTo(` - export type Scalars = { - ID: { input: string | number; output: string; } - String: { input: String['input']; output: String['output']; } - Boolean: { input: Boolean['input']; output: Boolean['output']; } - Int: { input: number; output: number; } - Float: { input: number; output: number; } - MyScalar: { input: MyScalar['input']; output: MyScalar['output']; } - };`); + export type Scalars = { + ID: { input: string | number; output: string; } + String: { input: String['input']; output: String['output']; } + Boolean: { input: Boolean['input']; output: Boolean['output']; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + MyScalar: { input: MyScalar['input']; output: MyScalar['output']; } + MyScalarInput: { input: MyScalarInput['input']; output: MyScalarInput['output']; } + };`); expect(result.content).toBeSimilarStringTo(` - export type MyType = { - __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['MyScalar']; - };`); + export type MyType = { + __typename?: 'MyType'; + foo?: Maybe; + bar: Scalars['MyScalar']['output']; + baz?: Maybe; + };`); + expect(result.content).toBeSimilarStringTo(` + export type MyTypeBazArgs = { + input?: InputMaybe; + };`); validateTs(result); }); it('Should import a type of a mapped scalar', async () => { - const schema = buildSchema(` - scalar MyScalar - scalar MyOtherScalar - scalar MyAliasedScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar + scalar MyOtherScalar + scalar MyAliasedScalar - type MyType { - foo: String - bar: MyScalar! - baz: MyOtherScalar! - qux: MyAliasedScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + baz: MyOtherScalar! + qux: MyAliasedScalar! + tux(in: MyScalar!): MyScalar! + } + `); const result = (await plugin( schema, [], @@ -1930,65 +1988,79 @@ describe('TypeScript', () => { expect(result.prepend).toContain(`import { MyOtherScalar } from '../../scalars';`); expect(result.prepend).toContain(`import { MyAliasedScalar as AliasedScalar } from '../../scalars';`); expect(result.content).toBeSimilarStringTo(` - export type Scalars = { - ID: { input: string | number; output: string; } - String: { input: string; output: string; } - Boolean: { input: boolean; output: boolean; } - Int: { input: number; output: number; } - Float: { input: number; output: number; } - MyScalar: { input: MyScalar['input']; output: MyScalar['output']; } - MyOtherScalar: { input: MyOtherScalar['input']; output: MyOtherScalar['output']; } - MyAliasedScalar: { input: AliasedScalar['input']; output: AliasedScalar['output']; } - };`); + export type Scalars = { + ID: { input: string | number; output: string; } + String: { input: string; output: string; } + Boolean: { input: boolean; output: boolean; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + MyScalar: { input: MyScalar['input']; output: MyScalar['output']; } + MyOtherScalar: { input: MyOtherScalar['input']; output: MyOtherScalar['output']; } + MyAliasedScalar: { input: AliasedScalar['input']; output: AliasedScalar['output']; } + };`); expect(result.content).toBeSimilarStringTo(` - export type MyType = { - __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['MyScalar']; - baz: Scalars['MyOtherScalar']; - qux: Scalars['MyAliasedScalar']; - };`); + export type MyType = { + __typename?: 'MyType'; + foo?: Maybe; + bar: Scalars['MyScalar']['output']; + baz: Scalars['MyOtherScalar']['output']; + qux: Scalars['MyAliasedScalar']['output']; + tux: Scalars['MyScalar']['output']; + };`); + expect(result.content).toBeSimilarStringTo(` + export type MyTypeTuxArgs = { + in: Scalars['MyScalar']['input']; + }`); validateTs(result); }); it('Should generate a scalars mapping correctly for custom scalars', async () => { - const schema = buildSchema(` - scalar MyScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar - type MyType { - foo: String - bar: MyScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + buz(input: MyScalar!): MyScalar! + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` - export type Scalars = { - ID: { input: string | number; output: string; } - String: { input: string; output: string; } - Boolean: { input: boolean; output: boolean; } - Int: { input: number; output: number; } - Float: { input: number; output: number; } - MyScalar: { input: any; output: any; } - };`); + export type Scalars = { + ID: { input: string | number; output: string; } + String: { input: string; output: string; } + Boolean: { input: boolean; output: boolean; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + MyScalar: { input: any; output: any; } + };`); expect(result.content).toBeSimilarStringTo(` - export type MyType = { - __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['MyScalar']; - };`); + export type MyType = { + __typename?: 'MyType'; + foo?: Maybe; + bar: Scalars['MyScalar']['output']; + buz: Scalars['MyScalar']['output']; + };`); + expect(result.content).toBeSimilarStringTo(` + export type MyTypeBuzArgs = { + input: Scalars['MyScalar']['input']; + }`); validateTs(result); }); it('Should generate a scalars mapping correctly for custom scalars with mapping', async () => { - const schema = buildSchema(` - scalar MyScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar - type MyType { - foo: String - bar: MyScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + buz(input: MyScalar!): MyScalar! + } + `); const result = (await plugin( schema, [], @@ -1997,32 +2069,39 @@ describe('TypeScript', () => { )) as Types.ComplexPluginOutput; expect(result.content).toBeSimilarStringTo(` - export type Scalars = { - ID: { input: string | number; output: string; } - String: { input: string; output: string; } - Boolean: { input: boolean; output: boolean; } - Int: { input: number; output: number; } - Float: { input: number; output: number; } - MyScalar: { input: Date; output: Date; } - };`); + export type Scalars = { + ID: { input: string | number; output: string; } + String: { input: string; output: string; } + Boolean: { input: boolean; output: boolean; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + MyScalar: { input: Date; output: Date; } + };`); expect(result.content).toBeSimilarStringTo(` - export type MyType = { - __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['MyScalar']; - };`); + export type MyType = { + __typename?: 'MyType'; + foo?: Maybe; + bar: Scalars['MyScalar']['output']; + buz: Scalars['MyScalar']['output']; + };`); + expect(result.content).toBeSimilarStringTo(` + export type MyTypeBuzArgs = { + input: Scalars['MyScalar']['input']; + }`); validateTs(result); }); it('Should generate a scalars mapping correctly for custom scalars with input/output mapping', async () => { - const schema = buildSchema(` - scalar MyScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar - type MyType { - foo: String - bar: MyScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + buz(input: MyScalar!): MyScalar! + } + `); const result = (await plugin( schema, [], @@ -2031,32 +2110,38 @@ describe('TypeScript', () => { )) as Types.ComplexPluginOutput; expect(result.content).toBeSimilarStringTo(` - export type Scalars = { - ID: { input: string | number; output: string; } - String: { input: string; output: string; } - Boolean: { input: boolean; output: boolean; } - Int: { input: number; output: number; } - Float: { input: number; output: number; } - MyScalar: { input: bigint; output: number | bigint; } - };`); + export type Scalars = { + ID: { input: string | number; output: string; } + String: { input: string; output: string; } + Boolean: { input: boolean; output: boolean; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + MyScalar: { input: bigint; output: number | bigint; } + };`); expect(result.content).toBeSimilarStringTo(` - export type MyType = { - __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['MyScalar']; - };`); + export type MyType = { + __typename?: 'MyType'; + foo?: Maybe; + bar: Scalars['MyScalar']['output']; + buz: Scalars['MyScalar']['output']; + };`); + expect(result.content).toBeSimilarStringTo(` + export type MyTypeBuzArgs = { + input: Scalars['MyScalar']['input']; + }`); validateTs(result); }); it('Should correctly throw an error when an unknown scalar is detected while using `strictScalars`', () => { - const schema = buildSchema(` - scalar MyScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar - type MyType { - foo: String - bar: MyScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + } + `); expect(() => { plugin(schema, [], { strictScalars: true }, { outputFile: '' }); @@ -2064,13 +2149,15 @@ describe('TypeScript', () => { }); it('Should allow overriding default scalar type', async () => { - const schema = buildSchema(` - scalar MyScalar + const schema = buildSchema(/* GraphQL */ ` + scalar MyScalar - type MyType { - foo: String - bar: MyScalar! - }`); + type MyType { + foo: String + bar: MyScalar! + buz(input: MyScalar!): MyScalar! + } + `); const result = (await plugin( schema, [], @@ -2079,21 +2166,26 @@ describe('TypeScript', () => { )) as Types.ComplexPluginOutput; expect(result.content).toBeSimilarStringTo(` - export type Scalars = { - ID: { input: string | number; output: string; } - String: { input: string; output: string; } - Boolean: { input: boolean; output: boolean; } - Int: { input: number; output: number; } - Float: { input: number; output: number; } - MyScalar: { input: unknown; output: unknown; } - };`); + export type Scalars = { + ID: { input: string | number; output: string; } + String: { input: string; output: string; } + Boolean: { input: boolean; output: boolean; } + Int: { input: number; output: number; } + Float: { input: number; output: number; } + MyScalar: { input: unknown; output: unknown; } + };`); expect(result.content).toBeSimilarStringTo(` - export type MyType = { - __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['MyScalar']; - };`); + export type MyType = { + __typename?: 'MyType'; + foo?: Maybe; + bar: Scalars['MyScalar']['output']; + buz: Scalars['MyScalar']['output']; + };`); + expect(result.content).toBeSimilarStringTo(` + export type MyTypeBuzArgs = { + input: Scalars['MyScalar']['input']; + }`); validateTs(result); }); @@ -2130,25 +2222,26 @@ describe('TypeScript', () => { describe('Object (type)', () => { it('Should build type correctly', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type MyType { foo: String bar: String! - }`); + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyType = { __typename?: 'MyType'; - foo?: Maybe; - bar: Scalars['String']; + foo?: Maybe; + bar: Scalars['String']['output']; }; `); validateTs(result); }); it('Should build type correctly when implementing interface', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` interface MyInterface { foo: String! } @@ -2156,25 +2249,25 @@ describe('TypeScript', () => { type MyType implements MyInterface { foo: String! } - `); + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyInterface = { - foo: Scalars['String']; + foo: Scalars['String']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type MyType = MyInterface & { __typename?: 'MyType'; - foo: Scalars['String']; + foo: Scalars['String']['output']; }; `); validateTs(result); }); it('Should build type correctly when implementing multiple interfaces', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` interface MyInterface { foo: String! } @@ -2187,24 +2280,24 @@ describe('TypeScript', () => { foo: String! bar: String! } - `); + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyInterface = { - foo: Scalars['String']; + foo: Scalars['String']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type MyOtherInterface = { - bar: Scalars['String']; + bar: Scalars['String']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type MyType = MyInterface & MyOtherInterface & { __typename?: 'MyType'; - foo: Scalars['String']; - bar: Scalars['String']; + foo: Scalars['String']['output']; + bar: Scalars['String']['output']; }; `); validateTs(result); @@ -2222,7 +2315,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInterface = { - foo: Scalars['String']; + foo: Scalars['String']['output']; }; `); expect(result.content).toBeSimilarStringTo(` @@ -2234,7 +2327,7 @@ describe('TypeScript', () => { }); it('Should build type correctly with links between types', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type MyType { foo: MyOtherType! } @@ -2242,7 +2335,7 @@ describe('TypeScript', () => { type MyOtherType { bar: String! } - `); + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` @@ -2254,14 +2347,14 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyOtherType = { __typename?: 'MyOtherType'; - bar: Scalars['String']; + bar: Scalars['String']['output']; }; `); validateTs(result); }); it('Should build type correctly when wrapping field definitions', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` interface MyInterface { foo: String! } @@ -2269,7 +2362,7 @@ describe('TypeScript', () => { type MyType implements MyInterface { foo: String! } - `); + `); const result = (await plugin( schema, [], @@ -2279,20 +2372,20 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInterface = { - foo: FieldWrapper; + foo: FieldWrapper; }; `); expect(result.content).toBeSimilarStringTo(` export type MyType = MyInterface & { __typename?: 'MyType'; - foo: FieldWrapper; + foo: FieldWrapper; }; `); validateTs(result); }); it('Should build list type correctly when wrapping field definitions', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type ListOfStrings { foo: [String!]! } @@ -2311,14 +2404,14 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type ListOfStrings = { __typename?: 'ListOfStrings'; - foo: Array>; + foo: Array>; }; `); expect(result.content).toBeSimilarStringTo(` export type ListOfMaybeStrings = { __typename?: 'ListOfMaybeStrings'; - foo: Array>>; + foo: Array>>; }; `); @@ -2326,7 +2419,7 @@ describe('TypeScript', () => { }); it('Should build list type correctly when wrapping entire field definitions', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type ListOfStrings { foo: [String!]! } @@ -2345,14 +2438,14 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type ListOfStrings = { __typename?: 'ListOfStrings'; - foo: EntireFieldWrapper>; + foo: EntireFieldWrapper>; }; `); expect(result.content).toBeSimilarStringTo(` export type ListOfMaybeStrings = { __typename?: 'ListOfMaybeStrings'; - foo: EntireFieldWrapper>>; + foo: EntireFieldWrapper>>; }; `); @@ -2360,7 +2453,7 @@ describe('TypeScript', () => { }); it('Should build list type correctly when wrapping both field definitions and entire field definitions', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` type ListOfStrings { foo: [String!]! } @@ -2379,14 +2472,14 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type ListOfStrings = { __typename?: 'ListOfStrings'; - foo: EntireFieldWrapper>>; + foo: EntireFieldWrapper>>; }; `); expect(result.content).toBeSimilarStringTo(` export type ListOfMaybeStrings = { __typename?: 'ListOfMaybeStrings'; - foo: EntireFieldWrapper>>>; + foo: EntireFieldWrapper>>>; }; `); @@ -2394,11 +2487,11 @@ describe('TypeScript', () => { }); it('Should not wrap input type fields', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` input MyInput { foo: String! } - `); + `); const result = (await plugin( schema, [], @@ -2408,7 +2501,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInput = { - foo: Scalars['String']; + foo: Scalars['String']['input']; }; `); validateTs(result); @@ -2474,17 +2567,18 @@ describe('TypeScript', () => { describe('Interface', () => { it('Should build interface correctly', async () => { - const schema = buildSchema(` + const schema = buildSchema(/* GraphQL */ ` interface MyInterface { foo: String bar: String! - }`); + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyInterface = { - foo?: Maybe; - bar: Scalars['String']; + foo?: Maybe; + bar: Scalars['String']['output']; }; `); validateTs(result); @@ -2540,7 +2634,7 @@ describe('TypeScript', () => { export type Query = { __typename?: 'Query'; - myField?: Maybe; + myField?: Maybe; }; export type QueryMyFieldArgs = { @@ -2618,7 +2712,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyInput = { - id: Scalars['ID']; + id: Scalars['ID']['input']; }; `); validateTs(result); @@ -2646,7 +2740,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type Input = - { int: Scalars['Int']; }; + { int: Scalars['Int']['input']; }; `); }); @@ -2668,8 +2762,8 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type Input = - { int: Scalars['Int']; boolean?: never; } - | { int?: never; boolean: Scalars['Boolean']; }; + { int: Scalars['Int']['input']; boolean?: never; } + | { int?: never; boolean: Scalars['Boolean']['input']; }; `); }); @@ -2690,7 +2784,7 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export interface Input { - int: Scalars['Int']; + int: Scalars['Int']['input']; } `); }); @@ -2713,8 +2807,8 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type Input = - { int: Scalars['Int']; boolean?: never; } - | { int?: never; boolean: Scalars['Boolean']; }; + { int: Scalars['Int']['input']; boolean?: never; } + | { int?: never; boolean: Scalars['Boolean']['input']; }; `); }); @@ -2762,8 +2856,8 @@ describe('TypeScript', () => { const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type Input = - { int: Scalars['Int']; boolean?: never; } - | { int?: never; boolean: Scalars['Boolean']; }; + { int: Scalars['Int']['input']; boolean?: never; } + | { int?: never; boolean: Scalars['Boolean']['input']; }; `); }); @@ -2787,8 +2881,8 @@ describe('TypeScript', () => { const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type Input = - { int: Scalars['Int']; boolean?: never; } - | { int?: never; boolean: Scalars['Boolean']; }; + { int: Scalars['Int']['input']; boolean?: never; } + | { int?: never; boolean: Scalars['Boolean']['input']; }; `); }); }); @@ -2796,7 +2890,11 @@ describe('TypeScript', () => { describe('Naming Convention & Types Prefix', () => { it('Should use custom namingConvention for type name and args typename', async () => { - const schema = buildSchema(`type MyType { foo(a: String!, b: String, c: [String], d: [Int!]!): String }`); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo(a: String!, b: String, c: [String], d: [Int!]!): String + } + `); const result = (await plugin( schema, [], @@ -2806,16 +2904,16 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type mytypefooargs = { - a: Scalars['String']; - b?: InputMaybe; - c?: InputMaybe>>; - d: Array; + a: Scalars['String']['input']; + b?: InputMaybe; + c?: InputMaybe>>; + d: Array; }; `); expect(result.content).toBeSimilarStringTo(` export type mytype = { __typename?: 'MyType'; - foo?: Maybe; + foo?: Maybe; }; `); @@ -2823,7 +2921,11 @@ describe('TypeScript', () => { }); it('Should use custom namingConvention and add custom prefix', async () => { - const schema = buildSchema(`type MyType { foo(a: String!, b: String, c: [String], d: [Int!]!): String }`); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo(a: String!, b: String, c: [String], d: [Int!]!): String + } + `); const result = (await plugin( schema, [], @@ -2833,17 +2935,17 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type Imytypefooargs = { - a: Scalars['String']; - b?: InputMaybe; - c?: InputMaybe>>; - d: Array; + a: Scalars['String']['input']; + b?: InputMaybe; + c?: InputMaybe>>; + d: Array; }; `); expect(result.content).toBeSimilarStringTo(` export type Imytype = { __typename?: 'MyType'; - foo?: Maybe; + foo?: Maybe; }; `); @@ -2875,47 +2977,47 @@ describe('TypeScript', () => { validateTs(result); }); - const schema = buildSchema(` - enum MyEnum { - A - B - C - } + const schema = buildSchema(/* GraphQL */ ` + enum MyEnum { + A + B + C + } - type MyType { - f: String - bar: MyEnum - b_a_r: String - myOtherField: String - } + type MyType { + f: String + bar: MyEnum + b_a_r: String + myOtherField: String + } - type My_Type { - linkTest: MyType - } + type My_Type { + linkTest: MyType + } - union MyUnion = My_Type | MyType + union MyUnion = My_Type | MyType - interface Some_Interface { - id: ID! - } + interface Some_Interface { + id: ID! + } - type Impl1 implements Some_Interface { - id: ID! - } + type Impl1 implements Some_Interface { + id: ID! + } - type Impl_2 implements Some_Interface { - id: ID! - } + type Impl_2 implements Some_Interface { + id: ID! + } - type impl_3 implements Some_Interface { - id: ID! - } + type impl_3 implements Some_Interface { + id: ID! + } - type Query { - something: MyUnion - use_interface: Some_Interface - } - `); + type Query { + something: MyUnion + use_interface: Some_Interface + } + `); it('Should generate correct values when using links between types - lowerCase', async () => { const result = (await plugin( @@ -2935,10 +3037,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type mytype = { __typename?: 'MyType'; - f?: Maybe; + f?: Maybe; bar?: Maybe; - b_a_r?: Maybe; - myOtherField?: Maybe; + b_a_r?: Maybe; + myOtherField?: Maybe; }; `); expect(result.content).toBeSimilarStringTo(` @@ -2952,25 +3054,25 @@ describe('TypeScript', () => { `); expect(result.content).toBeSimilarStringTo(` export type some_interface = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type impl1 = some_interface & { __typename?: 'Impl1'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type impl_2 = some_interface & { __typename?: 'Impl_2'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type impl_3 = some_interface & { __typename?: 'impl_3'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` @@ -2997,10 +3099,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyType = { __typename?: 'MyType'; - f?: Maybe; + f?: Maybe; bar?: Maybe; - b_a_r?: Maybe; - myOtherField?: Maybe; + b_a_r?: Maybe; + myOtherField?: Maybe; }; `); expect(result.content).toBeSimilarStringTo(` @@ -3014,25 +3116,25 @@ describe('TypeScript', () => { `); expect(result.content).toBeSimilarStringTo(` export type Some_Interface = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type Impl1 = Some_Interface & { __typename?: 'Impl1'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type Impl_2 = Some_Interface & { __typename?: 'Impl_2'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type Impl_3 = Some_Interface & { __typename?: 'impl_3'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` @@ -3059,10 +3161,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type IMyType = { __typename?: 'MyType'; - f?: Maybe; + f?: Maybe; bar?: Maybe; - b_a_r?: Maybe; - myOtherField?: Maybe; + b_a_r?: Maybe; + myOtherField?: Maybe; };`); expect(result.content).toBeSimilarStringTo(` export type IMy_Type = { @@ -3073,25 +3175,25 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(`export type IMyUnion = IMy_Type | IMyType;`); expect(result.content).toBeSimilarStringTo(` export type ISome_Interface = { - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type IImpl1 = ISome_Interface & { __typename?: 'Impl1'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type IImpl_2 = ISome_Interface & { __typename?: 'Impl_2'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` export type IImpl_3 = ISome_Interface & { __typename?: 'impl_3'; - id: Scalars['ID']; + id: Scalars['ID']['output']; }; `); expect(result.content).toBeSimilarStringTo(` @@ -3108,16 +3210,20 @@ describe('TypeScript', () => { describe('Arguments', () => { it('Should generate correctly types for field arguments - with basic fields', async () => { - const schema = buildSchema(`type MyType { foo(a: String!, b: String, c: [String], d: [Int!]!): String }`); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo(a: String!, b: String, c: [String], d: [Int!]!): String + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyTypeFooArgs = { - a: Scalars['String']; - b?: InputMaybe; - c?: InputMaybe>>; - d: Array; + a: Scalars['String']['input']; + b?: InputMaybe; + c?: InputMaybe>>; + d: Array; }; `); @@ -3125,17 +3231,19 @@ describe('TypeScript', () => { }); it('Should generate correctly types for field arguments - with default value', async () => { - const schema = buildSchema( - `type MyType { foo(a: String = "default", b: String! = "default", c: String, d: String!): String }` - ); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo(a: String = "default", b: String! = "default", c: String, d: String!): String + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyTypeFooArgs = { - a?: InputMaybe; - b?: Scalars['String']; - c?: InputMaybe; - d: Scalars['String']; + a?: InputMaybe; + b?: Scalars['String']['input']; + c?: InputMaybe; + d: Scalars['String']['input']; }; `); @@ -3143,9 +3251,11 @@ describe('TypeScript', () => { }); it('Should generate correctly types for field arguments - with default value and avoidOptionals option set to true', async () => { - const schema = buildSchema( - `type MyType { foo(a: String = "default", b: String! = "default", c: String, d: String!): String }` - ); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo(a: String = "default", b: String! = "default", c: String, d: String!): String + } + `); const result = (await plugin( schema, [], @@ -3155,10 +3265,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyTypeFooArgs = { - a?: InputMaybe; - b?: Scalars['String']; - c: InputMaybe; - d: Scalars['String']; + a?: InputMaybe; + b?: Scalars['String']['input']; + c: InputMaybe; + d: Scalars['String']['input']; }; `); @@ -3166,9 +3276,14 @@ describe('TypeScript', () => { }); it('Should generate correctly types for field arguments - with input type', async () => { - const schema = buildSchema( - `input MyInput { f: String } type MyType { foo(a: MyInput, b: MyInput!, c: [MyInput], d: [MyInput]!, e: [MyInput!]!): String }` - ); + const schema = buildSchema(/* GraphQL */ ` + input MyInput { + f: String + } + type MyType { + foo(a: MyInput, b: MyInput!, c: [MyInput], d: [MyInput]!, e: [MyInput!]!): String + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` @@ -3185,23 +3300,30 @@ describe('TypeScript', () => { }); it('Should add custom prefix for mutation arguments', async () => { - const schema = buildSchema(`input Input { name: String } type Mutation { foo(id: ID, input: Input): String }`); + const schema = buildSchema(/* GraphQL */ ` + input Input { + name: String + } + type Mutation { + foo(id: ID, input: Input): String + } + `); const result = await plugin(schema, [], { typesPrefix: 'T' }, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type TInput = { - name?: InputMaybe; + name?: InputMaybe; }; `); expect(result.content).toBeSimilarStringTo(` export type TMutation = { __typename?: 'Mutation'; - foo?: Maybe; + foo?: Maybe; }; export type TMutationFooArgs = { - id?: InputMaybe; + id?: InputMaybe; input?: InputMaybe; }; `); @@ -3228,25 +3350,30 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type NodeTextArgs = { - arg1: Scalars['String']; - arg2?: InputMaybe; + arg1: Scalars['String']['input']; + arg2?: InputMaybe; }; `); await validateTs(result); }); it('Should generate correctly types for inputs with default value - #4273', async () => { - const schema = buildSchema( - `input MyInput { a: String = "default", b: String! = "default", c: String, d: String! }` - ); + const schema = buildSchema(/* GraphQL */ ` + input MyInput { + a: String = "default" + b: String! = "default" + c: String + d: String! + } + `); const result = await plugin(schema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyInput = { - a?: InputMaybe; - b?: Scalars['String']; - c?: InputMaybe; - d: Scalars['String']; + a?: InputMaybe; + b?: Scalars['String']['input']; + c?: InputMaybe; + d: Scalars['String']['input']; }; `); @@ -3254,17 +3381,22 @@ describe('TypeScript', () => { }); it('Should generate correctly types for inputs with default value and avoidOptionals.defaultValue set to true - #5112', async () => { - const schema = buildSchema( - `input MyInput { a: String = "default", b: String! = "default", c: String, d: String! }` - ); + const schema = buildSchema(/* GraphQL */ ` + input MyInput { + a: String = "default" + b: String! = "default" + c: String + d: String! + } + `); const result = await plugin(schema, [], { avoidOptionals: { defaultValue: true } }, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyInput = { - a?: InputMaybe; - b: Scalars['String']; - c?: InputMaybe; - d: Scalars['String']; + a?: InputMaybe; + b: Scalars['String']['input']; + c?: InputMaybe; + d: Scalars['String']['input']; }; `); @@ -3272,9 +3404,11 @@ describe('TypeScript', () => { }); it('Should generate correctly types for field arguments with default value and avoidOptionals.defaultValue option set to true - #5112', async () => { - const schema = buildSchema( - `type MyType { foo(a: String = "default", b: String! = "default", c: String, d: String!): String }` - ); + const schema = buildSchema(/* GraphQL */ ` + type MyType { + foo(a: String = "default", b: String! = "default", c: String, d: String!): String + } + `); const result = (await plugin( schema, [], @@ -3284,10 +3418,10 @@ describe('TypeScript', () => { expect(result.content).toBeSimilarStringTo(` export type MyTypeFooArgs = { - a?: InputMaybe; - b: Scalars['String']; - c?: InputMaybe; - d: Scalars['String']; + a?: InputMaybe; + b: Scalars['String']['input']; + c?: InputMaybe; + d: Scalars['String']['input']; }; `); @@ -3625,13 +3759,13 @@ describe('TypeScript', () => { // Filter.contain should be optional expect(output.content).toBeSimilarStringTo(` export type Filter = { - contain?: InputMaybe; + contain?: InputMaybe; }; `); // filter should be non-optional expect(output.content).toBeSimilarStringTo(` export type QueryListArgs = { - after?: InputMaybe; + after?: InputMaybe; orderBy?: InputMaybe; filter: Filter; }; diff --git a/packages/presets/client/tests/client-preset.spec.ts b/packages/presets/client/tests/client-preset.spec.ts index 2d3c0dbac893..cca520581cb4 100644 --- a/packages/presets/client/tests/client-preset.spec.ts +++ b/packages/presets/client/tests/client-preset.spec.ts @@ -356,9 +356,9 @@ export * from "./gql";`); export type Query = { __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -486,9 +486,9 @@ export * from "./gql";`); export type Query = { __typename: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -599,7 +599,7 @@ export * from "./gql";`); export type Query = { __typename?: 'Query'; - a?: Maybe; + a?: Maybe; }; export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1265,9 +1265,9 @@ export * from "./gql.js";`); export type Query = { __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type BbbQueryVariables = Exact<{ [key: string]: never; }>; @@ -1343,9 +1343,9 @@ export * from "./gql.js";`); export type Query = { __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1424,9 +1424,9 @@ export * from "./gql.js";`); export type Query = { __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1505,9 +1505,9 @@ export * from "./gql.js";`); export type Query = { __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1588,9 +1588,9 @@ export * from "./gql.js";`); export type Query = { __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; + a?: Maybe; + b?: Maybe; + c?: Maybe; }; export type AaaQueryVariables = Exact<{ [key: string]: never; }>; @@ -1679,7 +1679,7 @@ export * from "./gql.js";`); export type A = { __typename?: 'A'; a: A; - b: Scalars['String']; + b: Scalars['String']['output']; }; export type Query = { @@ -1756,7 +1756,7 @@ export * from "./gql.js";`); export type Foo = { __typename?: 'Foo'; - value?: Maybe; + value?: Maybe; }; export type Query = { @@ -1836,7 +1836,7 @@ export * from "./gql.js";`); export type Foo = { __typename?: 'Foo'; - value?: Maybe; + value?: Maybe; }; export type Query = { @@ -1916,7 +1916,7 @@ export * from "./gql.js";`); export type Foo = { __typename?: 'Foo'; - value?: Maybe; + value?: Maybe; }; export type Query = { @@ -2033,7 +2033,7 @@ export * from "./gql.js";`); export type Foo = { __typename?: 'Foo'; - value?: Maybe; + value?: Maybe; }; export type Query = { @@ -2149,7 +2149,7 @@ export * from "./gql.js";`); export type Foo = { __typename?: 'Foo'; - value?: Maybe; + value?: Maybe; }; export type Query = {