Skip to content

Commit 6f1741a

Browse files
authored
Fix load schema pointer type (#10227)
* Fix load schema pointer type * Add tests
1 parent b28fe5f commit 6f1741a

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

.changeset/famous-spiders-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/cli': patch
3+
---
4+
5+
Fix schema pointers type to allow an array of pointers

packages/graphql-codegen-cli/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export class CodegenContext {
422422
return this._pluginContext;
423423
}
424424

425-
async loadSchema(pointer: Types.Schema): Promise<GraphQLSchema> {
425+
async loadSchema(pointer: Types.Schema | Types.Schema[]): Promise<GraphQLSchema> {
426426
const config = this.getConfig(defaultSchemaLoadOptions);
427427
if (this._graphqlConfig) {
428428
// TODO: SchemaWithLoader won't work here

packages/graphql-codegen-cli/src/load.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const defaultDocumentsLoadOptions = {
2828
};
2929

3030
export async function loadSchema(
31-
schemaPointers: UnnormalizedTypeDefPointer,
31+
schemaPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
3232
config: Types.Config
3333
): Promise<GraphQLSchema> {
3434
try {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { createContext, ensureContext } from '../src/index.js';
2+
3+
describe('Codegen config - Context', () => {
4+
it('loads and merge multiple schemas when using GraphQL config', async () => {
5+
const context = await createContext({
6+
config: './packages/graphql-codegen-cli/tests/test-files/graphql.config.js',
7+
project: 'prj1',
8+
errorsOnly: true,
9+
overwrite: true,
10+
profile: true,
11+
require: [],
12+
silent: false,
13+
watch: false,
14+
});
15+
16+
const schema1 = /* GraphQL */ `
17+
type Query
18+
scalar Date
19+
`;
20+
21+
const schema2 = /* GraphQL */ `
22+
extend type Query {
23+
me: User
24+
}
25+
type User {
26+
id: ID!
27+
name: String!
28+
createdAt: Date!
29+
}
30+
`;
31+
32+
const schema3 = /* GraphQL */ `
33+
extend type Query {
34+
media: Media
35+
}
36+
interface Media {
37+
id: ID!
38+
}
39+
type Image implements Media {
40+
id: ID!
41+
url: String!
42+
}
43+
type Book implements Media {
44+
id: ID!
45+
title: String!
46+
}
47+
`;
48+
49+
const mergedSchema = await context.loadSchema([schema1, schema2, schema3]);
50+
51+
const typeMap = mergedSchema.getTypeMap();
52+
53+
expect(typeMap['Query']).toBeDefined();
54+
expect(typeMap['Date']).toBeDefined();
55+
expect(typeMap['User']).toBeDefined();
56+
expect(typeMap['Media']).toBeDefined();
57+
expect(typeMap['Image']).toBeDefined();
58+
expect(typeMap['Book']).toBeDefined();
59+
});
60+
61+
it('loads and merge multiple schemas when using input config', async () => {
62+
const context = ensureContext({
63+
generates: {},
64+
});
65+
66+
const schema1 = /* GraphQL */ `
67+
type Mutation
68+
scalar DateTime
69+
`;
70+
71+
const schema2 = /* GraphQL */ `
72+
extend type Mutation {
73+
createUser: User
74+
}
75+
type User {
76+
id: ID!
77+
createdAt: DateTime!
78+
}
79+
`;
80+
81+
const mergedSchema = await context.loadSchema([schema1, schema2]);
82+
83+
const typeMap = mergedSchema.getTypeMap();
84+
85+
expect(typeMap['Mutation']).toBeDefined();
86+
expect(typeMap['DateTime']).toBeDefined();
87+
expect(typeMap['User']).toBeDefined();
88+
});
89+
});

0 commit comments

Comments
 (0)