Skip to content

Commit 3f0899d

Browse files
authored
feat: multi-project support (#36)
1 parent 2802667 commit 3f0899d

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export interface Options {
5454
* @default false
5555
*/
5656
matchOnSchemas?: boolean;
57+
/**
58+
* Name of a project in a multi-project config file.
59+
*/
60+
project?: string;
5761
/**
5862
* Manually define the codegen config.
5963
*/
@@ -98,6 +102,7 @@ export function GraphQLCodegen(options?: Options): Plugin {
98102
throwOnBuild = true,
99103
matchOnDocuments = true,
100104
matchOnSchemas = false,
105+
project = null,
101106
config = null,
102107
configOverride = {},
103108
configOverrideOnStart = {},
@@ -140,6 +145,7 @@ export function GraphQLCodegen(options?: Options): Plugin {
140145
log("Loading codegen context:", configFilePathOverride ?? cwd);
141146
codegenContext = await loadContext(configFilePathOverride);
142147
}
148+
if (project != null) codegenContext.useProject(project);
143149
log("Loading codegen context successful");
144150
} catch (error) {
145151
log("Loading codegen context failed");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`graphql-multi-project > generates 1`] = `
4+
"export type Maybe<T> = T | null;
5+
export type InputMaybe<T> = Maybe<T>;
6+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
7+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
8+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
9+
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
10+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
11+
/** All built-in and custom scalars, mapped to their actual values */
12+
export type Scalars = {
13+
ID: { input: string; output: string; }
14+
String: { input: string; output: string; }
15+
Boolean: { input: boolean; output: boolean; }
16+
Int: { input: number; output: number; }
17+
Float: { input: number; output: number; }
18+
};
19+
20+
export type Query = {
21+
__typename?: 'Query';
22+
foo?: Maybe<Scalars['String']['output']>;
23+
};
24+
25+
export type FooQueryVariables = Exact<{ [key: string]: never; }>;
26+
27+
28+
export type FooQuery = { __typename?: 'Query', foo?: string | null };
29+
"
30+
`;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { promises as fs } from "node:fs";
2+
import { createServer, type UserConfig, type ViteDevServer } from "vite";
3+
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
4+
import codegen from "../../src/index";
5+
6+
const TEST_PATH = "./test/graphql-multi-project" as const;
7+
const OUTPUT_PATH = `${TEST_PATH}/generated` as const;
8+
const OUTPUT_FILE = `${OUTPUT_PATH}/graphql.ts` as const;
9+
10+
const viteConfig = {
11+
root: import.meta.dirname,
12+
plugins: [
13+
codegen({
14+
configFilePathOverride: `${TEST_PATH}/graphql.config.yml`,
15+
project: "foo",
16+
}),
17+
],
18+
} satisfies UserConfig;
19+
20+
describe("graphql-multi-project", () => {
21+
let viteServer: ViteDevServer | null = null;
22+
23+
beforeAll(async () => {
24+
viteServer = await createServer(viteConfig).then((s) => s.listen());
25+
});
26+
27+
afterAll(async () => {
28+
await viteServer?.close();
29+
viteServer = null;
30+
});
31+
32+
afterEach(async () => {
33+
await fs.rm(OUTPUT_PATH, { recursive: true });
34+
});
35+
36+
it("generates", async () => {
37+
await new Promise((resolve) => setTimeout(resolve, 200));
38+
const file = await fs.readFile(OUTPUT_FILE, "utf-8");
39+
40+
expect(file).toMatchSnapshot();
41+
});
42+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
projects:
2+
foo:
3+
schema: ./test/graphql-multi-project/schema.graphql
4+
documents: ./test/graphql-multi-project/graphql/**/*.graphql
5+
extensions:
6+
codegen:
7+
generates:
8+
./test/graphql-multi-project/generated/graphql.ts:
9+
plugins:
10+
- typescript
11+
- typescript-operations
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query Foo {
2+
foo
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Query {
2+
foo: String
3+
}

0 commit comments

Comments
 (0)