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
1918} from 'graphql'
2019
2120import { Generator } from '../types'
2221
2322export const generator : Generator = {
2423 GraphQLUnionType : renderUnionType ,
2524 GraphQLObjectType : renderObjectType ,
26- GraphQLInputObjectType : renderObjectType ,
25+ GraphQLInputObjectType : renderInputObjectType ,
2726 GraphQLScalarType : renderScalarType ,
2827 GraphQLEnumType : renderEnumType ,
2928 GraphQLInterfaceType : renderObjectType ,
3029 RootType : renderRootType ,
3130 SchemaType : renderSchemaInterface ,
3231 Main : renderMainMethod ,
33- Header : renderHeader ,
32+ Header : renderHeader
3433}
3534
3635const scalarMapping = {
@@ -49,7 +48,11 @@ const typeDefs = \`
4948${ schema } \``
5049}
5150
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+ ) {
5356 return `export class Graphcool extends BaseGraphcool {
5457
5558 constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) {
@@ -58,52 +61,96 @@ function renderMainMethod(queryType: GraphQLObjectType, mutationType?: GraphQLOb
5861
5962 query: Query = {
6063${ renderMainMethodFields ( 'query' , queryType . getFields ( ) ) }
61- }${ mutationType ? `
64+ }${
65+ mutationType
66+ ? `
6267
6368 mutation: Mutation = {
6469${ renderMainMethodFields ( 'mutation' , mutationType . getFields ( ) ) }
65- }` : '' }
70+ }`
71+ : ''
72+ }
6673}`
6774}
6875
69-
7076export 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' )
7585}
7686
7787function renderScalarType ( type : GraphQLScalarType ) : string {
78- return `${ type . description ? `/*
88+ if ( type . name === 'ID' ) {
89+ return renderIDType ( type )
90+ }
91+ return `${
92+ type . description
93+ ? `/*
7994${ type . description }
8095*/
81- ` : '' } export type ${ type . name } = ${ scalarMapping [ type . name ] || 'string' } `
96+ `
97+ : ''
98+ } export type ${ type . name } = ${ scalarMapping [ type . name ] || 'string' } `
99+ }
100+
101+ function renderIDType ( type : GraphQLScalarType ) : string {
102+ return `${
103+ type . description
104+ ? `/*
105+ ${ type . description }
106+ */
107+ `
108+ : ''
109+ } export type ${ type . name } _Input = ${ scalarMapping [ type . name ] || 'string' }
110+ export type ${ type . name } _Output = string`
82111}
83112
84113function renderEnumType ( type : GraphQLEnumType ) : string {
85114 return `${ renderDescription ( type . description ) } export type ${ type . name } =
86- ${ type . getValues ( ) . map ( e => ` '${ e . name } '` ) . join ( ' |\n' ) } `
115+ ${ type
116+ . getValues ( )
117+ . map ( e => ` '${ e . name } '` )
118+ . join ( ' |\n' ) } `
87119}
88-
120+
89121function renderRootType ( type : GraphQLObjectType ) : string {
90- const fieldDefinition = Object . keys ( type . getFields ( ) ) . map ( f => {
91- const field = type . getFields ( ) [ f ]
92- 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' : '' } >`
93- } ) . 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' )
94134
95135 return renderTypeWrapper ( type . name , type . description , fieldDefinition )
96136}
97137
98138function renderUnionType ( type : GraphQLUnionType ) : string {
99- return `${ renderDescription ( type . description ) } export type ${ type . name } = ${ type . getTypes ( ) . map ( t => t . name ) . join ( ' | ' ) } `
139+ return `${ renderDescription ( type . description ) } export type ${ type . name } = ${ type
140+ . getTypes ( )
141+ . map ( t => t . name )
142+ . join ( ' | ' ) } `
100143}
101-
102- function renderObjectType ( type : GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType ) : string {
103- const fieldDefinition = Object . keys ( type . getFields ( ) ) . map ( f => {
104- const field = type . getFields ( ) [ f ]
105- return ` ${ renderFieldName ( field ) } : ${ renderFieldType ( field . type ) } `
106- } ) . join ( '\n' )
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' )
107154
108155 let interfaces : GraphQLInterfaceType [ ] = [ ]
109156 if ( type instanceof GraphQLObjectType ) {
@@ -113,32 +160,79 @@ function renderObjectType(type: GraphQLObjectType | GraphQLInputObjectType | Gra
113160 return renderInterfaceWrapper ( type . name , type . description , interfaces , fieldDefinition )
114161}
115162
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' )
172+
173+ let interfaces : GraphQLInterfaceType [ ] = [ ]
174+ if ( type instanceof GraphQLObjectType ) {
175+ interfaces = type . getInterfaces ( )
176+ }
177+
178+ return renderInterfaceWrapper ( type . name , type . description , interfaces , fieldDefinition )
179+ }
116180
117-
118181function renderFieldName ( field : GraphQLInputField | GraphQLField < any , any > ) {
119182 return `${ field . name } ${ isNonNullType ( field . type ) ? '' : '?' } `
120183}
121-
184+
122185function renderFieldType ( type : GraphQLInputType | GraphQLOutputType ) {
123186 if ( isNonNullType ( type ) ) {
124187 return renderFieldType ( ( type as GraphQLWrappingType ) . ofType )
125- }
188+ }
126189 if ( isListType ( type ) ) {
127190 return `Array<${ renderFieldType ( ( type as GraphQLWrappingType ) . ofType ) } >`
128191 }
129- return ( type as GraphQLNamedType ) . name
192+ return ` ${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name === 'ID' ? '_Output' : '' } `
130193}
131-
132- function renderSchemaInterface ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
194+
195+ function renderInputFieldType ( type : GraphQLInputType | GraphQLOutputType ) {
196+ if ( isNonNullType ( type ) ) {
197+ return renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType )
198+ }
199+ if ( isListType ( type ) ) {
200+ return `Array<${ renderInputFieldType (
201+ ( type as GraphQLWrappingType ) . ofType
202+ ) } > | ${ renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType ) } `
203+ }
204+ return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name === 'ID' ? '_Input' : '' } `
205+ }
206+
207+ function renderSchemaInterface (
208+ queryType : GraphQLObjectType ,
209+ mutationType ?: GraphQLObjectType | null ,
210+ subscriptionType ?: GraphQLObjectType | null
211+ ) {
133212 return `export interface Schema {
134213 query: ${ queryType . name }
135- ${ mutationType ? ` mutation: ${ mutationType . name }
136- ` : '' } ${ subscriptionType ? ` subscription: ${ subscriptionType . name }
137- ` : '' } }`
214+ ${
215+ mutationType
216+ ? ` mutation: ${ mutationType . name }
217+ `
218+ : ''
219+ } ${
220+ subscriptionType
221+ ? ` subscription: ${ subscriptionType . name }
222+ `
223+ : ''
224+ } }`
138225}
139-
140- function renderInterfaceWrapper ( typeName : string , typeDescription : string , interfaces : GraphQLInterfaceType [ ] , fieldDefinition : string ) : string {
141- return `${ renderDescription ( typeDescription ) } export interface ${ typeName } ${ interfaces . length > 0 ? ` extends ${ interfaces . map ( i => i . name ) . join ( ', ' ) } ` : '' } {
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+ } {
142236${ fieldDefinition }
143237}`
144238}
@@ -150,8 +244,12 @@ ${fieldDefinition}
150244}
151245
152246function renderDescription ( description ?: string ) {
153- return `${ description ? `/*
247+ return `${
248+ description
249+ ? `/*
154250${ description . split ( '\n' ) . map ( l => ` * ${ l } \n` ) }
155251 */
156- ` : '' } `
157- }
252+ `
253+ : ''
254+ } `
255+ }
0 commit comments