hel*_*joe 7 javascript node.js graphql
我想知道如何在同一个 GraphQL 查询中使用输入类型及其字段之一作为参数。我认为有一些有效的解决方案,但我想知道哪些(如果有)是最佳实践。
考虑以下假设查询。我们通过位置和状态获取玩家,以及处于同一位置的团队成员,但该member字段只有一个location参数:
input PlayerInput {
location: String!
status: Int!
}
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(location: ???) { // <-- How to access playerInput.location?
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以想出几种方法来解决这个问题:
1. 更改查询以采用单独的参数
input PlayerInput {
location: String!
status: Int!
}
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(location: ???) { // <-- How to access playerInput.location?
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
2. 更新架构以members采用正确的输入类型
query getPlayers($location: String!, $status: Int!) {
players(playerInput: { location: $location, status: $status }) {
name
team {
name
members(location: $location) {
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于某些原因,这看起来不太好,并且只有当您有能力更新架构时才有效。
3.location作为冗余的单独参数传入
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(playerInput: $playerInput) { // <-- Requires changing schema
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来不错,只是创建查询时存在一些重复:
query getPlayers($playerInput: PlayerInput, $location: String!) {
players(playerInput: $playerInput) {
name
team {
name
members(location: $location) {
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这些是做类似事情的首选方式吗?还有其他我没有考虑过的方法吗?
在采用输入类型的单个参数(选项#1)上使用多个参数有时在概念上是有意义的,但它缺乏任何其他真正的优点,并且具有使变量定义不必要地冗长的缺点。在我看来,使用输入类型通常也可以更好地强制输入。仅仅为了避免重复而更改架构是不值得的。
仅当两个字段实际上都需要所有这些输入字段时,才应在两个字段上使用相同的输入对象类型(选项 #2)。仅仅为了避免重复而这样做是一个坏主意——它实际上引入了未使用的输入,这对于使用该 API 的任何其他开发人员来说都不是一个好的体验。
选项#3 很好。它还将球交给了前端开发人员——如果他们想避免重复,他们也可以轻松地做到这一点,而无需引入额外的参数:
query getPlayers($status: Int!, $location: String!) {
players(playerInput: { status: $status, location: $location }) {
name
team {
name
members(location: $location) {
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然而,重复很可能实际上是一件好事。您在野外不会经常遇到这些场景,因为我们正在处理数据图。、players和字段都是层次结构的一部分,其中父字段限制子字段返回的数据team-members例如,members不返回所有成员,仅返回该特定团队的成员。这意味着,在大多数情况下,如果您限制players只包含来自特定位置的数据,那么您也会限制子字段(例如members. 如果您觉得members需要自己的location参数,那么这意味着两个位置输入可能不同。如果它们可能不同,则应将它们表示为两个单独的变量,以最大限度地提高灵活性和查询重用。
| 归档时间: |
|
| 查看次数: |
2017 次 |
| 最近记录: |