相关疑难解决方法(0)

在 GraphQL 中重用输入类型作为片段

GraphQL 中一个非常常见的用例是创建一个带有突变的对象,并接收完全相同的字段、数据库返回的加号和 ID。这是一个相关的问题,询问这个。

我的问题是,如何简化这种模式以避免重复字段?我试过将输入类型重用为片段,

input ClientInput {
  short_name: String
  full_name: String
  address: String
  email: String
  location: String  
}

type Client {
  id: String
  ...ClientInput
}
Run Code Online (Sandbox Code Playgroud)

...但失败了

语法错误:预期名称,找到...

我在 Fragments 上看到的所有文档和博客文章总是将它们创建on为现有类型。这意味着仍然重复除 ID 字段之外的所有内容:

type Client {
  _id: String
  short_name: String
  full_name: String
  address: String
  email: String
  location: String
}

fragment ClientFields on Client {
  short_name: String
  full_name: String
  address: String
  email: String
  location: String
}

input ClientInput {
  ...ClientFields
}
Run Code Online (Sandbox Code Playgroud)

怎么样更好?

types graphql

4
推荐指数
1
解决办法
5045
查看次数

标签 统计

graphql ×1

types ×1