1
1
import { codegen } from "@graphql-codegen/core" ;
2
2
import { Types } from "@graphql-codegen/plugin-helpers" ;
3
+ import { plugin as typescriptOperationsPlugin } from "@graphql-codegen/typescript-operations" ;
3
4
import { buildSchema , parse , printSchema } from "graphql" ;
4
5
5
6
import * as codegenTypedDocuments from "../codegenTypedDocuments" ;
@@ -30,14 +31,17 @@ const getConfig = (
30
31
filename : "not-relevant" ,
31
32
schema : parse ( printSchema ( schema ) ) ,
32
33
plugins : [
34
+ { typescriptOperationsPlugin : { } } ,
33
35
{
34
36
codegenTypedDocuments : {
35
- typesModule : "@codegen-types" ,
36
37
...pluginOptions ,
37
38
} ,
38
39
} ,
39
40
] ,
40
- pluginMap : { codegenTypedDocuments } ,
41
+ pluginMap : {
42
+ typescriptOperationsPlugin : { plugin : typescriptOperationsPlugin } ,
43
+ codegenTypedDocuments,
44
+ } ,
41
45
config : { } ,
42
46
documents : [ ] ,
43
47
...generateOptions ,
@@ -48,10 +52,30 @@ describe("codegenTypedDocuments", () => {
48
52
const config = getConfig ( ) ;
49
53
const output = await codegen ( config ) ;
50
54
51
- expect ( output ) . toMatchInlineSnapshot ( `""` ) ;
55
+ expect ( output ) . toBe ( "" ) ;
52
56
} ) ;
53
57
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 ( ) => {
55
79
const queryDocument = parse ( `
56
80
query authors {
57
81
authors {
@@ -60,6 +84,27 @@ describe("codegenTypedDocuments", () => {
60
84
}
61
85
` ) ;
62
86
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 ( ) => {
63
108
const mutationDocument = parse ( `
64
109
mutation createAuthor {
65
110
createAuthor {
@@ -68,186 +113,90 @@ describe("codegenTypedDocuments", () => {
68
113
}
69
114
` ) ;
70
115
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
+ } ;
75
120
76
- const config = getConfig ( { documents } ) ;
121
+ const config = getConfig ( { documents : [ document ] } ) ;
77
122
const output = await codegen ( config ) ;
78
123
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";
86
127
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
+ ) ;
94
137
} ) ;
95
138
96
139
it ( "should not have default exports for multiple operations" , async ( ) => {
97
- const queryDocument = parse ( `
140
+ const document = parse ( `
98
141
query authors {
99
142
authors {
100
143
idField
101
144
}
102
145
}
146
+
103
147
query alsoAuthors {
104
148
authors {
105
149
idField
106
150
}
107
- }
108
- ` ) ;
151
+ }
109
152
110
- const mutationDocument = parse ( `
111
153
mutation createAuthor {
112
154
createAuthor {
113
155
idField
114
156
}
115
157
}
158
+
116
159
mutation alsoCreateAuthor {
117
160
createAuthor {
118
161
idField
119
162
}
120
163
}
121
164
` ) ;
122
165
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
+ } ) ;
129
169
const output = await codegen ( config ) ;
130
170
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";
139
174
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; }>;
149
176
150
- describe ( "module path customization" , ( ) => {
151
- const queryDocument = parse ( `
152
- query authors {
153
- authors {
154
- idField
155
- }
156
- }
157
- ` ) ;
158
177
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 };
166
179
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; }>;
183
181
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
- } ) ;
192
182
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 };
196
184
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; }>;
204
186
205
- it ( "respects the prefix setting" , async ( ) => {
206
- const config = getConfig ( { documents } , { prefix : "gql/" } ) ;
207
- const output = await codegen ( config ) ;
208
187
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 } };
216
189
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; }>;
220
191
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
- } ) ;
228
192
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
+ ) ;
252
201
} ) ;
253
202
} ) ;
0 commit comments