1
- import {
2
- GraphQLUnionType ,
3
- GraphQLWrappingType ,
4
- GraphQLObjectType ,
5
- GraphQLInputObjectType ,
6
- GraphQLInputField ,
7
- GraphQLField ,
8
- GraphQLInputType ,
9
- GraphQLOutputType ,
10
- GraphQLScalarType ,
11
- GraphQLNamedType ,
12
-
13
- isNonNullType ,
14
- isListType ,
15
- GraphQLFieldMap ,
16
- GraphQLEnumType ,
17
- GraphQLType ,
18
- GraphQLInterfaceType ,
1
+ import {
2
+ GraphQLUnionType ,
3
+ GraphQLWrappingType ,
4
+ GraphQLObjectType ,
5
+ GraphQLInputObjectType ,
6
+ GraphQLInputField ,
7
+ GraphQLField ,
8
+ GraphQLInputType ,
9
+ GraphQLOutputType ,
10
+ GraphQLScalarType ,
11
+ GraphQLNamedType ,
12
+ isNonNullType ,
13
+ isListType ,
14
+ GraphQLFieldMap ,
15
+ GraphQLEnumType ,
16
+ GraphQLType ,
17
+ GraphQLInterfaceType
19
18
} from 'graphql'
20
19
21
20
import { Generator } from '../types'
@@ -30,7 +29,7 @@ export const generator: Generator = {
30
29
RootType : renderRootType ,
31
30
SchemaType : renderSchemaInterface ,
32
31
Main : renderMainMethod ,
33
- Header : renderHeader ,
32
+ Header : renderHeader
34
33
}
35
34
36
35
const scalarMapping = {
@@ -49,7 +48,11 @@ const typeDefs = \`
49
48
${ schema } \``
50
49
}
51
50
52
- function renderMainMethod ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
51
+ function renderMainMethod (
52
+ queryType : GraphQLObjectType ,
53
+ mutationType ?: GraphQLObjectType | null ,
54
+ subscriptionType ?: GraphQLObjectType | null
55
+ ) {
53
56
return `export class Graphcool extends BaseGraphcool {
54
57
55
58
constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) {
@@ -58,61 +61,96 @@ function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLOb
58
61
59
62
query: Query = {
60
63
${ renderMainMethodFields ( 'query' , queryType . getFields ( ) ) }
61
- }${ mutationType ? `
64
+ }${
65
+ mutationType
66
+ ? `
62
67
63
68
mutation: Mutation = {
64
69
${ renderMainMethodFields ( 'mutation' , mutationType . getFields ( ) ) }
65
- }` : '' }
70
+ }`
71
+ : ''
72
+ }
66
73
}`
67
74
}
68
75
69
-
70
76
export function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
71
- return Object . keys ( fields ) . map ( f => {
72
- const field = fields [ f ]
73
- return ` ${ field . name } : (args, info): Promise<${ renderFieldType ( field . type ) } ${ ! isNonNullType ( field . type ) ? ' | null' : '' } > => super.delegate('${ operation } ', '${ field . name } ', args, {}, info)`
74
- } ) . join ( ',\n' )
77
+ return Object . keys ( fields )
78
+ . map ( f => {
79
+ const field = fields [ f ]
80
+ return ` ${ field . name } : (args, info): Promise<${ renderFieldType ( field . type ) } ${
81
+ ! isNonNullType ( field . type ) ? ' | null' : ''
82
+ } > => super.delegate('${ operation } ', '${ field . name } ', args, {}, info)`
83
+ } )
84
+ . join ( ',\n' )
75
85
}
76
86
77
87
function renderScalarType ( type : GraphQLScalarType ) : string {
78
- if ( type . name === 'ID' ) { return renderIDType ( type ) }
79
- return `${ type . description ? `/*
88
+ if ( type . name === 'ID' ) {
89
+ return renderIDType ( type )
90
+ }
91
+ return `${
92
+ type . description
93
+ ? `/*
80
94
${ type . description }
81
95
*/
82
- ` : '' } export type ${ type . name } = ${ scalarMapping [ type . name ] || 'string' } `
96
+ `
97
+ : ''
98
+ } export type ${ type . name } = ${ scalarMapping [ type . name ] || 'string' } `
83
99
}
84
100
85
101
function renderIDType ( type : GraphQLScalarType ) : string {
86
- return `${ type . description ? `/*
102
+ return `${
103
+ type . description
104
+ ? `/*
87
105
${ type . description }
88
106
*/
89
- ` : '' } export type ${ type . name } _Input = ${ scalarMapping [ type . name ] || 'string' }
107
+ `
108
+ : ''
109
+ } export type ${ type . name } _Input = ${ scalarMapping [ type . name ] || 'string' }
90
110
export type ${ type . name } _Output = string`
91
111
}
92
112
93
113
function renderEnumType ( type : GraphQLEnumType ) : string {
94
114
return `${ renderDescription ( type . description ) } export type ${ type . name } =
95
- ${ type . getValues ( ) . map ( e => ` '${ e . name } '` ) . join ( ' |\n' ) } `
115
+ ${ type
116
+ . getValues ( )
117
+ . map ( e => ` '${ e . name } '` )
118
+ . join ( ' |\n' ) } `
96
119
}
97
-
120
+
98
121
function renderRootType ( type : GraphQLObjectType ) : string {
99
- const fieldDefinition = Object . keys ( type . getFields ( ) ) . map ( f => {
100
- const field = type . getFields ( ) [ f ]
101
- return ` ${ field . name } : (args: {${ field . args . length > 0 ? ' ' : '' } ${ field . args . map ( f => `${ renderFieldName ( f ) } : ${ renderFieldType ( f . type ) } ` ) . join ( ', ' ) } ${ field . args . length > 0 ? ' ' : '' } }, info?: GraphQLResolveInfo | string) => Promise<${ renderFieldType ( field . type ) } ${ ! isNonNullType ( field . type ) ? ' | null' : '' } >`
102
- } ) . join ( '\n' )
122
+ const fieldDefinition = Object . keys ( type . getFields ( ) )
123
+ . map ( f => {
124
+ const field = type . getFields ( ) [ f ]
125
+ return ` ${ field . name } : (args: {${ field . args . length > 0 ? ' ' : '' } ${ field . args
126
+ . map ( f => `${ renderFieldName ( f ) } : ${ renderFieldType ( f . type ) } ` )
127
+ . join ( ', ' ) } ${
128
+ field . args . length > 0 ? ' ' : ''
129
+ } }, info?: GraphQLResolveInfo | string) => Promise<${ renderFieldType ( field . type ) } ${
130
+ ! isNonNullType ( field . type ) ? ' | null' : ''
131
+ } >`
132
+ } )
133
+ . join ( '\n' )
103
134
104
135
return renderTypeWrapper ( type . name , type . description , fieldDefinition )
105
136
}
106
137
107
138
function renderUnionType ( type : GraphQLUnionType ) : string {
108
- return `${ renderDescription ( type . description ) } export type ${ type . name } = ${ type . getTypes ( ) . map ( t => t . name ) . join ( ' | ' ) } `
109
- }
110
-
111
- function renderObjectType ( type : GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType ) : string {
112
- const fieldDefinition = Object . keys ( type . getFields ( ) ) . map ( f => {
113
- const field = type . getFields ( ) [ f ]
114
- return ` ${ renderFieldName ( field ) } : ${ renderFieldType ( field . type ) } `
115
- } ) . join ( '\n' )
139
+ return `${ renderDescription ( type . description ) } export type ${ type . name } = ${ type
140
+ . getTypes ( )
141
+ . map ( t => t . name )
142
+ . join ( ' | ' ) } `
143
+ }
144
+
145
+ function renderObjectType (
146
+ type : GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType
147
+ ) : string {
148
+ const fieldDefinition = Object . keys ( type . getFields ( ) )
149
+ . map ( f => {
150
+ const field = type . getFields ( ) [ f ]
151
+ return ` ${ renderFieldName ( field ) } : ${ renderFieldType ( field . type ) } `
152
+ } )
153
+ . join ( '\n' )
116
154
117
155
let interfaces : GraphQLInterfaceType [ ] = [ ]
118
156
if ( type instanceof GraphQLObjectType ) {
@@ -122,11 +160,15 @@ function renderObjectType(type: GraphQLObjectType | GraphQLInputObjectType | Gra
122
160
return renderInterfaceWrapper ( type . name , type . description , interfaces , fieldDefinition )
123
161
}
124
162
125
- function renderInputObjectType ( type : GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType ) : string {
126
- const fieldDefinition = Object . keys ( type . getFields ( ) ) . map ( f => {
127
- const field = type . getFields ( ) [ f ]
128
- return ` ${ renderFieldName ( field ) } : ${ renderInputFieldType ( field . type ) } `
129
- } ) . join ( '\n' )
163
+ function renderInputObjectType (
164
+ type : GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType
165
+ ) : string {
166
+ const fieldDefinition = Object . keys ( type . getFields ( ) )
167
+ . map ( f => {
168
+ const field = type . getFields ( ) [ f ]
169
+ return ` ${ renderFieldName ( field ) } : ${ renderInputFieldType ( field . type ) } `
170
+ } )
171
+ . join ( '\n' )
130
172
131
173
let interfaces : GraphQLInterfaceType [ ] = [ ]
132
174
if ( type instanceof GraphQLObjectType ) {
@@ -139,37 +181,58 @@ function renderInputObjectType(type: GraphQLObjectType | GraphQLInputObjectType
139
181
function renderFieldName ( field : GraphQLInputField | GraphQLField < any , any > ) {
140
182
return `${ field . name } ${ isNonNullType ( field . type ) ? '' : '?' } `
141
183
}
142
-
184
+
143
185
function renderFieldType ( type : GraphQLInputType | GraphQLOutputType ) {
144
186
if ( isNonNullType ( type ) ) {
145
187
return renderFieldType ( ( type as GraphQLWrappingType ) . ofType )
146
- }
188
+ }
147
189
if ( isListType ( type ) ) {
148
190
return `Array<${ renderFieldType ( ( type as GraphQLWrappingType ) . ofType ) } >`
149
191
}
150
- return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name == 'ID' ? '_Output' : '' } `
192
+ return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name === 'ID' ? '_Output' : '' } `
151
193
}
152
194
153
195
function renderInputFieldType ( type : GraphQLInputType | GraphQLOutputType ) {
154
196
if ( isNonNullType ( type ) ) {
155
197
return renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType )
156
- }
198
+ }
157
199
if ( isListType ( type ) ) {
158
- return `Array<${ renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType ) } > | ${ renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType ) } `
200
+ return `Array<${ renderInputFieldType (
201
+ ( type as GraphQLWrappingType ) . ofType
202
+ ) } > | ${ renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType ) } `
159
203
}
160
- return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name == 'ID' ? '_Input' : '' } `
204
+ return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name === 'ID' ? '_Input' : '' } `
161
205
}
162
-
163
- function renderSchemaInterface ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
206
+
207
+ function renderSchemaInterface (
208
+ queryType : GraphQLObjectType ,
209
+ mutationType ?: GraphQLObjectType | null ,
210
+ subscriptionType ?: GraphQLObjectType | null
211
+ ) {
164
212
return `export interface Schema {
165
213
query: ${ queryType . name }
166
- ${ mutationType ? ` mutation: ${ mutationType . name }
167
- ` : '' } ${ subscriptionType ? ` subscription: ${ subscriptionType . name }
168
- ` : '' } }`
169
- }
170
-
171
- function renderInterfaceWrapper ( typeName : string , typeDescription : string , interfaces : GraphQLInterfaceType [ ] , fieldDefinition : string ) : string {
172
- return `${ renderDescription ( typeDescription ) } export interface ${ typeName } ${ interfaces . length > 0 ? ` extends ${ interfaces . map ( i => i . name ) . join ( ', ' ) } ` : '' } {
214
+ ${
215
+ mutationType
216
+ ? ` mutation: ${ mutationType . name }
217
+ `
218
+ : ''
219
+ } ${
220
+ subscriptionType
221
+ ? ` subscription: ${ subscriptionType . name }
222
+ `
223
+ : ''
224
+ } }`
225
+ }
226
+
227
+ function renderInterfaceWrapper (
228
+ typeName : string ,
229
+ typeDescription : string ,
230
+ interfaces : GraphQLInterfaceType [ ] ,
231
+ fieldDefinition : string
232
+ ) : string {
233
+ return `${ renderDescription ( typeDescription ) } export interface ${ typeName } ${
234
+ interfaces . length > 0 ? ` extends ${ interfaces . map ( i => i . name ) . join ( ', ' ) } ` : ''
235
+ } {
173
236
${ fieldDefinition }
174
237
}`
175
238
}
@@ -181,8 +244,12 @@ ${fieldDefinition}
181
244
}
182
245
183
246
function renderDescription ( description ?: string ) {
184
- return `${ description ? `/*
247
+ return `${
248
+ description
249
+ ? `/*
185
250
${ description . split ( '\n' ) . map ( l => ` * ${ l } \n` ) }
186
251
*/
187
- ` : '' } `
188
- }
252
+ `
253
+ : ''
254
+ } `
255
+ }
0 commit comments