graphql,当存在“添加”和“更新”突变时,如何设计输入类型?

sli*_*wp2 6 graphql graphql-js apollo-server

这是我的要求:

  1. “添加”突变,BookInput输入类型的每个字段(或称为标量)都应具有附加的类型修饰符“!” 验证非空值。这意味着当我添加一本书时,参数必须具有titleauthor字段,例如{title: "angular", author: "novaline"}

  2. “更新”突变,我想更新本书的一部分字段,不想更新整本书(MongoDB文档,而且,我不想前端将graphql服务器传递给一个完整的大本书突变参数节省带宽)。这意味着book参数可以是{title: "angular"}{title: "angular", author: "novaline"}

这是我的类型定义:

const typeDefs = `
  input BookInput {
    title: String!
    author: String!
  }

  type Book {
    id: ID!
    title: String!
    author: String!
  }

  type Query {
    books: [Book!]!
  }

  type Mutation{
    add(book: BookInput!): Book
    update(id: String!, book: BookInput!): Book
  }
`;
Run Code Online (Sandbox Code Playgroud)

目前,“添加”突变可以正常工作。但是,如果我通过{title: "angular"}参数,“更新”突变将无法通过非空检查

这是一个无法通过非空检查的突变,BookInput输入类型缺少“作者”字段。

mutation {
  update(id: "1", book: {title: "angular"}) {
    id
    title
    author
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,graphql将给我一个错误:

{
  "errors": [
    {
      "message": "Field BookInput.author of required type String! was not provided.",
      "locations": [
        {
          "line": 2,
          "column": 24
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

如何设计BookInput输入类型?不想定义addBookInputupdateBookInput。它是重复的。

Mik*_*ank 9

一个非常常见的模式是每个突变都有单独的输入类型。您可能还想为每个操作创建一个变异查询。也许是这样的:

const typeDefs = `
  input AddBookInput {
    title: String!
    author: String!
  }

  input UpdateBookInput {
    # NOTE: all fields are optional for the update input 
    title: String
    author: String
  }

  type Book {
    id: ID!
    title: String!
    author: String!
  }

  type Query {
    books: [Book!]!
  }

  type Mutation{
    addBook(input: AddBookInput!): Book
    updateBook(id: String!, input: UpdateBookInput!): Book
  }
`;
Run Code Online (Sandbox Code Playgroud)

有些人还喜欢将更新ID包含在更新输入中:

const typeDefs = `
  input AddBookInput {
    title: String!
    author: String!
  }

  input UpdateBookInput {
    # NOTE: all fields, except the 'id' (the selector), are optional for the update input 
    id: String!
    title: String
    author: String
  }

  type Book {
    id: ID!
    title: String!
    author: String!
  }

  type Query {
    books: [Book!]!
  }

  type Mutation{
    addBook(input: AddBookInput!): Book
    updateBook(input: UpdateBookInput!): Book
  }
`;
Run Code Online (Sandbox Code Playgroud)

最后,您可能需要对返回类型使用“有效载荷”类型-以获得更大的灵活性(为您提供更多的灵活性,以便稍后在不破坏API的情况下更改返回类型):

const typeDefs = `
  input AddBookInput {
    title: String!
    author: String!
  }

  input UpdateBookInput {
    # NOTE: all fields, except the 'id' (the selector), are optional for the update input 
    id: String!
    title: String
    author: String
  }

  type Book {
    id: ID!
    title: String!
    author: String!
  }

  type AddBookPayload {
    book: Book!
  }

  type UpdateBookPayload {
    book: Book!
  }

  type Query {
    books: [Book!]!
  }

  type Mutation{
    addBook(input: AddBookInput!): AddBookPayload!
    updateBook(input: UpdateBookInput!): UpdateBookPayload!
  }
`;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!