Skip to content

Commit

Permalink
Fix load schema pointer type (#10227)
Browse files Browse the repository at this point in the history
* Fix load schema pointer type

* Add tests
  • Loading branch information
eddeee888 authored Jan 27, 2025
1 parent b28fe5f commit 6f1741a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-spiders-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

Fix schema pointers type to allow an array of pointers
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export class CodegenContext {
return this._pluginContext;
}

async loadSchema(pointer: Types.Schema): Promise<GraphQLSchema> {
async loadSchema(pointer: Types.Schema | Types.Schema[]): Promise<GraphQLSchema> {
const config = this.getConfig(defaultSchemaLoadOptions);
if (this._graphqlConfig) {
// TODO: SchemaWithLoader won't work here
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const defaultDocumentsLoadOptions = {
};

export async function loadSchema(
schemaPointers: UnnormalizedTypeDefPointer,
schemaPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[],
config: Types.Config
): Promise<GraphQLSchema> {
try {
Expand Down
89 changes: 89 additions & 0 deletions packages/graphql-codegen-cli/tests/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { createContext, ensureContext } from '../src/index.js';

describe('Codegen config - Context', () => {
it('loads and merge multiple schemas when using GraphQL config', async () => {
const context = await createContext({
config: './packages/graphql-codegen-cli/tests/test-files/graphql.config.js',
project: 'prj1',
errorsOnly: true,
overwrite: true,
profile: true,
require: [],
silent: false,
watch: false,
});

const schema1 = /* GraphQL */ `
type Query
scalar Date
`;

const schema2 = /* GraphQL */ `
extend type Query {
me: User
}
type User {
id: ID!
name: String!
createdAt: Date!
}
`;

const schema3 = /* GraphQL */ `
extend type Query {
media: Media
}
interface Media {
id: ID!
}
type Image implements Media {
id: ID!
url: String!
}
type Book implements Media {
id: ID!
title: String!
}
`;

const mergedSchema = await context.loadSchema([schema1, schema2, schema3]);

const typeMap = mergedSchema.getTypeMap();

expect(typeMap['Query']).toBeDefined();
expect(typeMap['Date']).toBeDefined();
expect(typeMap['User']).toBeDefined();
expect(typeMap['Media']).toBeDefined();
expect(typeMap['Image']).toBeDefined();
expect(typeMap['Book']).toBeDefined();
});

it('loads and merge multiple schemas when using input config', async () => {
const context = ensureContext({
generates: {},
});

const schema1 = /* GraphQL */ `
type Mutation
scalar DateTime
`;

const schema2 = /* GraphQL */ `
extend type Mutation {
createUser: User
}
type User {
id: ID!
createdAt: DateTime!
}
`;

const mergedSchema = await context.loadSchema([schema1, schema2]);

const typeMap = mergedSchema.getTypeMap();

expect(typeMap['Mutation']).toBeDefined();
expect(typeMap['DateTime']).toBeDefined();
expect(typeMap['User']).toBeDefined();
});
});

0 comments on commit 6f1741a

Please sign in to comment.