Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scalar input/output object bug when given external file or module pattern #9418

Merged
merged 10 commits into from
May 23, 2023
10 changes: 2 additions & 8 deletions packages/plugins/other/visitor-plugin-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,8 @@ export function buildScalars(
const mappedScalar = scalarsMapping[name];
if (typeof mappedScalar === 'object' && mappedScalar.input && mappedScalar.output) {
result[name] = {
input: {
isExternal: false,
type: mappedScalar.input,
},
output: {
isExternal: false,
type: mappedScalar.output,
},
input: parseMapper(mappedScalar.input, name),
output: parseMapper(mappedScalar.output, name),
};
} else {
result[name] = {
Expand Down
118 changes: 118 additions & 0 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1961,13 +1961,19 @@ describe('TypeScript', () => {
scalar MyScalar
scalar MyOtherScalar
scalar MyAliasedScalar
scalar OrgScalar
scalar OrgOtherScalar
scalar OrgAliasedScalar

type MyType {
foo: String
bar: MyScalar!
baz: MyOtherScalar!
qux: MyAliasedScalar!
tux(in: MyScalar!): MyScalar!
ay: OrgScalar!
bee: OrgOtherScalar!
ce: OrgAliasedScalar!
}
`);
const result = (await plugin(
Expand All @@ -1978,6 +1984,9 @@ describe('TypeScript', () => {
MyScalar: '../../scalars#default',
MyOtherScalar: '../../scalars#MyOtherScalar',
MyAliasedScalar: '../../scalars#MyAliasedScalar as AliasedScalar',
OrgScalar: '@org/scalars#default',
OrgOtherScalar: '@org/scalars#OrgOtherScalar',
OrgAliasedScalar: '@org/scalars#OrgOtherScalar as OrgAliasedScalar',
},
},
{ outputFile: '' }
Expand All @@ -1987,6 +1996,9 @@ describe('TypeScript', () => {
expect(result.prepend).toContain(`import MyScalar from '../../scalars';`);
expect(result.prepend).toContain(`import { MyOtherScalar } from '../../scalars';`);
expect(result.prepend).toContain(`import { MyAliasedScalar as AliasedScalar } from '../../scalars';`);
expect(result.prepend).toContain(`import OrgScalar from '@org/scalars';`);
expect(result.prepend).toContain(`import { OrgOtherScalar } from '@org/scalars';`);
expect(result.prepend).toContain(`import { OrgOtherScalar as OrgAliasedScalar } from '@org/scalars';`);
expect(result.content).toBeSimilarStringTo(`
export type Scalars = {
ID: { input: string | number; output: string; }
Expand All @@ -1997,6 +2009,9 @@ describe('TypeScript', () => {
MyScalar: { input: MyScalar['input']; output: MyScalar['output']; }
MyOtherScalar: { input: MyOtherScalar['input']; output: MyOtherScalar['output']; }
MyAliasedScalar: { input: AliasedScalar['input']; output: AliasedScalar['output']; }
OrgScalar: { input: OrgScalar['input']; output: OrgScalar['output']; }
OrgOtherScalar: { input: OrgOtherScalar['input']; output: OrgOtherScalar['output']; }
OrgAliasedScalar: { input: OrgAliasedScalar['input']; output: OrgAliasedScalar['output']; }
Copy link
Collaborator Author

@eddeee888 eddeee888 May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would have much better backward compatibility if we don't require mapped scalar from external sources to be an input/output object

Will push a commit to fix this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2665abf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've already done it, but agreed, this last commit is quite nice. Means the only code change we had to do in our (internal) project was stop using Scalars[''] or add the ['input'] / ['output'] type.

};`);

expect(result.content).toBeSimilarStringTo(`
Expand All @@ -2007,6 +2022,109 @@ describe('TypeScript', () => {
baz: Scalars['MyOtherScalar']['output'];
qux: Scalars['MyAliasedScalar']['output'];
tux: Scalars['MyScalar']['output'];
ay: Scalars['OrgScalar']['output'];
bee: Scalars['OrgOtherScalar']['output'];
ce: Scalars['OrgAliasedScalar']['output'];
};`);
expect(result.content).toBeSimilarStringTo(`
export type MyTypeTuxArgs = {
in: Scalars['MyScalar']['input'];
}`);
validateTs(result);
});

it('Should import a type of a mapped scalar for input/output mapping', async () => {
const schema = buildSchema(/* GraphQL */ `
scalar MyScalar
scalar MyOtherScalar
scalar MyAliasedScalar
scalar OrgScalar
scalar OrgOtherScalar
scalar OrgAliasedScalar

type MyType {
foo: String
bar: MyScalar!
baz: MyOtherScalar!
qux: MyAliasedScalar!
tux(in: MyScalar!): MyScalar!
ay: OrgScalar!
bee: OrgOtherScalar!
ce: OrgAliasedScalar!
}
`);
const result = (await plugin(
schema,
[],
{
scalars: {
MyScalar: {
input: '../../scalarsInput#default as MyScalarInput',
output: '../../scalarsOutput#default as MyScalarOutput',
},
MyOtherScalar: {
input: '../../scalars#MyOtherScalarInput',
output: '../../scalars#MyOtherScalarOutput',
},
MyAliasedScalar: {
input: '../../scalars#MyAliasedScalar as AliasedScalarInput',
output: '../../scalars#MyAliasedScalar as AliasedScalarOutput',
},
OrgScalar: {
input: '@org/scalars-input#default as OrgScalarInput',
output: '@org/scalars-output#default as OrgScalarOutput',
},
OrgOtherScalar: {
input: '@org/scalars#OrgOtherScalarInput',
output: '@org/scalars#OrgOtherScalarOutput',
},
OrgAliasedScalar: {
input: '@org/scalars#OrgOtherScalar as OrgAliasedScalarInput',
output: '@org/scalars#OrgOtherScalar as OrgAliasedScalarOutput',
},
},
},
{ outputFile: '' }
)) as Types.ComplexPluginOutput;

expect(result.prepend).toContain(`import MyScalarInput from '../../scalarsInput';`);
expect(result.prepend).toContain(`import MyScalarOutput from '../../scalarsOutput';`);
expect(result.prepend).toContain(`import { MyOtherScalarInput } from '../../scalars';`);
expect(result.prepend).toContain(`import { MyOtherScalarOutput } from '../../scalars';`);
expect(result.prepend).toContain(`import { MyAliasedScalar as AliasedScalarInput } from '../../scalars';`);
expect(result.prepend).toContain(`import { MyAliasedScalar as AliasedScalarOutput } from '../../scalars';`);
expect(result.prepend).toContain(`import OrgScalarInput from '@org/scalars-input';`);
expect(result.prepend).toContain(`import OrgScalarOutput from '@org/scalars-output';`);
expect(result.prepend).toContain(`import { OrgOtherScalarInput } from '@org/scalars';`);
expect(result.prepend).toContain(`import { OrgOtherScalarOutput } from '@org/scalars';`);
expect(result.prepend).toContain(`import { OrgOtherScalar as OrgAliasedScalarInput } from '@org/scalars';`);
expect(result.prepend).toContain(`import { OrgOtherScalar as OrgAliasedScalarOutput } from '@org/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: MyScalarInput; output: MyScalarOutput; }
MyOtherScalar: { input: MyOtherScalarInput; output: MyOtherScalarOutput; }
MyAliasedScalar: { input: AliasedScalarInput; output: AliasedScalarOutput; }
OrgScalar: { input: OrgScalarInput; output: OrgScalarOutput; }
OrgOtherScalar: { input: OrgOtherScalarInput; output: OrgOtherScalarOutput; }
OrgAliasedScalar: { input: OrgAliasedScalarInput; output: OrgAliasedScalarOutput; }
};`);

expect(result.content).toBeSimilarStringTo(`
export type MyType = {
__typename?: 'MyType';
foo?: Maybe<Scalars['String']['output']>;
bar: Scalars['MyScalar']['output'];
baz: Scalars['MyOtherScalar']['output'];
qux: Scalars['MyAliasedScalar']['output'];
tux: Scalars['MyScalar']['output'];
ay: Scalars['OrgScalar']['output'];
bee: Scalars['OrgOtherScalar']['output'];
ce: Scalars['OrgAliasedScalar']['output'];
};`);
expect(result.content).toBeSimilarStringTo(`
export type MyTypeTuxArgs = {
Expand Down