Type-graphql: Default nullability

Created on 31 Mar 2019  ·  6Comments  ·  Source: MichalLytek/type-graphql

GraphQL schema fields are nullable by default, but using TypeGraphQL everything is required by default. Is there a way to change this default?

If I make a GQL request and a certain field is null, the server throws an error. Getting a null for that field instead of an exception seems to be far better default behaviour.

Community Enhancement

Most helpful comment

Fields are not nullable by default because properties in TypeScript are not optional by default, so it's much more intuitive design.

Also marking ! and !]! on almost every field in my schema SDL drove me crazy, so I've decided that fields are not nullable by default.

With #296 the problem will be gone as you will have 1:1 sync with TS types 😉

Is there a way to change this default?

You can create a decorator alias: const NullableField = () => Field({ nullable: true });.
Maybe I should provide it in out of the box 🤔 Or maybe even a helper to create decorator aliases (they always have overloads with typeFunc and options).

All 6 comments

Fields are not nullable by default because properties in TypeScript are not optional by default, so it's much more intuitive design.

Also marking ! and !]! on almost every field in my schema SDL drove me crazy, so I've decided that fields are not nullable by default.

With #296 the problem will be gone as you will have 1:1 sync with TS types 😉

Is there a way to change this default?

You can create a decorator alias: const NullableField = () => Field({ nullable: true });.
Maybe I should provide it in out of the box 🤔 Or maybe even a helper to create decorator aliases (they always have overloads with typeFunc and options).

Thanks for the response. I’m a little confused by the second paragraph.
Surely making things non nullable by default means that you have !
everywhere?

On Sun, 31 Mar 2019 at 22:36 Michał Lytek notifications@github.com wrote:

Fields are not nullable by default because properties in TypeScript are
not optional by default, so it's much more intuitive design.

Also marking ! and !]! almost everything in my schema SDL drove me crazy,
so I've decided that fields are not nullable by default.

With #296 https://github.com/19majkel94/type-graphql/issues/296 the
problem will be gone as you will have 1:1 sync with TS types :P

Is there a way to change this default?

You can create a decorator alias: const NullableField = () => Field({
nullable: true });.
Maybe I should provide it in out of the box 🤔


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/19majkel94/type-graphql/issues/297#issuecomment-478373283,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AC8oXw0BvPduRSEMKocRwnnpSKmPMzEWks5vcQ4rgaJpZM4cUVIP
.

@elie222

class Foo {
  bar: string;
  baz: string[];
  fooBar(baz: string): string {}
}
type Foo {
  bar: String!
  baz: [String!]!
  fooBar(baz: String!): String!
}

I’m a little confused by the second paragraph.

I am trying to say that before TypeGraphQL I was creating GraphQL APIs using SDL and I was forgetting about placing ! in types like string array ([String!]!) which was a bad developer experience for me.

@elie222
I think that I shouldn't force this behavior, it should be configurable with current setting as a default one, even with #45.

Will try to make this possible soon 😉

Thanks for the great work!

On Thu, 4 Apr 2019 at 17:33 Michał Lytek notifications@github.com wrote:

@elie222 https://github.com/elie222
I think that I shouldn't force this behavior, it should be configurable
with current setting as a default one, event with #45
https://github.com/19majkel94/type-graphql/issues/45.

Will try to make this possible soon 😉


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/19majkel94/type-graphql/issues/297#issuecomment-479922576,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AC8oX5xVVBbq7I10l3ssQyyzkcQQxZqzks5vdg00gaJpZM4cUVIP
.

@19majkel94
Right improvement! 👍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

limenutt picture limenutt  ·  3Comments

MichalLytek picture MichalLytek  ·  4Comments

MichalLytek picture MichalLytek  ·  3Comments

Tybot204 picture Tybot204  ·  3Comments

MichalLytek picture MichalLytek  ·  3Comments