0

I am trying to get some data with GraphQL by sending over a json web token but the result I get is an error.

This is the part of my resolvers, that is addressed by the error message.

const resolvers = {
    Query: {
        me: async (token) => {
            try {
                const payload = verify(token, process.env.SECRET);
                if (payload.userId) {
                    const me = await User.findById(payload.userId);
                    return me;
                } else {
                    throw { message: 'Invalid token!' };
                }
            } catch (error) {
                return error.message;
            }
        },

...

The error reads:

"errors": [
{
  "message": "Cannot return null for non-nullable field User.name.",
  "locations": [
    {
      "line": 3,
      "column": 5
    }
  ],
  "path": [
    "me",
    "name"
  ],
  "extensions": {
    "code": "INTERNAL_SERVER_ERROR",

...

The typeDef for user:

type User {
    id: ID!
    name: String!
    email: String!
    password: String!
}

and

extend type Query {
    me(token: String!): User
}

Where is my mistake!?

Thank you!

Timo
  • 21
  • 6

1 Answers1

0
const resolvers = {
    Query: {
        me: async (_, { token }) => {
            try {
                const payload = verify(token, process.env.SECRET);
                if (payload) {
                    const me = await User.findById(payload.userId);
                    return me;
                } else {
                    throw { message: 'Invalid token!' };
                }
            } catch (error) {
                return error.message;
            }
        }

...

this was the solution. I had (token) => { ... } but it should be (_, { token }) => { ... } in the me-function!

Timo
  • 21
  • 6