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

Cannot read property 'getMutationType' of undefined #25

Closed
nikolasburk opened this issue Dec 28, 2017 · 1 comment
Closed

Cannot read property 'getMutationType' of undefined #25

nikolasburk opened this issue Dec 28, 2017 · 1 comment

Comments

@nikolasburk
Copy link
Contributor

I'm trying to produce a simple auth example, you can find the full repo here.

The project is setup as follows:

index.js

const { GraphQLServer } = require('graphql-yoga')
const { importSchema } = require('graphql-import')
const { Graphcool } = require('graphcool-binding')
const { me, signup, login, AuthPayload } = require('./auth')

const typeDefs = importSchema('./src/schema.graphql')
console.log(typeDefs)

const resolvers = {
  Query: {
    me,
  },
  Mutation: {
    signup,
  //   login,
  },
}

const server = new GraphQLServer({
  typeDefs,
  resolvers,
  context: req => ({
    ...req,
    db: new Graphcool({
      schemaPath: './database/schema.generated.graphql',
      endpoint: process.env.GRAPHCOOL_ENDPOINT,
      secret: process.env.GRAPHCOOL_SECRET,
    }),
  }),
})

server.start(() => console.log('Server is running on http://localhost:4000'))

schema.graphql

type Query {
  me: User
}

type Mutation {
  signup(email: String!, password: String!): AuthPayload!
  # login(email: String!, password: String!): AuthPayload!
}

type AuthPayload {
  token: String!
  user: User!
}

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

auth.js

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const { Context, getUserId } = require('./utils')

const AuthPayload = {
  user: async ({ user: { id } }, args, ctx, info) => {
    return ctx.db.query.user({ where: { id } }, info)
  }
}

// query the currently logged in user
function me(parent, args, ctx, info) {
  const id = getUserId(ctx)
  return ctx.db.query.user({ where: { id } }, info)
}

// register a new user
async function signup(parent, args, ctx, info) {
  const password = await bcrypt.hash(args.password, 10)
  const user = await ctx.db.mutation.createUser({
    data: { ...args, password },
  })

  return {
    token: jwt.sign({ userId: user.id }, process.env.JWT_SECRET),
    user,
  }
}

// log in an existing user
async function login(parent, { email, password }, ctx, info) {
  const user = await ctx.db.query.user({ where: { email } })
  if (!user) {
    throw new Error(`No such user found for email: ${email}`)
  }

  const valid = await bcrypt.compare(password, user.password)
  if (!valid) {
    throw new Error('Invalid password')
  }

  return {
    token: jwt.sign({ userId: user.id }, process.env.JWT_SECRET),
    user,
  }
}

module.exports = { me, signup, login, AuthPayload }

utils.js

const jwt = require('jsonwebtoken')
const { Graphcool } = require('graphcool-binding')

function getUserId(ctx) {
  const Authorization = ctx.request.get('Authorization')
  if (Authorization) {
    const token = Authorization.replace('Bearer ', '')
    const { userId } = jwt.verify(token, process.env.JWT_SECRET)
    return userId
  }

  throw new AuthError()
}

class AuthError extends Error {
  constructor() {
    super('Not authorized')
  }
}

module.exports = {
  getUserId,
  AuthError
}

I am getting an error when sending the signup mutation:

mutation {
  signup(email:"asd@asd.com" password: "asd") {
    token
  }
}

This is the full error message:

TypeError: Cannot read property 'getMutationType' of undefined
    at Object.getTypeForRootFieldName (/Users/nburk/Projects/graphcool/docs/migration-example/1.0/myapp/node_modules/graphql-binding/dist/utils.js:25:45)
    at buildInfoForAllScalars (/Users/nburk/Projects/graphcool/docs/migration-example/1.0/myapp/node_modules/graphql-binding/dist/info.js:17:24)
    at Object.buildInfo (/Users/nburk/Projects/graphcool/docs/migration-example/1.0/myapp/node_modules/graphql-binding/dist/info.js:7:16)
    at Proxy.<anonymous> (/Users/nburk/Projects/graphcool/docs/migration-example/1.0/myapp/node_modules/graphql-binding/dist/handler.js:15:27)
    at signup (/Users/nburk/Projects/graphcool/docs/migration-example/1.0/myapp/src/auth.js:20:38)
    at <anonymous>
@schickling
Copy link
Contributor

This should be fixed in 0.3.5. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants