Sheppard25
Sheppard25

Reputation: 596

Importing GraphQL resolvers and schema triggers error

When I am trying to run server with following configuration I receive an error:

Error: "createUser" defined in resolvers, but has invalid value "function (userInput) {... The resolver's value must be of type object.

index.ts

const schema = loadSchemaSync(join(__dirname, './schema/**.graphql'), {
  loaders: [new GraphQLFileLoader()]
})

const schemaWithResolvers = addResolversToSchema({
  schema,
  resolvers: {
    ...UserResolvers
  }
})

.graphql schema

# import User, UserInput from "User.graphql"

type RootQueries {
  user(id: String!): User
}

type RootMutations {
  createUser(userInput: UserInput): User
}

schema {
  query: RootQueries
  mutation: RootMutations
}

resolvers

const UserResolvers = {
  async createUser(userInput: UserInput) {
    // some code
  }
}

export { UserResolvers }

Upvotes: 0

Views: 1626

Answers (1)

Bergi
Bergi

Reputation: 664207

You're probably looking for

const schemaWithResolvers = addResolversToSchema({
  schema,
  resolvers: {
    RootMutations: UserResolvers
  }
})

as resolvers are usually grouped by the type they appear on.

Upvotes: 2

Related Questions