Skip to content

Commit

Permalink
Use Scalars input/output in base types and document types
Browse files Browse the repository at this point in the history
  • Loading branch information
eddeee888 committed May 7, 2023
1 parent 436a554 commit 132d828
Show file tree
Hide file tree
Showing 12 changed files with 712 additions and 578 deletions.
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/tests/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scalars['String']>`);
expect(output[0].content).toContain(`hello?: Maybe<Scalars['String']['output']>`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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}>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}>;
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -377,18 +377,18 @@ export type Query = {
};
export type Concept = {
id?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']['output']>;
};
export type Dimension = Concept & {
__typename?: 'Dimension';
id?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']['output']>;
};
export type DimValue = {
__typename?: 'DimValue';
dimension?: Maybe<Dimension>;
value: Scalars['String'];
value: Scalars['String']['output'];
};
export type Searchable = Dimension | DimValue;
Expand Down Expand Up @@ -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<AdditionalInfo>;
};
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;
Expand Down Expand Up @@ -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<AdditionalInfo>;
};
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<Scalars['String']>;
test2?: Maybe<Scalars['String']>;
id: Scalars['ID']['output'];
login: Scalars['String']['output'];
test?: Maybe<Scalars['String']['output']>;
test2?: Maybe<Scalars['String']['output']>;
};
export type UserResult = User | Error2 | Error3;
Expand Down
50 changes: 25 additions & 25 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -2823,14 +2823,14 @@ describe('TypeScript Operations Plugin', () => {

expect(content).toBeSimilarStringTo(
`export type TestQueryQueryVariables = Exact<{
username?: InputMaybe<Scalars['String']>;
email?: InputMaybe<Scalars['String']>;
password: Scalars['String'];
username?: InputMaybe<Scalars['String']['input']>;
email?: InputMaybe<Scalars['String']['input']>;
password: Scalars['String']['input'];
input?: InputMaybe<InputType>;
mandatoryInput: InputType;
testArray?: InputMaybe<Array<InputMaybe<Scalars['String']>> | InputMaybe<Scalars['String']>>;
requireString: Array<InputMaybe<Scalars['String']>> | InputMaybe<Scalars['String']>;
innerRequired: Array<Scalars['String']> | Scalars['String'];
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'];
}>;`
);
await validate(content, config, schema);
Expand All @@ -2849,7 +2849,7 @@ describe('TypeScript Operations Plugin', () => {

expect(content).toBeSimilarStringTo(
`export type TestQueryQueryVariables = Exact<{
test?: InputMaybe<Scalars['DateTime']>;
test?: InputMaybe<Scalars['DateTime']['input']>;
}>;`
);
await validate(content, config);
Expand Down Expand Up @@ -3203,7 +3203,7 @@ describe('TypeScript Operations Plugin', () => {

expect(content).toBeSimilarStringTo(`
export type UsersQueryVariables = Exact<{
reverse?: InputMaybe<Scalars['Boolean']>;
reverse?: InputMaybe<Scalars['Boolean']['input']>;
}>;
`);
});
Expand Down Expand Up @@ -4066,18 +4066,18 @@ describe('TypeScript Operations Plugin', () => {
};
export type Concept = {
id?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']['output']>;
};
export type Dimension = Concept & {
__typename?: 'Dimension';
id?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']['output']>;
};
export type DimValue = {
__typename?: 'DimValue';
dimension?: Maybe<Dimension>;
value: Scalars['String'];
value: Scalars['String']['output'];
};
export type Searchable = Dimension | DimValue;
Expand Down Expand Up @@ -4177,7 +4177,7 @@ describe('TypeScript Operations Plugin', () => {
export type Dimension = {
__typename?: 'Dimension';
id?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']['output']>;
};
export type SearchableFragmentFragment = { __typename?: 'Dimension', id?: string | null };
Expand Down Expand Up @@ -4265,7 +4265,7 @@ describe('TypeScript Operations Plugin', () => {
export type Dimension = {
__typename?: 'Dimension';
id?: Maybe<Scalars['String']>;
id?: Maybe<Scalars['String']['output']>;
};
export type SearchPopularQueryVariables = Exact<{ [key: string]: never; }>;
Expand Down Expand Up @@ -5465,9 +5465,9 @@ function test(q: GetEntityBrandDataQuery): void {

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

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
testArray?: InputMaybe<Array<InputMaybe<Scalars['String']>>>;
requireString: Array<InputMaybe<Scalars['String']>>;
innerRequired: Array<Scalars['String']>;
testArray?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
requireString: Array<InputMaybe<Scalars['String']['input']>>;
innerRequired: Array<Scalars['String']['input']>;
}>;`);
await validate(content, config);
});
Expand Down Expand Up @@ -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<string> | null, parents?: Array<User> } };`);
Expand Down Expand Up @@ -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 }> } };`);
});
Expand Down Expand Up @@ -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 = (
Expand Down
Loading

0 comments on commit 132d828

Please sign in to comment.