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

object validation creates misleading non null error #1989

Closed
mschipperheyn opened this issue Jun 24, 2019 · 13 comments
Closed

object validation creates misleading non null error #1989

mschipperheyn opened this issue Jun 24, 2019 · 13 comments
Labels

Comments

@mschipperheyn
Copy link

mschipperheyn commented Jun 24, 2019

What is happening

A non null error is reported for a field that is not null, e.g.
Cannot return null for non-nullable field Account.id.

What is really happening

When you dig into graphql code and start console.logging intermediate results, you start seeing very different things:

Scenario 1
Expected a value of type "PostRegistration" but received: "TERMS"

So, in this case, I had added a value to an ENUM field in the database but forgot to add it to the graphql enum, hence the error. Impossible to determine from the provided error message (cannot return null ...).

Scenario 2
non nullable field Account.foo was null

In this case, another required key on the same object was provided with a null value. It seems obvious, but the error message is so specific, that you can easily miss this.

In short, a whole class of field validation errors, is being aggregated to a single highly specific message that is plain wrong.

@tedb19
Copy link

tedb19 commented Oct 7, 2019

any progress on this?

@djay05554
Copy link

Hello, guys

I can't find the solution to the bug.
I correctly confirm my schema but there is no wrong.

@timbrownsf
Copy link

Any recommendations on how to debug this if it is happening and figure out the actual error?

@mschipperheyn
Copy link
Author

@timbrownsf Yeah,
Easy: add console.log of the error at the error the erronous error occurs so you can see what the actual error is
Harder but better. Use server side inspect and add a breakpoint there.

@emma255
Copy link

emma255 commented Jan 23, 2020

this one works well return Customer::findOrFail($args['id']);
and this one not brings that error return Customer::where('email', $args['email'])->get();

So for myself i think there is an issue on handling exceptions in case the second query above fails, as compared to the first query above that whether find or fail. so am continuing digging up whether i will get the solution of it..

@vanthome
Copy link

That is really annoying, very hard to trace errors. I see something like this a good remedyhttps://github.com//pull/402
With that we could add proper logging for field level resolver throws.

@nidshar
Copy link

nidshar commented Mar 31, 2020

Any updates on this one?

@allaniftrue
Copy link

allaniftrue commented Apr 4, 2020

apollographql/apollo-client#4180 (comment), this reply helped solve the issue

@evifere
Copy link

evifere commented Apr 16, 2020

I bypass the problem using JSON.parse and JSON.stringify like this.

  @Query(returns => Film, { nullable: false })
  async film(
    @Arg("episode_id")
    episode_id: number
  ) {
    let film = await FilmsModel.findOne({episode_id:episode_id});
    return JSON.parse(JSON.stringify(film)) ;
  }

@vipiny35
Copy link

I bypass the problem using JSON.parse and JSON.stringify like this.

  @Query(returns => Film, { nullable: false })
  async film(
    @Arg("episode_id")
    episode_id: number
  ) {
    let film = await FilmsModel.findOne({episode_id:episode_id});
    return JSON.parse(JSON.stringify(film)) ;
  }

Didn't work. Any other work-around? I tried using lean() and toObject() but with no luck!

@fromi
Copy link

fromi commented Jun 29, 2020

@vipiny35 Not sure if your problem is the same, but if you want to access sub-objects functions you have to do something like this:

  @Query(() => Game)
  async game(@Arg('id') id: string) {
    return GameModel.findById(id)
  }

  @FieldResolver(() => GraphQLJSON, {nullable: true})
  setup(@Root() game: Game, @Ctx('userId') userId: string) {
    const player = game.players.find(player => toObjectType(Player, player).isControlledBy(userId))
    ...
  }

export default function toObjectType<T>(Type: new (...args: any[]) => T, object: T) {
  return Object.assign(new Type(), object);
}

You would expect "game" to be the Document from mongoose here, however in the FieldResolver it is some flat data objet, not a class anymore.

@yaacovCR
Copy link
Contributor

@mschipperheyn Can you provide a reproducible example? (3 years later!!!)

For posterity: when we bubble up nulls, the original error is still supposed to be included in the errors array for this exact purpose. So this problem at first glance seems surprising. I may have misunderstood, of course!

@yaacovCR
Copy link
Contributor

yaacovCR commented Oct 7, 2024

Closing this old issue. Feel free to reopen as necessary.

@yaacovCR yaacovCR closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests