Skip to content

feat: multi-project support #36

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

Merged
merged 1 commit into from
Feb 7, 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
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export interface Options {
* @default false
*/
matchOnSchemas?: boolean;
/**
* Name of a project in a multi-project config file.
*/
project?: string;
/**
* Manually define the codegen config.
*/
Expand Down Expand Up @@ -98,6 +102,7 @@ export function GraphQLCodegen(options?: Options): Plugin {
throwOnBuild = true,
matchOnDocuments = true,
matchOnSchemas = false,
project = null,
config = null,
configOverride = {},
configOverrideOnStart = {},
Expand Down Expand Up @@ -140,6 +145,7 @@ export function GraphQLCodegen(options?: Options): Plugin {
log("Loading codegen context:", configFilePathOverride ?? cwd);
codegenContext = await loadContext(configFilePathOverride);
}
if (project != null) codegenContext.useProject(project);
log("Loading codegen context successful");
} catch (error) {
log("Loading codegen context failed");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`graphql-multi-project > generates 1`] = `
"export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: { input: string; output: string; }
String: { input: string; output: string; }
Boolean: { input: boolean; output: boolean; }
Int: { input: number; output: number; }
Float: { input: number; output: number; }
};

export type Query = {
__typename?: 'Query';
foo?: Maybe<Scalars['String']['output']>;
};

export type FooQueryVariables = Exact<{ [key: string]: never; }>;


export type FooQuery = { __typename?: 'Query', foo?: string | null };
"
`;
42 changes: 42 additions & 0 deletions test/graphql-multi-project/graphql-multi-project.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { promises as fs } from "node:fs";
import { createServer, type UserConfig, type ViteDevServer } from "vite";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import codegen from "../../src/index";

const TEST_PATH = "./test/graphql-multi-project" as const;
const OUTPUT_PATH = `${TEST_PATH}/generated` as const;
const OUTPUT_FILE = `${OUTPUT_PATH}/graphql.ts` as const;

const viteConfig = {
root: import.meta.dirname,
plugins: [
codegen({
configFilePathOverride: `${TEST_PATH}/graphql.config.yml`,
project: "foo",
}),
],
} satisfies UserConfig;

describe("graphql-multi-project", () => {
let viteServer: ViteDevServer | null = null;

beforeAll(async () => {
viteServer = await createServer(viteConfig).then((s) => s.listen());
});

afterAll(async () => {
await viteServer?.close();
viteServer = null;
});

afterEach(async () => {
await fs.rm(OUTPUT_PATH, { recursive: true });
});

it("generates", async () => {
await new Promise((resolve) => setTimeout(resolve, 200));
const file = await fs.readFile(OUTPUT_FILE, "utf-8");

expect(file).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions test/graphql-multi-project/graphql.config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
projects:
foo:
schema: ./test/graphql-multi-project/schema.graphql
documents: ./test/graphql-multi-project/graphql/**/*.graphql
extensions:
codegen:
generates:
./test/graphql-multi-project/generated/graphql.ts:
plugins:
- typescript
- typescript-operations
3 changes: 3 additions & 0 deletions test/graphql-multi-project/graphql/queries/Foo.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query Foo {
foo
}
3 changes: 3 additions & 0 deletions test/graphql-multi-project/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
foo: String
}