【问题标题】:GraphQL Input type deduplicationGraphQL 输入类型重复数据删除
【发布时间】:2018-09-19 23:26:48
【问题描述】:

说我有这个:

type Page {
  id: ID!
  title: String!
  body: String!
  comments: [Comment]
}

type Comment {
   id: ID!
   text: String!   
   someOtherField: String!
   optional: String
}

mutation AddPage (input: AddPageInput): Page!

input AddPageInput {
  title: String!
  body: String
  comment: AddPageCommentInput
}

input AddPageCommentInput {
   text: String!   
   someOtherField: String!
   optional: String
}

mutation AddComment (input: AddCommentInput): Comment!

input AddCommentInput {
   forPageId: ID!
   text: String!   
   someOtherField: String!
   optional: String
}

我想删除 AddPageCommentInput 的重复数据,因为它与 AddCommentInput 具有相同的字段,但 forPageId 除外。

我要做的是让AddPage 解析器将AddPageCommentInput 委托给AddComment 解析器(在保存Page 并使用forPageId 扩展输入之后)

继承(手动输入定义)是确保事情一致的唯一方法吗?


编辑 我没有这些类型,只是尝试为问题提供上下文。

一个措辞更好的问题是:“我有哪些选项可以模拟输入的继承?”

【问题讨论】:

标签: graphql


【解决方案1】:

您可以通过稍微重塑突变(并在过程中使其更清晰)来颠覆问题:

input CommentInput {
   text: String!   
   someOtherField: String!
   optional: String
}

input AddPageInput {
  title: String!
  body: String
  comment: CommentInput
}

input AddCommentToPageInput {
   pageId: ID!
   comment: CommentInput!
}

type AddCommentToPagePayload {
    page: Page!
    comment: Comment!
}

mutation AddPage(input: AddPageInput!): Page!
mutation AddCommentToPage(input: AddCommentToPageInput!): AddCommentToPagePayload!

【讨论】:

    【解决方案2】:

    这里有一个建议:您可以从AddCommentInput 中取出forPageId 字段并将其用作第二个参数。这将让您丢弃 AddPageCommentInput 并在这两个地方使用 AddCommentInput

    代码将如下所示:

    mutation AddPage (input: AddPageInput): Page!
    
    input AddPageInput {
      title: String!
      body: String
      comment: AddCommentInput
    }
    
    mutation AddComment (forPageId: ID!, input: AddCommentInput): Comment!
    
    input AddCommentInput {
       text: String!   
       someOtherField: String!
       optional: String
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 2013-05-11
      • 2019-02-26
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2023-03-05
      • 2019-03-15
      相关资源
      最近更新 更多