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 load schema pointer type #10227

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add tests for this use case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea! This is done in 9efc60b

Note that instead of testing this load.ts directly, I'm passing multiple schemas into Codegen Context which eventually passes those schemas to load.ts

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();
});
});
Loading