Skip to content

Commit 5cd99a2

Browse files
committed
feat: add ignore switch
1 parent cab01a0 commit 5cd99a2

12 files changed

+373
-70
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,26 @@ type User = {
736736
}
737737
```
738738
739+
## `ignore [data | variables]`
740+
741+
`ignore data` makes `graphql-typegen` not output a data type.
742+
`ignore variables` makes `graphql-typegen` not output a variables type.
743+
`ignore` makes `graphql-typegen` not output either a data or variables type.
744+
745+
This may be set in a comment in your GraphQL operation:
746+
747+
```js
748+
const query = gql`
749+
# @graphql-typegen ignore variables
750+
query user($id: Int!) {
751+
user(id: $id) {
752+
id
753+
name
754+
}
755+
}
756+
`
757+
```
758+
739759
# CLI Usage
740760
741761
```

src/internal/analyzeSchema.ts

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,55 @@ export type TypeKind =
2222
export type IntrospectionArg = {
2323
name: string
2424
type: IntrospectionType
25-
description: string
25+
description?: string | null
2626
}
2727

2828
export type AnalyzedArg = {
2929
name: string
3030
type: AnalyzedType
31-
description: string
31+
description: string | null | undefined
3232
config?: ConfigDirectives
3333
}
3434

3535
export type IntrospectionField = {
3636
name: string
3737
args: IntrospectionArg[]
3838
type: IntrospectionType
39-
description: string
39+
description?: string | null
4040
}
4141

4242
export type AnalyzedField = {
4343
name: string
4444
args: Record<string, AnalyzedArg>
4545
type: AnalyzedType
46-
description: string
46+
description: string | null | undefined
4747
parent?: AnalyzedType
4848
config?: ConfigDirectives
4949
}
5050

5151
export type IntrospectionInputField = {
5252
name: string
5353
type: IntrospectionType
54-
description: string
54+
description: string | null
5555
}
5656

5757
export type AnalyzedInputField = {
5858
name: string
5959
type: AnalyzedType
60-
description: string
60+
description: string | null | undefined
6161
parent?: AnalyzedType
6262
config?: ConfigDirectives
6363
}
6464

6565
export type EnumValue = {
6666
name: string
67-
description: string
67+
description?: string | null
6868
}
6969

