Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default nullability #297

Closed
elie222 opened this issue Mar 31, 2019 · 6 comments
Closed

Default nullability #297

elie222 opened this issue Mar 31, 2019 · 6 comments
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Milestone

Comments

@elie222
Copy link

elie222 commented Mar 31, 2019

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.

@MichalLytek
Copy link
Owner

MichalLytek commented Mar 31, 2019

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).

@MichalLytek MichalLytek added the Question ❔ Not future request, proposal or bug issue label Mar 31, 2019
@elie222
Copy link
Author

elie222 commented Mar 31, 2019 via email

@MichalLytek
Copy link
Owner

@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.

@MichalLytek
Copy link
Owner

MichalLytek commented Apr 4, 2019

@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 😉

@MichalLytek MichalLytek added Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request and removed Question ❔ Not future request, proposal or bug issue labels Apr 4, 2019
@MichalLytek MichalLytek added this to the 1.0.0 release milestone Apr 4, 2019
@MichalLytek MichalLytek changed the title Nullable by default Default nullability Apr 4, 2019
@elie222
Copy link
Author

elie222 commented Apr 4, 2019 via email

@nodkz
Copy link

nodkz commented May 7, 2019

@19majkel94
Right improvement! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants