Howtographql: [Dataloader] The loader.load() function must be called with a value,but got: null.

Created on 31 Jul 2017  路  8Comments  路  Source: howtographql/howtographql

I got an issue at this step
https://www.howtographql.com/graphql-js/7-using-data-loaders/

When I finished the task and make query is fine but when I tried to mutation, issue happened:

mutation {
  createLink(url: "http://something.com", description: "dfdaf") {
    postedBy {
      name
    }
  }
}

An error message happens

{
  "data": {
    "createLink": {
      "postedBy": null
    }
  },
  "errors": [
    {
      "message": "The loader.load() function must be called with a value,but got: null.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "createLink",
        "postedBy"
      ]
    }
  ]
}

I did enhance authentication & authorization with express-jwt so what I must define more in the loader services or I missed something?
Update
I did drop all database in mongodb and step by step create new user, re login to get token, and when mutation create link, it's got The loader.load() function must be called with a value,but got: undefined.
Thanks for any answer.

Most helpful comment

Thanks :)

I'll take a look at the code soon if you haven't figured out the issue, but the dataloader doesn't accept undefined as the value, so the error makes sense. It's ok for no user to be currently logged in in some cases though, so if that's the case for your code you can just make sure to check if user is not undefined before calling the dataloader, only calling it when there is a user to be fetched.

All 8 comments

@viiiprock it seems to be complaining of some call to the user loader that is passing undefined instead of the id. Maybe some place in your different authentication code that is calling the user loader even when there's no user logged? I'd try to debug the code by adding console.logs near all calls to load and see if it catches something.

Another thing that would help investigate this is if you have this code in a public repo somewhere. If so, send us the link through here and I can try to debug it too :)

Your reply is so kind & lovely @mairatma
I'm trying to define some logic but it still a bit struggling in my head.
Here is my code. I'm still new with those stuff, sorry if my code is naive
I used Chrome extension ModHeader to add Bearer token
Thank you so much.
graphql_demo.zip

I'm so sorry, it's weird, the authentication is passed but user now it undefined. My code is wrong somewhere else in auth, not by dataloaders logic

Thanks :)

I'll take a look at the code soon if you haven't figured out the issue, but the dataloader doesn't accept undefined as the value, so the error makes sense. It's ok for no user to be currently logged in in some cases though, so if that's the case for your code you can just make sure to check if user is not undefined before calling the dataloader, only calling it when there is a user to be fetched.

Thanks so much @mairatma
I reverse my commit related to https://www.howtographql.com/graphql-js/6-last-mutation/
The auth work well, I attach form you in case you need to check.
graphql.zip

I'm notice your comment and trying to work on it
So sorry to bother you.

Well, I fixed my issue. It works now.
First I change log singed token resolvers to

// signed token
        const signedToken = jwt.sign({
          id: data.id,
          email: data.email,
        }, JWT_SECRET, {
          expiresIn: '1d'
        })

and in auth service I change this code to

import jwt from 'jsonwebtoken'
import { JWT_SECRET } from '../../config'

export const authenticate = async ({ headers: {authorization} }, Users) => {
  if (authorization && authorization.split(' ')[0] === 'Bearer') {
      const user = jwt.verify(authorization.split(' ')[1], JWT_SECRET)
      return user && await Users.findOne({ user: user._id })
  }
  return null
}

Thanks for you help, I appreciate.

Ah, I'm glad to know you found the issue! Let us know if you have any more problems :)

sure, you are so kind, you could make anybody loves you ;)

Was this page helpful?
0 / 5 - 0 ratings