7070
export type IntrospectionType = {
7171
kind: TypeKind
7272
name: string
73-
description: string
73+
description?: string | null
7474
ofType?: IntrospectionType | null
7575
fields?: IntrospectionField[] | null
7676
inputFields?: IntrospectionInputField[] | null
@@ -82,7 +82,7 @@ export type IntrospectionType = {
8282
export type AnalyzedType = {
8383
kind: TypeKind
8484
name: string
85-
description: string
85+
description: string | null | undefined
8686
ofType?: AnalyzedType | null
8787
fields?: Record<string, AnalyzedField> | null
8888
inputFields?: Record<string, AnalyzedInputField> | null
@@ -103,41 +103,42 @@ function analyzeTypes(
103103
): Record<string, AnalyzedType> {
104104
const introspectionTypes: IntrospectionType[] = data.__schema.types as any
105105
function getDescriptionDirectives(
106-
description: string | undefined
106+
node:
107+
| IntrospectionField
108+
| IntrospectionInputField
109+
| IntrospectionType
110+
| IntrospectionArg
107111
): ConfigDirectives {
108-
return getConfigDirectives(
109-
description ? description.split(/\n/gm) : [],
110-
cwd
111-
)
112+
const description = (node as any).description || ''
113+
return getConfigDirectives(description ? description.split(/\n/gm) : [], {
114+
cwd,
115+
})
112116
}
113117

114118
function convertIntrospectionArgs(
115119
args: IntrospectionArg[]
116120
): Record<string, AnalyzedArg> {
117121
const AnalyzedArgs: Record<string, AnalyzedArg> = {}
118-
for (const { name, type, description } of args) {
122+
for (const arg of args) {
123+
const { name, type, description } = arg
119124
AnalyzedArgs[name] = {
120125
name,
121126
type: convertIntrospectionType(type),
122127
description,
123-
config: getDescriptionDirectives(description),
128+
config: getDescriptionDirectives(arg),
124129
}
125130
}
126131
return AnalyzedArgs
127132
}
128133

129-
function convertIntrospectionField({
130-
name,
131-
args,
132-
type,
133-
description,
134-
}: IntrospectionField): AnalyzedField {
134+
function convertIntrospectionField(field: IntrospectionField): AnalyzedField {
135+
const { name, args, type, description } = field
135136
return {
136137
name,
137138
type: convertIntrospectionType(type),
138139
args: convertIntrospectionArgs(args),
139140
description,
140-
config: getDescriptionDirectives(description),
141+
config: getDescriptionDirectives(field),
141142
}
142143
}
143144

@@ -151,16 +152,15 @@ function analyzeTypes(
151152
return AnalyzedFields
152153
}
153154

154-
function convertIntrospectionInputField({
155-
name,
156-
type,
157-
description,
158-
}: IntrospectionInputField): AnalyzedInputField {
155+
function convertIntrospectionInputField(
156+
field: IntrospectionInputField
157+
): AnalyzedInputField {
158+
const { name, type, description } = field
159159
return {
160160
name,
161161
type: convertIntrospectionType(type),
162162
description,
163-
config: getDescriptionDirectives(description),
163+
config: getDescriptionDirectives(field),
164164
}
165165
}
166166

@@ -174,17 +174,18 @@ function analyzeTypes(
174174
return AnalyzedFields
175175
}
176176

177-
function convertIntrospectionType({
178-
name,
179-
description,
180-
kind,
181-
ofType,
182-
fields,
183-
inputFields,
184-
enumValues,
185-
interfaces,
186-
possibleTypes,
187-
}: IntrospectionType): AnalyzedType {
177+
function convertIntrospectionType(type: IntrospectionType): AnalyzedType {
178+
const {
179+
name,
180+
description,
181+
kind,
182+
ofType,
183+
fields,
184+
inputFields,
185+
enumValues,
186+
interfaces,
187+
possibleTypes,
188+
} = type
188189
return {
189190
name,
190191
description,
@@ -195,7 +196,7 @@ function analyzeTypes(
195196
? convertIntrospectionInputFields(inputFields)
196197
: null,
197198
enumValues,
198-
config: getDescriptionDirectives(description),
199+
config: getDescriptionDirectives(type),
199200
interfaces: interfaces
200201
? interfaces.map(iface => convertIntrospectionType(iface))
201202
: null,

src/internal/generateFlowTypesFromDocument.ts

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ import simplifyIntersection from './simplifyIntersection'
2929

3030
type GeneratedQueryType = {
3131
variables?: TypeAlias
32-
data: TypeAlias
32+
data?: TypeAlias
3333
}
3434

3535
type GeneratedMutationType = {
3636
variables?: TypeAlias
37-
data: TypeAlias
38-
mutationFunction: TypeAlias
37+
data?: TypeAlias
38+
mutationFunction?: TypeAlias
3939
}
4040

4141
type GeneratedTypes = {
@@ -106,6 +106,8 @@ export default function generateFlowTypesFromDocument({
106106
addTypename,
107107
extract,
108108
external,
109+
ignoreData: undefined,
110+
ignoreVariables: undefined,
109111
}
110112
}
111113

@@ -261,10 +263,14 @@ export default function generateFlowTypesFromDocument({
261263
function convertOperationDefinition(
262264
def: graphql.OperationDefinitionNode
263265
): void {
266+
const ownDirectives = getCommentDirectives(def, cwd)
267+
const { ignoreData, ignoreVariables } = ownDirectives
268+
if (ignoreData && ignoreVariables) return
269+
264270
const { operation, selectionSet, variableDefinitions } = def
265271
const config = getCombinedConfig(
266272
{ external: null, extract: null },
267-
getCommentDirectives(def, cwd)
273+
ownDirectives
268274
)
269275

270276
let name = def.name ? upperFirst(def.name.value) : `Unnamed`
@@ -277,38 +283,70 @@ export default function generateFlowTypesFromDocument({
277283
if (name.toLowerCase().lastIndexOf(operation) < 0) {
278284
name += upperFirst(operation)
279285
}
280-
const data = addTypeAlias(
281-
`${name}Data`,
282-
convertSelectionSet(selectionSet, types[upperFirst(operation)], config)
283-
)
284286
if (operation === 'query' || operation === 'subscription') {
285287
const operationTypes: GeneratedQueryType = def.name
286-
? (generatedTypes[operation][def.name.value] = { data })
287-
: { data }
288-
if (variableDefinitions && variableDefinitions.length) {
288+
? (generatedTypes[operation][def.name.value] = {})
289+
: {}
290+
if (!ignoreData) {
291+
operationTypes.data = addTypeAlias(
292+
`${name}Data`,
293+
convertSelectionSet(
294+
selectionSet,
295+
types[upperFirst(operation)],
296+
config
297+
)
298+
)
299+
}
300+
if (
301+
!ignoreVariables &&
302+
variableDefinitions &&
303+
variableDefinitions.length
304+
) {
289305
operationTypes.variables = addTypeAlias(
290306
`${name}Variables`,
291307
convertVariableDefinitions(variableDefinitions, config)
292308
)
293309
}
294310
} else if (operation === 'mutation') {
295-
const variables =
296-
variableDefinitions && variableDefinitions.length
297-
? addTypeAlias(
298-
`${name}Variables`,
299-
convertVariableDefinitions(variableDefinitions, config)
300-
)
301-
: null
302-
const mutationFunction = statement([
303-
`type ${name}Function = ${MutationFunction()}<${data.id.name}${
304-
variables ? `, ${variables.id.name}` : ''
305-
}>`,
306-
])
307-
statements.push(mutationFunction)
308311
const operationTypes: GeneratedMutationType = def.name
309-
? (generatedTypes.mutation[def.name.value] = { data, mutationFunction })
310-
: { data, mutationFunction }
311-
if (variables) operationTypes.variables = variables
312+
? (generatedTypes[operation][def.name.value] = {})
313+
: {}
314+
315+
let data, variables
316+
317+
if (!ignoreData) {
318+
const _data = addTypeAlias(
319+
`${name}Data`,
320+
convertSelectionSet(
321+
selectionSet,
322+
types[upperFirst(operation)],
323+
config
324+
)
325+
)
326+
data = _data
327+
operationTypes.data = _data
328+
}
329+
if (
330+
!ignoreVariables &&
331+
variableDefinitions &&
332+
variableDefinitions.length
333+
) {
334+
const _variables = addTypeAlias(
335+
`${name}Variables`,
336+
convertVariableDefinitions(variableDefinitions, config)
337+
)
338+
variables = _variables
339+
operationTypes.variables = _variables
340+
}
341+
if (data && variables) {
342+
const mutationFunction = statement([
343+
`type ${name}Function = ${MutationFunction()}<${data.id.name}${
344+
variables ? `, ${variables.id.name}` : ''
345+
}>`,
346+
])
347+
operationTypes.mutationFunction = mutationFunction
348+
statements.push(mutationFunction)
349+
}
312350
}
313351
}
314352

src/internal/getCommentDirectives.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ describe(`getCommentDirectives`, function() {
6161
extract: 'Foob',
6262
external: undefined,
6363
addTypename: undefined,
64+
ignoreData: undefined,
65+
ignoreVariables: undefined,
6466
})
6567
})
6668
})

src/internal/getCommentDirectives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export default function getCommentDirectives(
1111
if (typeof value === 'string') yield value
1212
}
1313
}
14-
return getConfigDirectives(lines(), cwd)
14+
return getConfigDirectives(lines(), { cwd, nodeKind: node.kind })
1515
}

0 commit comments

Comments
 (0)