Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Allow undefined for nullable graphql types #331

Merged
merged 9 commits into from
Jan 5, 2019
39 changes: 20 additions & 19 deletions packages/graphqlgen/src/generators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,26 @@ export function printFieldLikeType(
field: GraphQLTypeField,
modelMap: ModelMap,
) {
const isNullable =
field.defaultValue === null ||
(!field.type.isRequired && field.defaultValue === undefined)

if (field.type.isScalar) {
return `${getTypeFromGraphQLType(field.type.name)}${
field.type.isArray ? '[]' : ''
}${isNullable ? '| null' : ''}`
}

if (field.type.isInput || field.type.isEnum) {
return `${field.type.name}${field.type.isArray ? '[]' : ''}${
isNullable ? '| null' : ''
}`
}

return `${getModelName(field.type, modelMap)}${
field.type.isArray ? '[]' : ''
}${isNullable ? '| null' : ''}`
const name = field.type.isScalar
? getTypeFromGraphQLType(field.type.name)
: field.type.isInput || field.type.isEnum
? field.type.name
: getModelName(field.type, modelMap)

const isRequired = field.type.isArray
? field.type.isArrayRequired
: field.type.isRequired
const suffix = isRequired
? ''
: field.defaultValue === null
? ' | null'
: field.defaultValue === undefined
? ' | null | undefined'
: ''
const type = field.type.isArray
? name + (field.type.isRequired ? '' : ' | null | undefined')
: name + suffix
return field.type.isArray ? `Array<${type}>${suffix}` : type
}

export function getTypeFromGraphQLType(
Expand Down
11 changes: 9 additions & 2 deletions packages/graphqlgen/src/source-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type GraphQLTypeDefinition = {

export type GraphQLType = GraphQLTypeDefinition & {
isArray: boolean
isArrayRequired: boolean
isRequired: boolean
}

Expand Down Expand Up @@ -83,6 +84,7 @@ export type GraphQLUnionObject = {
interface FinalType {
isRequired: boolean
isArray: boolean
isArrayRequired: boolean
type: GraphQLInputType | GraphQLOutputType
}

Expand Down Expand Up @@ -130,13 +132,15 @@ function extractTypeDefinition(

const getFinalType = (
type: GraphQLInputType | GraphQLOutputType,
acc: FinalType = { isArray: false, isRequired: false, type },
acc: FinalType = { isArray: false, isArrayRequired: false, isRequired: false, type },
): FinalType => {
if (type instanceof GraphQLNonNull) {
acc.isRequired = true
}
if (type instanceof GraphQLList) {
acc.isArray = true
acc.isArrayRequired = acc.isRequired
acc.isRequired = false
}

if (type instanceof GraphQLNonNull || type instanceof GraphQLList) {
Expand All @@ -157,13 +161,16 @@ function extractTypeLike(
type: GraphQLInputType | GraphQLOutputType,
): GraphQLType {
const typeLike: GraphQLType = {} as GraphQLType
const { isArray, isRequired, type: finalType } = getFinalType(type)
const { isArray, isArrayRequired, isRequired, type: finalType } = getFinalType(type)
if (isRequired) {
typeLike.isRequired = true
}
if (isArray) {
typeLike.isArray = true
}
if (isArrayRequired) {
typeLike.isArrayRequired = true
}
if (
finalType instanceof GraphQLObjectType ||
finalType instanceof GraphQLInterfaceType ||
Expand Down
Loading