Skip to content

Commit

Permalink
chore: fix generated golang on unsupported graphql types
Browse files Browse the repository at this point in the history
  • Loading branch information
ctison committed Apr 17, 2023
1 parent 9e66c5f commit fd481fc
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions packages/graphql-codegen-golang/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ export class GolangGenerator {
const goType: string = this.types[node.name.value] as string
l.push('', `type ${goType} struct {`)
node.fields?.forEach(field => {
l.push(this._generateField(field.name.value, field.type))
try {
l.push(this._generateField(field.name.value, field.type))
} catch (e) {
if (e instanceof UnsupportedTypeError) {
console.debug(`Skipping ${e.message}`)
} else {
throw e
}
}
})
l.push('}')
})
Expand All @@ -215,7 +223,15 @@ export class GolangGenerator {
const goType: string = this.types[node.name.value] as string
l.push('', `type ${goType} struct {`)
node.fields?.forEach(field => {
l.push(this._generateField(field.name.value, field.type))
try {
l.push(this._generateField(field.name.value, field.type))
} catch (e) {
if (e instanceof UnsupportedTypeError) {
console.debug(`Skipping ${e.message}`)
} else {
throw e
}
}
})
l.push('}')
})
Expand Down Expand Up @@ -267,8 +283,9 @@ export class GolangGenerator {
nonNull
)
}
console.debug(`Skipped unsupported field type "${type.name.value}"`)
return ''
throw new UnsupportedTypeError(
`unsupported field type "${type.name.value}"`
)
}

/**
Expand Down Expand Up @@ -370,7 +387,15 @@ export class GolangGenerator {
}
const l: string[] = [`type ${name}Variables struct {`]
variableDefinitions.forEach(variable => {
l.push(this._generateField(variable.variable.name.value, variable.type))
try {
l.push(this._generateField(variable.variable.name.value, variable.type))
} catch (e) {
if (e instanceof UnsupportedTypeError) {
console.debug(`Skipping ${e.message}`)
} else {
throw e
}
}
})
l.push('}', '')
return l
Expand Down Expand Up @@ -399,3 +424,10 @@ export class GolangGenerator {
return l
}
}

class UnsupportedTypeError extends Error {
constructor(msg: string) {
super(msg)
Object.setPrototypeOf(this, UnsupportedTypeError.prototype)
}
}

0 comments on commit fd481fc

Please sign in to comment.