Skip to content

Commit a992784

Browse files
committed
Add desired functionality
1 parent 6751b52 commit a992784

File tree

8 files changed

+383
-248
lines changed

8 files changed

+383
-248
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": true
5+
}
6+
}

jest.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ module.exports = {
77
"<rootDir>/node_modules/",
88
],
99
collectCoverageFrom: ["src/**/*.ts"],
10-
moduleNameMapper: {
11-
"^apollo-typed-documents$": "<rootDir>/src",
12-
},
1310
transform: {
1411
"\\.(ts|js)$": "ts-jest",
1512
"\\.graphql$": "jest-transform-graphql",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"pascal-case": "^3.1.1"
3434
},
3535
"peerDependencies": {
36+
"@graphql-typed-document-node/core": "^3.1.0",
3637
"graphql": "^14.6.0"
3738
},
3839
"devDependencies": {
@@ -42,14 +43,13 @@
4243
"@graphql-codegen/core": "^1.17.9",
4344
"@graphql-codegen/plugin-helpers": "^1.18.2",
4445
"@graphql-codegen/typescript": "^1.19.0",
45-
"@graphql-codegen/typescript-operations": "^1.17.12",
46+
"@graphql-codegen/typescript-operations": "^2.1.6",
4647
"@types/jest": "^26.0.19",
4748
"@types/node": "^14.14.14",
4849
"@types/react": "^17.0.0",
4950
"@types/tmp": "^0.2.0",
5051
"@typescript-eslint/eslint-plugin": "^4.11.0",
5152
"@typescript-eslint/parser": "^4.11.0",
52-
"apollo-typed-documents": "link:.",
5353
"eslint": "^7.16.0",
5454
"eslint-config-prettier": "^7.1.0",
5555
"eslint-plugin-prettier": "^3.3.0",
Lines changed: 91 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { codegen } from "@graphql-codegen/core";
22
import { Types } from "@graphql-codegen/plugin-helpers";
3+
import { plugin as typescriptOperationsPlugin } from "@graphql-codegen/typescript-operations";
34
import { buildSchema, parse, printSchema } from "graphql";
45

56
import * as codegenTypedDocuments from "../codegenTypedDocuments";
@@ -30,14 +31,17 @@ const getConfig = (
3031
filename: "not-relevant",
3132
schema: parse(printSchema(schema)),
3233
plugins: [
34+
{ typescriptOperationsPlugin: {} },
3335
{
3436
codegenTypedDocuments: {
35-
typesModule: "@codegen-types",
3637
...pluginOptions,
3738
},
3839
},
3940
],
40-
pluginMap: { codegenTypedDocuments },
41+
pluginMap: {
42+
typescriptOperationsPlugin: { plugin: typescriptOperationsPlugin },
43+
codegenTypedDocuments,
44+
},
4145
config: {},
4246
documents: [],
4347
...generateOptions,
@@ -48,10 +52,30 @@ describe("codegenTypedDocuments", () => {
4852
const config = getConfig();
4953
const output = await codegen(config);
5054

51-
expect(output).toMatchInlineSnapshot(`""`);
55+
expect(output).toBe("");
5256
});
5357

54-
it("should have ambient module declarations for each document", async () => {
58+
it("should not have any output for fragment", async () => {
59+
const fragmentDocument = parse(`
60+
fragment authors on Author {
61+
idField
62+
}
63+
`);
64+
65+
const document = {
66+
document: fragmentDocument,
67+
location: "authorFragment.gql",
68+
};
69+
70+
const config = getConfig({ documents: [document] });
71+
const output = await codegen(config);
72+
73+
expect(output).toBe(
74+
"export type AuthorsFragment = { __typename?: 'Author', idField: string };\n"
75+
);
76+
});
77+
78+
it("should have default export for a single query document", async () => {
5579
const queryDocument = parse(`
5680
query authors {
5781
authors {
@@ -60,6 +84,27 @@ describe("codegenTypedDocuments", () => {
6084
}
6185
`);
6286

87+
const document = { document: queryDocument, location: "authors.gql" };
88+
89+
const config = getConfig({ documents: [document] });
90+
const output = await codegen(config);
91+
92+
expect(output).toBe(
93+
`
94+
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
95+
96+
export type AuthorsQueryVariables = Exact<{ [key: string]: never; }>;
97+
98+
99+
export type AuthorsQuery = { __typename?: 'Query', authors?: Array<{ __typename?: 'Author', idField: string } | null | undefined> | null | undefined };
100+
101+
export const authors: TypedDocumentNode<AuthorsQuery, AuthorsQueryVariables>;
102+
export default authors;
103+
`.trimStart()
104+
);
105+
});
106+
107+
it("should have default export for a single mutation document", async () => {
63108
const mutationDocument = parse(`
64109
mutation createAuthor {
65110
createAuthor {
@@ -68,186 +113,90 @@ describe("codegenTypedDocuments", () => {
68113
}
69114
`);
70115

71-
const documents = [
72-
{ document: queryDocument, location: "authors.gql" },
73-
{ document: mutationDocument, location: "createAuthor.gql" },
74-
];
116+
const document = {
117+
document: mutationDocument,
118+
location: "createAuthors.gql",
119+
};
75120

76-
const config = getConfig({ documents });
121+
const config = getConfig({ documents: [document] });
77122
const output = await codegen(config);
78123

79-
expect(output).toMatchInlineSnapshot(`
80-
"declare module \\"*/authors.gql\\" {
81-
import { TypedDocumentNode } from \\"apollo-typed-documents\\";
82-
import { AuthorsQuery, AuthorsQueryVariables } from \\"@codegen-types\\";
83-
export const authors: TypedDocumentNode<AuthorsQueryVariables, AuthorsQuery>;
84-
export default authors;
85-
}
124+
expect(output).toBe(
125+
`
126+
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
86127
87-
declare module \\"*/createAuthor.gql\\" {
88-
import { TypedDocumentNode } from \\"apollo-typed-documents\\";
89-
import { CreateAuthorMutation, CreateAuthorMutationVariables } from \\"@codegen-types\\";
90-
export const createAuthor: TypedDocumentNode<CreateAuthorMutationVariables, CreateAuthorMutation>;
91-
export default createAuthor;
92-
}"
93-
`);
128+
export type CreateAuthorMutationVariables = Exact<{ [key: string]: never; }>;
129+
130+
131+
export type CreateAuthorMutation = { __typename?: 'Mutation', createAuthor: { __typename?: 'Author', idField: string } };
132+
133+
export const createAuthor: TypedDocumentNode<CreateAuthorMutation, CreateAuthorMutationVariables>;
134+
export default createAuthor;
135+
`.trimStart()
136+
);
94137
});
95138

96139
it("should not have default exports for multiple operations", async () => {
97-
const queryDocument = parse(`
140+
const document = parse(`
98141
query authors {
99142
authors {
100143
idField
101144
}
102145
}
146+
103147
query alsoAuthors {
104148
authors {
105149
idField
106150
}
107-
}
108-
`);
151+
}
109152
110-
const mutationDocument = parse(`
111153
mutation createAuthor {
112154
createAuthor {
113155
idField
114156
}
115157
}
158+
116159
mutation alsoCreateAuthor {
117160
createAuthor {
118161
idField
119162
}
120163
}
121164
`);
122165

123-
const documents = [
124-
{ document: queryDocument, location: "authors.gql" },
125-
{ document: mutationDocument, location: "createAuthor.gql" },
126-
];
127-
128-
const config = getConfig({ documents });
166+
const config = getConfig({
167+
documents: [{ document, location: "authors.gql" }],
168+
});
129169
const output = await codegen(config);
130170

131-
expect(output).toMatchInlineSnapshot(`
132-
"declare module \\"*/authors.gql\\" {
133-
import { TypedDocumentNode } from \\"apollo-typed-documents\\";
134-
import { AuthorsQuery, AuthorsQueryVariables } from \\"@codegen-types\\";
135-
export const authors: TypedDocumentNode<AuthorsQueryVariables, AuthorsQuery>;
136-
import { AlsoAuthorsQuery, AlsoAuthorsQueryVariables } from \\"@codegen-types\\";
137-
export const alsoAuthors: TypedDocumentNode<AlsoAuthorsQueryVariables, AlsoAuthorsQuery>;
138-
}
171+
expect(output).toBe(
172+
`
173+
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
139174
140-
declare module \\"*/createAuthor.gql\\" {
141-
import { TypedDocumentNode } from \\"apollo-typed-documents\\";
142-
import { CreateAuthorMutation, CreateAuthorMutationVariables } from \\"@codegen-types\\";
143-
export const createAuthor: TypedDocumentNode<CreateAuthorMutationVariables, CreateAuthorMutation>;
144-
import { AlsoCreateAuthorMutation, AlsoCreateAuthorMutationVariables } from \\"@codegen-types\\";
145-
export const alsoCreateAuthor: TypedDocumentNode<AlsoCreateAuthorMutationVariables, AlsoCreateAuthorMutation>;
146-
}"
147-
`);
148-
});
175+
export type AuthorsQueryVariables = Exact<{ [key: string]: never; }>;
149176
150-
describe("module path customization", () => {
151-
const queryDocument = parse(`
152-
query authors {
153-
authors {
154-
idField
155-
}
156-
}
157-
`);
158177
159-
const mutationDocument = parse(`
160-
mutation createAuthor {
161-
createAuthor {
162-
idField
163-
}
164-
}
165-
`);
178+
export type AuthorsQuery = { __typename?: 'Query', authors?: Array<{ __typename?: 'Author', idField: string } | null | undefined> | null | undefined };
166179
167-
const documents = [
168-
{ document: queryDocument, location: "literary/types/authors.gql" },
169-
{ document: mutationDocument, location: "mutations/createAuthor.gql" },
170-
];
171-
172-
it("wildcards the basename by default", async () => {
173-
const config = getConfig({ documents });
174-
const output = await codegen(config);
175-
176-
expect(output).toMatchInlineSnapshot(`
177-
"declare module \\"*/authors.gql\\" {
178-
import { TypedDocumentNode } from \\"apollo-typed-documents\\";
179-
import { AuthorsQuery, AuthorsQueryVariables } from \\"@codegen-types\\";
180-
export const authors: TypedDocumentNode<AuthorsQueryVariables, AuthorsQuery>;
181-
export default authors;
182-
}
180+
export type AlsoAuthorsQueryVariables = Exact<{ [key: string]: never; }>;
183181
184-
declare module \\"*/createAuthor.gql\\" {
185-
import { TypedDocumentNode } from \\"apollo-typed-documents\\";
186-
import { CreateAuthorMutation, CreateAuthorMutationVariables } from \\"@codegen-types\\";
187-
export const createAuthor: TypedDocumentNode<CreateAuthorMutationVariables, CreateAuthorMutation>;
188-
export default createAuthor;
189-
}"
190-
`);
191-
});
192182
193-
it("respects the relativeToCwd setting", async () => {
194-
const config = getConfig({ documents }, { relativeToCwd: true });
195-
const output = await codegen(config);
183+
export type AlsoAuthorsQuery = { __typename?: 'Query', authors?: Array<{ __typename?: 'Author', idField: string } | null | undefined> | null | undefined };
196184
197-
expect(output).toEqual(
198-
expect.stringContaining(`declare module "*/literary/types/authors.gql"`)
199-
);
200-
expect(output).toEqual(
201-
expect.stringContaining(`declare module "*/mutations/createAuthor.gql"`)
202-
);
203-
});
185+
export type CreateAuthorMutationVariables = Exact<{ [key: string]: never; }>;
204186
205-
it("respects the prefix setting", async () => {
206-
const config = getConfig({ documents }, { prefix: "gql/" });
207-
const output = await codegen(config);
208187
209-
expect(output).toEqual(
210-
expect.stringContaining(`declare module "gql/authors.gql"`)
211-
);
212-
expect(output).toEqual(
213-
expect.stringContaining(`declare module "gql/createAuthor.gql"`)
214-
);
215-
});
188+
export type CreateAuthorMutation = { __typename?: 'Mutation', createAuthor: { __typename?: 'Author', idField: string } };
216189
217-
it("respects the modulePathPrefix setting", async () => {
218-
const config = getConfig({ documents }, { modulePathPrefix: "stuff/" });
219-
const output = await codegen(config);
190+
export type AlsoCreateAuthorMutationVariables = Exact<{ [key: string]: never; }>;
220191
221-
expect(output).toEqual(
222-
expect.stringContaining(`declare module "*/stuff/authors.gql"`)
223-
);
224-
expect(output).toEqual(
225-
expect.stringContaining(`declare module "*/stuff/createAuthor.gql"`)
226-
);
227-
});
228192
229-
it("allows combining path settings", async () => {
230-
const config = getConfig(
231-
{ documents },
232-
{
233-
prefix: "",
234-
modulePathPrefix: "defs/",
235-
relativeToCwd: true,
236-
}
237-
);
238-
239-
const output = await codegen(config);
240-
241-
expect(output).toEqual(
242-
expect.stringContaining(
243-
`declare module "defs/literary/types/authors.gql"`
244-
)
245-
);
246-
expect(output).toEqual(
247-
expect.stringContaining(
248-
`declare module "defs/mutations/createAuthor.gql"`
249-
)
250-
);
251-
});
193+
export type AlsoCreateAuthorMutation = { __typename?: 'Mutation', createAuthor: { __typename?: 'Author', idField: string } };
194+
195+
export const authors: TypedDocumentNode<AuthorsQuery, AuthorsQueryVariables>;
196+
export const alsoAuthors: TypedDocumentNode<AlsoAuthorsQuery, AlsoAuthorsQueryVariables>;
197+
export const createAuthor: TypedDocumentNode<CreateAuthorMutation, CreateAuthorMutationVariables>;
198+
export const alsoCreateAuthor: TypedDocumentNode<AlsoCreateAuthorMutation, AlsoCreateAuthorMutationVariables>;
199+
`.trimStart()
200+
);
252201
});
253202
});

0 commit comments

Comments
 (0)