Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.

Commit 3ca4b70

Browse files
committed
feat(graphcool-ts): split ID type into ID_Input and ID_Output
GraphQL ID types can be string or number, but are always returned as string. Typing the output type as string makes it easier to use the return values. Closes #12
1 parent 0389332 commit 3ca4b70

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/generators/graphcool-ts.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Generator } from '../types'
2323
export const generator: Generator = {
2424
GraphQLUnionType: renderUnionType,
2525
GraphQLObjectType: renderObjectType,
26-
GraphQLInputObjectType: renderObjectType,
26+
GraphQLInputObjectType: renderInputObjectType,
2727
GraphQLScalarType: renderScalarType,
2828
GraphQLEnumType: renderEnumType,
2929
GraphQLInterfaceType: renderObjectType,
@@ -75,12 +75,21 @@ export function renderMainMethodFields(operation: string, fields: GraphQLFieldMa
7575
}
7676

7777
function renderScalarType(type: GraphQLScalarType): string {
78+
if (type.name === 'ID') { return renderIDType(type)}
7879
return `${type.description ? `/*
7980
${type.description}
8081
*/
8182
`: ''}export type ${type.name} = ${scalarMapping[type.name] || 'string'}`
8283
}
8384

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+
8493
function renderEnumType(type: GraphQLEnumType): string {
8594
return `${renderDescription(type.description)}export type ${type.name} =
8695
${type.getValues().map(e => ` '${e.name}'`).join(' |\n')}`
@@ -113,6 +122,20 @@ function renderObjectType(type: GraphQLObjectType | GraphQLInputObjectType | Gra
113122
return renderInterfaceWrapper(type.name, type.description, interfaces, fieldDefinition)
114123
}
115124

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+
116139

117140

118141
function renderFieldName(field: GraphQLInputField | GraphQLField<any, any>) {
@@ -126,7 +149,17 @@ function renderFieldType(type: GraphQLInputType | GraphQLOutputType) {
126149
if (isListType(type)) {
127150
return `Array<${renderFieldType((type as GraphQLWrappingType).ofType)}>`
128151
}
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' : ''}`
130163
}
131164

132165
function renderSchemaInterface(queryType: GraphQLObjectType, mutationType?: GraphQLObjectType | null, subscriptionType?: GraphQLObjectType | null) {

0 commit comments

Comments
 (0)