@@ -23,7 +23,7 @@ import { Generator } from '../types'
23
23
export const generator : Generator = {
24
24
GraphQLUnionType : renderUnionType ,
25
25
GraphQLObjectType : renderObjectType ,
26
- GraphQLInputObjectType : renderObjectType ,
26
+ GraphQLInputObjectType : renderInputObjectType ,
27
27
GraphQLScalarType : renderScalarType ,
28
28
GraphQLEnumType : renderEnumType ,
29
29
GraphQLInterfaceType : renderObjectType ,
@@ -75,12 +75,21 @@ export function renderMainMethodFields(operation: string, fields: GraphQLFieldMa
75
75
}
76
76
77
77
function renderScalarType ( type : GraphQLScalarType ) : string {
78
+ if ( type . name === 'ID' ) { return renderIDType ( type ) }
78
79
return `${ type . description ? `/*
79
80
${ type . description }
80
81
*/
81
82
` : '' } export type ${ type . name } = ${ scalarMapping [ type . name ] || 'string' } `
82
83
}
83
84
85
+ function renderIDType ( type : GraphQLScalarType ) : string {
86
+ return `${ type . description ? `/*
87
+ ${ type . description }
88
+ */
89
+ ` : '' } export type ${ type . name } _Input = ${ scalarMapping [ type . name ] || 'string' }
90
+ export type ${ type . name } _Output = string`
91
+ }
92
+
84
93
function renderEnumType ( type : GraphQLEnumType ) : string {
85
94
return `${ renderDescription ( type . description ) } export type ${ type . name } =
86
95
${ type . getValues ( ) . map ( e => ` '${ e . name } '` ) . join ( ' |\n' ) } `
@@ -113,6 +122,20 @@ function renderObjectType(type: GraphQLObjectType | GraphQLInputObjectType | Gra
113
122
return renderInterfaceWrapper ( type . name , type . description , interfaces , fieldDefinition )
114
123
}
115
124
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' )
130
+
131
+ let interfaces : GraphQLInterfaceType [ ] = [ ]
132
+ if ( type instanceof GraphQLObjectType ) {
133
+ interfaces = type . getInterfaces ( )
134
+ }
135
+
136
+ return renderInterfaceWrapper ( type . name , type . description , interfaces , fieldDefinition )
137
+ }
138
+
116
139
117
140
118
141
function renderFieldName ( field : GraphQLInputField | GraphQLField < any , any > ) {
@@ -126,7 +149,17 @@ function renderFieldType(type: GraphQLInputType | GraphQLOutputType) {
126
149
if ( isListType ( type ) ) {
127
150
return `Array<${ renderFieldType ( ( type as GraphQLWrappingType ) . ofType ) } >`
128
151
}
129
- return ( type as GraphQLNamedType ) . name
152
+ return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name == 'ID' ? '_Output' : '' } `
153
+ }
154
+
155
+ function renderInputFieldType ( type : GraphQLInputType | GraphQLOutputType ) {
156
+ if ( isNonNullType ( type ) ) {
157
+ return renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType )
158
+ }
159
+ if ( isListType ( type ) ) {
160
+ return `Array<${ renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType ) } > | ${ renderInputFieldType ( ( type as GraphQLWrappingType ) . ofType ) } `
161
+ }
162
+ return `${ ( type as GraphQLNamedType ) . name } ${ ( type as GraphQLNamedType ) . name == 'ID' ? '_Input' : '' } `
130
163
}
131
164
132
165
function renderSchemaInterface ( queryType : GraphQLObjectType , mutationType ?: GraphQLObjectType | null , subscriptionType ?: GraphQLObjectType | null ) {
0 commit comments