1

我需要帮助找出与 Apollo Client 一起使用的 GraphQL 标签。文档并没有超出最基本的突变用例。

我的目标是让唯一需要的输入是电子邮件。如果存在其他变量,我希望这些变量被接受并创建一个包含所有这些信息的提案。

我有突变(仅在电子邮件和完整变量场景中)在 GraphQl Playground 上成功运行(如果有帮助,您可以在此处找到并对其进行测试,查看架构等):https://prisma2 -graphql-yoga-shield.now.sh/playground

mutation {
  createOneProposal(
    data: {
      email: "fake@gmail.com"
      name: "Sean"
      types: {
        create: {
          model: PURCHASE
          name: "e-commerce"
          cost: 600
          services: {
            create: [
              { service: "Responsive web design" }
              { service: "Another service!" }
              { service: "And yet another service!" }
            ]
          }
        }
      }
    }
  ) {
    created_at
    proposal_id
    types {
      cost
      model
      name
      type_id
      services {
        service
        service_id
      }
    }
  }
}

结果产生:

{
  "data": {
    "createOneProposal": {
      "created_at": "2020-02-27T21:28:53.256Z",
      "proposal_id": 35,
      "types": [
        {
          "cost": 600,
          "model": "PURCHASE",
          "name": "e-commerce",
          "type_id": 6,
          "services": [
            {
              "service": "Responsive web design",
              "service_id": 10
            },
            {
              "service": "Another service!",
              "service_id": 11
            },
            {
              "service": "And yet another service!",
              "service_id": 12
            }
          ]
        }
      ]
    }
  }
}

我对 gql 标签的初始设计:

export const NEW_PROPOSAL = gql`
  mutation createOneProposal(
    $email: String!
    $name: String
    $cost: Int
    $model: Model
    $service: Service
  ) {
    createOneProposal(
      email: $email
      name: $name
      cost: $cost
      model: $model
      service: $service
    ) {
      created_at
      proposal_id
      types {
        services {
          service_id
        }
      }
    }
  }
`;

但是,我对此有很多错误。

{"errors":[
{"Variable "$service" cannot be non-input type `"Service`".","locations":[{"line":1,"column":97}]},
{"Unknown argument "email" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":21}]},
{"Unknown argument "name" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":36}]},
{"Unknown argument"cost" on field "createOneProposal\" of type "Mutation`".","locations":[{"line":2,"column":49}]},
{"Unknown argument "model" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":62}]},
{"Unknown argument "service" on field "createOneProposal`" of type "Mutation`".","locations":[{"line":2,"column":77}]},
{"Field "createOneProposal" argument "data" of type "ProposalCreateInput!`" is required, but it was not provided.","locations":[{"line":2,"column":3}]}]}

那么,我该怎么做...我想出了查询版本(容易得多...),但我就是想不通!

我的架构,如果有帮助的话:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("MYSQL_URL_PRISMA2")
}

model Post {
  content      String    @default("")
  created_at   DateTime  @default(now())
  post_id      Int       @default(autoincrement()) @id
  published    Boolean   @default(false)
  published_at DateTime?
  title        String    @default("")
  author       User
}

model Profile {
  bio        String?
  profile_id Int     @default(autoincrement()) @id
  user_id    User
}

model Proposal {
  email       String   @unique
  name        String?
  proposal_id Int      @default(autoincrement()) @id
  created_at  DateTime @default(now())
  types       Type[]
}

model Type {
  cost        Int?
  name        String?
  model       Model?    @default(SUBSCRIPTION)
  services    Service[]
  type_id     Int       @default(autoincrement()) @id
  proposal_id Proposal
}

model Service {
  service_id Int     @default(autoincrement()) @id
  service    String?
  type_id    Type
}

model User {
  email    String    @default("") @unique
  name     String    @default("")
  password String    @default("")
  role     Role      @default(USER)
  user_id  Int       @default(autoincrement()) @id
  posts    Post[]
  profiles Profile[]
}

enum Role {
  USER ADMIN
}

enum Model {
  SUBSCRIPTION PURCHASE CUSTOM
}
4

2 回答 2

0

感谢@xadm 的一些非常需要的指导,我弄清楚了标签的结构!对于将来遇到此问题的任何人:

mutation createOneProposal($input: ProposalCreateInput!){
    createOneProposal(data:$input){
        created_at
    name
    email
    proposal_id
    type{
      cost
      description
      model
      name
      type_id
      services{
        service
        cost
        service_id
      }
    }
      }
    }

于 2020-02-29T01:51:52.380 回答
0

GraphQL 类型分为输入类型输出类型。输入类型用于变量定义或参数定义等输入。输出类型用于输入字段,这些字段构成实际响应。某些类型,如标量和枚举,可以用作输入或输出。但是,对于对象,有输出对象类型(有时仅称为对象类型或对象)和输入对象类型。

Service是一个输出类型,所以它不能用在需要输入类型的地方(在这种情况下,是一个变量定义)。检查 Prisma 生成的模式以确定要使用的适当类型。

于 2020-02-27T23:42:03.563 回答