How to return a GraphQL error in graphql-tools addMockFunctionsToSchema?
Asked Answered
H

1

10

I'd like to mock an error response in graphql-tools/addMockFunctionsToSchema mock resolver following this pattern:

const mocks = {
  ...,
  Mutation: () => ({
    getToken: (_, { password }) => {
      if (password === 'password') {
        return { value: casual.uuid }
      } else {
        throw new Error('Incorrect email or password')
      }
    }
  })
}

const schema = makeExecutableSchema({`
  type Token { value: ID! }
  type Mutation {
     getToken(email: String!, password: String!): Token
  }
`});

addMockFunctionsToSchema({ schema, mocks});

This works OK and does return a GraphQL error but:

  1. It looks like it's only returning an error because it's an internal server error, throwing on this line.
  2. I'd like to mock an actual GraphQL error response indicating that the user input was invalid
Helfand answered 14/4, 2018 at 15:42 Comment(0)
A
1

I was just looking into this issue today. It turns out that you should just return the error instead of throwing one.

  Mutation: () => ({
    getToken: (_, { password }) => {
      if (password === 'password') {
        return { value: casual.uuid }
      } else {
        return new Error('Incorrect email or password')
      }
    }
  })
Acidhead answered 26/2, 2021 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.