Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import { TypeScriptOperationVariablesToObject as TSOperationVariablesToObject } from '@graphql-codegen/typescript';

const SCALARS = {
ID: 'string',
String: 'string',
Int: 'number',
Float: 'number',
Boolean: 'boolean',
};

const MAYBE_SUFFIX = ' | null';

export class TypeScriptOperationVariablesToObject extends TSOperationVariablesToObject {
protected formatTypeString(fieldType: string, _isNonNullType: boolean, _hasDefaultValue: boolean): string {
return fieldType;
}

protected clearOptional(str: string): string {
if (str?.endsWith(MAYBE_SUFFIX)) {
return (str = str.substring(0, str.length - MAYBE_SUFFIX.length));
}

return str;
}

protected wrapMaybe(type: string): string {
return type?.endsWith(MAYBE_SUFFIX) ? type : `${type}${MAYBE_SUFFIX}`;
}

protected getScalar(name: string): string {
return SCALARS[name] ?? 'any';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ exports[`TypeScript Operations Plugin > Config > should include fragment variabl


export type TextNotificationFragmentFragmentVariables = Exact<{
skip: Scalars['Boolean']['input'];
skip: boolean;
}>;
"
`;

exports[`TypeScript Operations Plugin > Issues > #2699 - Issues with multiple interfaces and unions 1`] = `
"export type GetEntityBrandDataQueryVariables = Exact<{
gid: Scalars['ID']['input'];
brand: Scalars['ID']['input'];
gid: string;
brand: string;
}>;


Expand Down
53 changes: 27 additions & 26 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from 'dedent';
import { mergeOutputs, Types } from '@graphql-codegen/plugin-helpers';
import { validateTs } from '@graphql-codegen/testing';
import { buildClientSchema, buildSchema, parse } from 'graphql';
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('TypeScript Operations Plugin', () => {

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
showProperty: Scalars['Boolean']['input'];
showProperty: boolean;
}> | undefined;
`);
});
Expand Down Expand Up @@ -2481,7 +2482,7 @@ export type Q2Query = { search: Array<

expect(content).toBeSimilarStringTo(
`export type MeQueryVariables = Exact<{
repoFullName: Scalars['String']['input'];
repoFullName: string;
}>;`
);
expect(content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -2847,16 +2848,16 @@ export type Q2Query = { search: Array<
});

expect(content).toBeSimilarStringTo(
`export type TestQueryQueryVariables = Exact<{
username?: InputMaybe<Scalars['String']['input']>;
email?: InputMaybe<Scalars['String']['input']>;
password: Scalars['String']['input'];
input?: InputMaybe<InputType>;
dedent(`export type TestQueryQueryVariables = Exact<{
username?: string | null;
email?: string | null;
password: string;
input?: InputType | null;
mandatoryInput: InputType;
testArray?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>> | InputMaybe<Scalars['String']['input']>>;
requireString: Array<InputMaybe<Scalars['String']['input']>> | InputMaybe<Scalars['String']['input']>;
innerRequired: Array<Scalars['String']['input']> | Scalars['String']['input'];
}>;`
testArray?: Array<string | null> | string | null;
requireString: Array<string | null> | string;
innerRequired: Array<string> | string;
}>;`)
);
await validate(content, config, schema);
});
Expand All @@ -2874,7 +2875,7 @@ export type Q2Query = { search: Array<

expect(content).toBeSimilarStringTo(
`export type TestQueryQueryVariables = Exact<{
test?: InputMaybe<Scalars['DateTime']['input']>;
test?: any | null;
}>;`
);
await validate(content, config);
Expand Down Expand Up @@ -3228,7 +3229,7 @@ export type Q2Query = { search: Array<

expect(content).toBeSimilarStringTo(`
export type UsersQueryVariables = Exact<{
reverse?: InputMaybe<Scalars['Boolean']['input']>;
reverse?: boolean | null;
}>;
`);
});
Expand Down Expand Up @@ -5506,9 +5507,9 @@ function test(q: GetEntityBrandDataQuery): void {

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
testArray?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>> | InputMaybe<Scalars['String']['input']>>;
requireString: Array<InputMaybe<Scalars['String']['input']>> | InputMaybe<Scalars['String']['input']>;
innerRequired: Array<Scalars['String']['input']> | Scalars['String']['input'];
testArray?: Array<string | null> | string | null;
requireString: Array<string | null> | string;
innerRequired: Array<string> | string;
}>;`);
await validate(content, config);
});
Expand Down Expand Up @@ -5538,9 +5539,9 @@ function test(q: GetEntityBrandDataQuery): void {

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
testArray?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
requireString: Array<InputMaybe<Scalars['String']['input']>>;
innerRequired: Array<Scalars['String']['input']>;
testArray?: Array<string | null> | null;
requireString: Array<string | null>;
innerRequired: Array<string>;
}>;`);
await validate(content, config);
});
Expand Down Expand Up @@ -6086,8 +6087,8 @@ function test(q: GetEntityBrandDataQuery): void {

expect(content).toBeSimilarStringTo(`
export type UserQueryQueryVariables = Exact<{
skipFirstName: Scalars['Boolean']['input'];
skipAddress: Scalars['Boolean']['input'];
skipFirstName: boolean;
skipAddress: boolean;
}>;

export type UserQueryQuery = {
Expand Down Expand Up @@ -6145,7 +6146,7 @@ function test(q: GetEntityBrandDataQuery): void {

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
showAddress: Scalars['Boolean']['input'];
showAddress: boolean;
}>;

export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', name: string, address?: string, nicknames?: Array<string> | null, parents?: Array<User> } };`);
Expand Down Expand Up @@ -6198,8 +6199,8 @@ function test(q: GetEntityBrandDataQuery): void {

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
showAddress: Scalars['Boolean']['input'];
showName: Scalars['Boolean']['input'];
showAddress: boolean;
showName: boolean;
}>;
export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', id: string, name?: string, address?: { __typename?: 'Address', city: string }, friends?: Array<{ __typename?: 'User', id: string }> } };`);
});
Expand Down Expand Up @@ -6246,8 +6247,8 @@ function test(q: GetEntityBrandDataQuery): void {

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
showAddress: Scalars['Boolean']['input'];
showName: Scalars['Boolean']['input'];
showAddress: boolean;
showName: boolean;
}>;

export type UserQuery = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('TypeScript Operations Plugin - Standalone', () => {
expect(result).toMatchInlineSnapshot(`
"type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type UserQueryVariables = Exact<{
id: Scalars['ID']['input'];
id: string;
}>;


Expand All @@ -105,8 +105,8 @@ describe('TypeScript Operations Plugin - Standalone', () => {
};

export type UsersWithScalarInputQueryVariables = Exact<{
from: Scalars['DateTime']['input'];
to?: InputMaybe<Scalars['DateTime']['input']>;
from: any;
to?: any | null;
}>;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class TypeScriptOperationVariablesToObject extends OperationVariablesToOb
);
}

private clearOptional(str: string): string {
protected clearOptional(str: string): string {
const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
const rgx = new RegExp(`^${this.wrapMaybe(`(.*?)`)}$`, 'i');

Expand Down
Loading