Skip to content

Commit

Permalink
added a test for #4212
Browse files Browse the repository at this point in the history
adjust ts validation function
remove old dependency for open
  • Loading branch information
dotansimha committed Mar 7, 2021
1 parent 6f483c9 commit 3e514fd
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 66 deletions.
80 changes: 74 additions & 6 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('TypeScript Operations Plugin', () => {
const schema = buildSchema(/* GraphQL */ `
scalar DateTime
input InputType {
t: String
}
type User {
id: ID!
username: String!
Expand Down Expand Up @@ -90,10 +94,10 @@ describe('TypeScript Operations Plugin', () => {
config: any = {},
pluginSchema = schema,
usage = '',
openPlayground = false
suspenseErrors = []
) => {
const m = mergeOutputs([await tsPlugin(pluginSchema, [], config, { outputFile: '' }), content, usage]);
await validateTs(m, null, null, null, openPlayground);
validateTs(m, undefined, undefined, undefined, suspenseErrors);

return m;
};
Expand Down Expand Up @@ -173,7 +177,7 @@ describe('TypeScript Operations Plugin', () => {
)> }
);
`);
await validate(content, config);
await validate(content, config, schema, '', [`Cannot find namespace 'Types'.`]);
});

it('Should handle "namespacedImportName" and "preResolveTypes" together', async () => {
Expand Down Expand Up @@ -215,7 +219,7 @@ describe('TypeScript Operations Plugin', () => {
`export type TestQuery = { __typename?: 'Query', f?: Types.Maybe<Types.E>, user: { __typename?: 'User', id: string, f?: Types.Maybe<Types.E>, j?: Types.Maybe<any> } };`
);

await validate(content, config);
await validate(content, config, schema, '', [`Cannot find namespace 'Types'.`]);
});

it('Should generate the correct output when using immutableTypes config', async () => {
Expand Down Expand Up @@ -1850,7 +1854,7 @@ describe('TypeScript Operations Plugin', () => {
) }
);
`);
await validate(content, config);
await validate(content, config, schema);
});

it('Should generate the correct intersection for fragments when using with interfaces with same type', async () => {
Expand Down Expand Up @@ -2456,7 +2460,7 @@ describe('TypeScript Operations Plugin', () => {
innerRequired: Array<Scalars['String']> | Scalars['String'];
}>;`
);
await validate(content, config);
await validate(content, config, schema);
});

it('Should handle operation variables correctly when they use custom scalars', async () => {
Expand Down Expand Up @@ -3919,6 +3923,70 @@ describe('TypeScript Operations Plugin', () => {
});

describe('Issues', () => {
it('#4212 - Should merge TS arrays in a more elegant way', async () => {
const testSchema = buildSchema(/* GraphQL */ `
type Item {
id: ID!
name: String!
}
type Object {
items: [Item!]!
}
type Query {
obj: Object
}
`);

const query = parse(/* GraphQL */ `
fragment Object1 on Object {
items {
id
}
}
fragment Object2 on Object {
items {
name
}
}
fragment CombinedObject on Object {
...Object1
...Object2
}
query test {
obj {
...CombinedObject
}
}
`);

const { content } = await plugin(
testSchema,
[{ location: '', document: query }],
{},
{
outputFile: 'graphql.ts',
}
);

await validate(
content,
{},
testSchema,
`
function test (t: TestQuery) {
for (const item of t.obj!.items) {
console.log(item.id, item.name, item.__typename);
}
}
`
);
});

it('#5422 - Error when interface doesnt have implemeting types', async () => {
const testSchema = buildSchema(/* GraphQL */ `
interface A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ describe('React Apollo', () => {
output: Types.PluginOutput,
testSchema: GraphQLSchema,
documents: Types.DocumentFile[],
config: any,
playground = false
config: any
) => {
const tsOutput = await tsPlugin(testSchema, documents, config, { outputFile: '' });
const tsDocumentsOutput = await tsDocumentsPlugin(testSchema, documents, config, { outputFile: '' });
const merged = mergeOutputs([tsOutput, tsDocumentsOutput, output]);
validateTs(merged, undefined, true, false, playground);
validateTs(merged, undefined, true, false);

return merged;
};
Expand Down
23 changes: 11 additions & 12 deletions packages/plugins/typescript/react-query/tests/react-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ const validateTypeScript = async (
output: Types.PluginOutput,
testSchema: GraphQLSchema,
documents: Types.DocumentFile[],
config: any,
playground = false
config: any
) => {
const tsOutput = await tsPlugin(testSchema, documents, config, { outputFile: '' });
const tsDocumentsOutput = await tsDocumentsPlugin(testSchema, documents, config, { outputFile: '' });
const merged = mergeOutputs([tsOutput, tsDocumentsOutput, output]);
validateTs(merged, undefined, true, false, playground);
validateTs(merged, undefined, true, false);

return merged;
};
Expand Down Expand Up @@ -118,7 +117,7 @@ describe('React-Query', () => {
);`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});

it('Should generate query correctly with internal mapper', async () => {
Expand Down Expand Up @@ -155,7 +154,7 @@ describe('React-Query', () => {
);`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});

it('Should generate mutation correctly with lazy variables', async () => {
Expand Down Expand Up @@ -195,7 +194,7 @@ describe('React-Query', () => {
);`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});
});

Expand Down Expand Up @@ -242,7 +241,7 @@ describe('React-Query', () => {
);`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});
it('Should support useTypeImports', async () => {
const config = {
Expand Down Expand Up @@ -312,7 +311,7 @@ describe('React-Query', () => {
);`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});

it('Should generate query correctly with fetch config', async () => {
Expand Down Expand Up @@ -352,7 +351,7 @@ describe('React-Query', () => {
}`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});

it('Should generate query correctly with hardcoded endpoint from env var', async () => {
Expand Down Expand Up @@ -386,7 +385,7 @@ describe('React-Query', () => {
}`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});

it('Should generate query correctly with hardcoded endpoint from just identifier', async () => {
Expand Down Expand Up @@ -420,7 +419,7 @@ describe('React-Query', () => {
}`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});
});

Expand Down Expand Up @@ -464,7 +463,7 @@ describe('React-Query', () => {
);`);

expect(out.content).toMatchSnapshot();
await validateTypeScript(mergeOutputs(out), schema, docs, config, false);
await validateTypeScript(mergeOutputs(out), schema, docs, config);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ describe('Vue Apollo', () => {
output: Types.PluginOutput,
testSchema: GraphQLSchema,
documents: Types.DocumentFile[],
config: any,
playground = false
config: any
) => {
const tsOutput = await tsPlugin(testSchema, documents, config, { outputFile: '' });
const tsDocumentsOutput = await tsDocumentsPlugin(testSchema, documents, config, { outputFile: '' });
const merged = mergeOutputs([tsOutput, tsDocumentsOutput, output]);
validateTs(merged, undefined, true, false, playground);
validateTs(merged, undefined, true, false);

return merged;
};
Expand Down
1 change: 0 additions & 1 deletion packages/utils/graphql-codegen-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"lz-string": "^1.4.4",
"graphql-helix": "1.2.3",
"nock": "13.0.10",
"open": "^7.3.0",
"tslib": "~2.1.0"
},
"publishConfig": {
Expand Down

0 comments on commit 3e514fd

Please sign in to comment.