Apollo-server: Error: "user" defined in resolvers, but not in schema

Created on 8 May 2019  路  7Comments  路  Source: apollographql/apollo-server

Sorry to just dump my code on you guys, but I don't really know what to do here. Following advice found here:

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

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
  }

All 7 comments

I also had this error, except it was for a propery of one of my types:

Error: Workspace.link defined in resolvers, but not in schema

I made a different, way dumber mistake- a good ol' fashion copy-paste error:

const Link = require('./resolvers/Link')
const Workspace = require('./resolvers/Link')

So, Workspace was being treated like it was a Link object. Drove me very close to insanity!

This was a bit confusing for me to follow as an apollo/graphql newbie. Here's my fix for anybody who's in a similar situation.

Old resolver.js
module.exports = { createUser: async args => { console.log('create user logic here'); } login: async args => { console.log('login logic here'); } }

New resolver.js
`
module.exports = {
Mutation: {
createUser: async args => {
console.log("create user logic here");
}
},

Query: {
login: async ({ email, password }) => {
console.log("login logic here");
}
}
}
`

Hope this helps!

The reason for this error is because user was defined in server.js, but not in schema.graphql. It needs to be defined in both.

This was a bit confusing for me to follow as an apollo/graphql newbie. Here's my fix for anybody who's in a similar situation.

Old resolver.js
module.exports = { createUser: async args => { console.log('create user logic here'); } login: async args => { console.log('login logic here'); } }

New resolver.js
`
module.exports = {
Mutation: {
createUser: async args => {
console.log("create user logic here");
}
},

Query: {
login: async ({ email, password }) => {
console.log("login logic here");
}
}
}
`

Hope this helps!

Hey could you please, properly format the code? I'll appreciate, thanx!

In my case, it was an even sillier mistake. I had accidentally put my mutations inside my query :laughing:

module.exports = {
   Query: {
      async getPosts () {
         try {
            const posts = await Post.find()
            return posts;
         } catch (err) {
            throw new Error(err)
         }
      },
      async getPost(_, { postId }) {
         try {
            const post = await Post.findById(postId)

            if (post) {
               return post
            } else {
               throw new Error('Post not found')
            }
         } catch (err) {
            throw new Error(err)
         }
      }
   },
   Mutation: {
      async createPost(_, { body }, context) {
         const user = checkAuth(context)
         console.log(user)

         const newPost = newPost({
            body,
            user: user.id,
            username: user.username,
            createdAt: new Date().toISOString
         })

         const post = await newPost.save 
         return post 
      }
   }
}

I did a very silly mistake which almost drove me crazy.
was using
const Query=require('./graphql/resolvers/Query') const Mutation=require('./graphql/resolvers/mutations')
instead of
const {Query}=require('./graphql/resolvers/Query') const {Mutation}=require('./graphql/resolvers/mutations')

Was this page helpful?
0 / 5 - 0 ratings