【问题标题】:Importing GraphQL resolvers and schema triggers error导入 GraphQL 解析器和模式触发错误
【发布时间】:2020-11-23 01:25:00
【问题描述】:

当我尝试使用以下配置运行服务器时,我收到一个错误:

错误:“createUser”在解析器中定义,但值无效“function (userInput) {... 解析器的值必须是对象类型。

index.ts

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

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

.graphql 架构

# import User, UserInput from "User.graphql"

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

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

schema {
  query: RootQueries
  mutation: RootMutations
}

解析器

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

export { UserResolvers }

【问题讨论】:

  • 你在用什么addResolversToSchema函数?
  • @Bergi 这是一个来自 graphql-tools 的函数

标签: javascript node.js express graphql


【解决方案1】:

你可能正在寻找

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

因为解析器通常按它们出现的类型分组。

【讨论】:

  • 它有所帮助,但现在我在 createUser 解析器中的 userInput 参数未定义
  • @Sheppard25 您的createUser 函数与expected resolver signature 不匹配,但第一个参数绝不应该是undefined
  • 是的,我看到这是 graphql-tools 的变化。第一个参数未定义可能是因为我不使用 rootValues 属性
猜你喜欢
  • 2021-01-25
  • 2019-02-08
  • 2021-09-25
  • 2017-11-03
  • 1970-01-01
  • 2018-04-14
  • 2021-07-20
  • 1970-01-01
  • 2022-06-27
相关资源
最近更新 更多