Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "user" defined in resolvers, but not in schema

I'm trying to set up my schema for Apollo Server, and I'm running into an error.

What I'm trying is actually an attempt to fix a common and even more unhelpful message, using advice found here:

https://github.com/apollographql/apollo-server/issues/1998

I'm not really sure how to reduce the code here, since I haven't been able to isolate the error more than this.

schema.js

import { makeExecutableSchema } from 'apollo-server-express'
import ApplicationSchema from './types'
import Resolvers from './resolvers'

export default makeExecutableSchema({
  typeDefs: ApplicationSchema,
  resolvers: Resolvers
})

resolvers/index.js

import user from './user'

export default {
  user
}

resolvers/user.js

import Joi from 'joi'
import mongoose from 'mongoose'
import { UserInputError } from 'apollo-server-express'
import { signUp, signIn } from '../validators'
import { User } from '../schemata' // mongoose schema
import * as Authentication from '../authentication'

export default {
  Query: {
    me: (root, args, { request }, info) => {
      Authentication.checkSignedIn(request)

      return User.findbyId(request.session.userId)
    },

    users: (root, args, { request }, info) => {
      Authentication.checkSignedIn(request)
      User.find({})
    },

    user: (root, args, { request }, info) => {
      const { id } = args

      Authentication.checkSignedIn(request)

      if (!mongoose.Types.ObjectId.isValid(id)) {
        throw new UserInputError(`${id} is not a valid user ID.`)
      }

      return User.findById(id)
    }
  },

  Mutation: {
    signup: async (root, args, { request }, info) => {
      Authentication.checkSignedOut(request)
      await Joi.validate(args, signUp, { abortEarly: false })
      return User.create(args)
    },

    signIn: async (root, args, { request }, info) => {
      const { userId } = request.session

      if (userId) return User.findById(userId)

      await Joi.validate(args, signIn, { abortEarly: false })

      const user = await Authentication.attemptSignIn(args)

      request.session.userId = user.id

      return user
    },

    signOut: async (root, args, { request, response }, info) => {
      Authentication.checkSignedIn(request)

      return Authentication.signOut(request, response)
    }
  }
}

types/index.js

import root from './root'
import user from './user'

export default [
  root, 
  user
]

types/root,js

import { gql } from 'apollo-server-express'

export default gql`
 type Query {
   _: String
 }

 type Mutation {
   _: String
 }

 type Subscription {
   _: String
 }

types/user.js

import { gql } from 'apollo-server-express'

export default gql`
  type User {
    id: ID!
    email: String!
    username: String!
    name: String!
    password: String!
    createdAt: String!
  }

  extend type Query {
    me: User
    user(id: ID!): User
    users: [User!]!
  }

  extend type Mutation {
    signUp(email: String!, username: String!, name: String!): User
    signIn(email: String!, password: String!): User
    signOut: Boolean
  }

Hopefully, schema.js should run without errors and the resulting executable schema will work well with Apollo Server.

like image 951
Fluffy Ribbit Avatar asked May 08 '19 21:05

Fluffy Ribbit


People also ask

How to fix “user is not in the sudoers file” error?

How to fix “user is not in the sudoers file” error. Adding the impacted user to the Wheel group will help to fix this issue. Any user with superuser (su) access can perform this task. The wheel group is a special user group on Linux systems.

What is the default schema for the local user iuser?

The default schema for the local user "iuser" is "iuser" but also owned another schema and I can't uncheck the schema that it owned. Okay, I think I got it working.

What does the error “not defined” mean?

“ not defined ". A possible reason for the error to occur is that you are utilizing the early binding method to declare and define the object, but the required reference has not been added. Refer to the sample code below to understand the difference between early and late binding.

What does the error in the title mean?

The error in the title is a compile time error that is encountered when you compile the code. Let us split and read the error to understand it better. First, let’s try to understand we have encountered the error because something is “ not defined ”.


1 Answers

Your resolvers object should be structured like what's inside types/user.js, i.e.:

{
  Query: {
    // Query fields
  },
  Mutation: {
    // Mutation fields
  },
  // etc.
}

The resolvers object should be a map of type names. Instead, because of the way your imports are set up, you're ending up with:

{
  user: {
    Query: {
      // Query fields
    },
    Mutation: {
      // Mutation fields
    },
    // etc.
  }
}  

Because this is what you're passing in as your resolvers, Apollo assumes you're trying to map resolvers for a type called user, which doesn't exist in your schema, so it throws the error you're seeing.

If you want to combine multiple resolver objects, you need to recursively merge them using something like lodash's merge:

import user from './user'
import foo from './foo'
import _ from 'lodash'

export default _.merge(
  user,
  foo
)
like image 89
Daniel Rearden Avatar answered Oct 03 '22 22:10

Daniel Rearden