Hi!
I know it was discussed and closed here https://github.com/dotansimha/graphql-code-generator/issues/138
However I think the current implementation is not totally correct
By default, every type is nullable - it's legitimate to return null as any of the scalar types.
from http://graphql.org/graphql-js/basic-types/
Now when generating interfaces it marks the field optional but the value remains non-nullable.
Optional field is good for convinience (as discussed in issue 138), but non-nullable value leaves us to return either value or undefined. And we can't return null, which is against the spec.
// schema.gql
extend type Query {
nutrient(_id: String): Nutrient
}
// types.generated.ts
export interface Query {
nutrient?: Nutrient;
}
// resolver.ts
const Query: TypedResolver<Types.Query> = {
nutrient: async (root, ctx) => {
const result = await db.nutrients.findOne({ _id: new ObjectID(ctx._id) }); **// Promise(Nutruent | null)**
return result || undefined; **// ugly trick**
}
};
IMHO, the solution would be to generate types like this:
// types.generated.ts
export interface Query {
nutrient?: Nutrient | null;
}
Thanks!
It makes sense. Fixing it now!
@rasdaniil @altschuler fixed in 0.8.14 :)
Thanks @dotansimha !
Most helpful comment
@rasdaniil @altschuler fixed in 0.8.14 :)