【问题标题】:Variable '$_data' cannot be non input type in GraphQL mutation with Prisma使用 Prisma 的 GraphQL 突变中的变量“$_data”不能是非输入类型
【发布时间】:2018-10-10 01:00:20
【问题描述】:

我将 Prisma 与 GraphQL 一起使用,但在运行 mutatioin 时出现错误。 我成功部署了 prisma 并将其与本地 graphQL 绑定。

-- datamodel.graphql - 棱镜设置

type Link {
  id: ID! @unique
  description: String!
  url: String!
  postedBy: User
}

type User {
  id: ID! @unique
  name: String!
  email: String! @unique
  password: String!
  links: [Link!]!
}

-- schema.graphql - 本地设置

# import Link from "./generated/prisma.graphql"
type Query {
  info: String!
  feed: [Link!]!
}

type Mutation {
  post(url: String!, description: String!): Link!
  signup(email: String!, password: String!, name: String!): AuthPayload
  login(email: String!, password: String!): AuthPayload
}

type AuthPayload {
  token: String
  user: User
}

type User {
  id: ID!
  name: String!
  email: String!
  links: [Link!]!
}

注册突变的解析器是

async function signup(parent, args, context, info) {
  // 1
  const password = await bcrypt.hash(args.password, 10)
  // 2
  const user = await context.db.mutation.createUser({
    data: { ...args, password },
  }, `{ id }`)

  // 3
  const token = jwt.sign({ userId: user.id }, APP_SECRET)

  // 4
  return {
    token,
    user,
  }
}

这是 .graphqlconfig.yml 内容

projects:
  app:
    schemaPath: src/schema.graphql
    extensions:
      endpoints:
        default: http://localhost:4000
  database:
    schemaPath: src/generated/prisma.graphql
    extensions:
      prisma: database/prisma.yml

我运行的 GraphQL 查询是。

mutation {
  signup(
    name: "Alice"
    email: "alice@graph.cool"
    password: "graphql"
  ) {
    token
    user {
      id
    }
  }
}

当我运行它时得到的响应是

{
  "data": {
    "signup": null
  },
  "errors": [
    {
      "message": "Variable '$_data' cannot be non input type 'UserCreateInput!'. (line 1, column 19):\nmutation ($_data: UserCreateInput!) {\n                  ^",
      "locations": [],
      "path": [
        "createUser"
      ]
    }
  ]
}

我找不到原因。

谢谢。

【问题讨论】:

  • 你有没有发现是什么原因造成的?我遇到了类似的问题。
  • 我也遇到了同样的问题,但似乎无法弄清楚问题的原因

标签: graphql mutation prisma


【解决方案1】:

尝试使用 prisma deploy --force

这对我有用。

【讨论】:

  • 这解决了我的问题!
  • 如果您只是按照教程进行操作,则在授权章节中运行“prisma deploy”时可能会错过警告。使用 --force 标志重试以确保部署新列。
【解决方案2】:

通过更改为 index.js 中的正确端点来修复它

const server = new GraphQLServer({
   typeDefs: './src/schema.graphql',
   resolvers,
   context: req => ({
       ...req,
       db: new Prisma({
           typeDefs: 'src/generated/prisma.graphql',
           endpoint: 'http://localhost:4466/local/dev',
           secret: 'secret',
           debug: true,
       }),
   }),
})

【讨论】:

    【解决方案3】:

    我认为你的 prisma.yml 是错误的。

    # The endpoint represents the HTTP endpoint for your Prisma API. It encodes
    # several pieces of information:
    # * Prisma server (`localhost:4466` in this example)
    # * Service name (`myservice` in this example)
    # * Stage (`dev` in this example)
    # NOTE: When service name and stage are set to `default`, they can be omitted.
    # Meaning http://myserver.com/default/default can be written as http://myserver.com.
    endpoint: http://localhost:4466/myservice/dev
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 2020-01-31
      • 2019-06-18
      • 2019-06-09
      • 2019-06-11
      • 2019-01-30
      • 2019-03-09
      • 2017-01-30
      相关资源
      最近更新 更多