【问题标题】:Why does GraphQL choose to use an 'input' type? [duplicate]为什么 GraphQL 选择使用“输入”类型? [复制]
【发布时间】:2018-02-05 06:43:27
【问题描述】:

这是我对 GraphQL 的 Client 和 ClientInput 的定义

type Client {
  _id: String
  short_name: String
  full_name: String
  address: String
  contact_name: String
  contact_email: String
  contract_currency: String
  location: String
}

input ClientInput {
  short_name: String
  full_name: String
  address: String
  contact_name: String
  contact_email: String
  contract_currency: String
  location: String  
}

它们或多或少是相同的。他们为什么选择发明一种输入类型?

这是我从他们的官方文档中找到的:

input 是 graphql 中的另一种特殊类型,因为在 graphql 中你不能 在您的架构中混合输入和输出类型。

我仍然不完全清楚为什么。为什么在graphql中,我不能混合输入和输出类型?

【问题讨论】:

    标签: graphql


    【解决方案1】:

    因为当您将数据插入数据库时​​,您查询数据时不一定有相同的数据。例如,您想强制人们使用 id 查询“用户”,但在创建用户时没有 id,因此您的输入将是

    input UserInput {
       name: String
    }
    

    你的类型将是

    type User {
       id: ID!
       name: String
    }
    

    【讨论】:

    • 如果字段相同,例如查询类型可以是“type User {name: String}”,同UserInput。为什么 type 和 input type 不能互换?
    • 有没有办法reuse input types as fragments
    猜你喜欢
    • 2021-07-28
    • 2020-03-23
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2016-01-07
    • 2016-10-11
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多