假设我有以下 json 数据:
"data": {
"continents": [
{
"code": "AF",
"name": "Africa",
},
{
"code": "EU",
"name": "Europe"
},
// ...
]
}
Run Code Online (Sandbox Code Playgroud)
使用以下命令获取列表项的正确 GraphQL 查询是什么: code : "AF"?换句话说,如何产生以下结果:
"data": {
"code": "AF",
"name": "Africa"
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
query {
continents {
code
name
}
}
Run Code Online (Sandbox Code Playgroud)
但这只是返回完整的数组。
我一直在以下位置运行我的示例: https: //lucasconstantino.github.io/graphiql-online/
Jan*_*har 26
filter事实证明,列表/数组上没有定义内置函数!GraphQL(查询语言)基本上是关于选择对象上的字段 [模式和类型| GraphQL]。
只需查看相关的 GraphQL 模式即可:
type Query {
continents(filter: ContinentFilterInput): [Continent!]!
// ...
}
type Continent {
code: ID!
name: String!
countries: [Country!]!
}
input ContinentFilterInput {
code: StringQueryOperatorInput
}
input StringQueryOperatorInput {
eq: String
ne: String
in: [String]
nin: [String]
regex: String
glob: String
}
// ...
Run Code Online (Sandbox Code Playgroud)
我们看到查询有一个输入类型的continents参数。这些信息足以让我们开始构建过滤器查询:filterContinentFilterInput
query {
continents(filter: ...) {
code
name
}
}
Run Code Online (Sandbox Code Playgroud)
经过检查ContinentFilterInput,我们发现它有一个code输入类型字段StringQueryOperatorInput:
query {
continents(filter: { code: ...}) {
code
name
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我们eq在输入类型中找到StringQueryOperatorInput一个标量类型 ( ) 的字段String,我们就完成了:
query {
continents(filter: { code: { eq: "AF" } }) {
code
name
}
}
Run Code Online (Sandbox Code Playgroud)
Noa*_*oam 12
对于当前的示例,您可以这样做
query {
continents(filter: {code: {eq: "AF"}}) {
name
}
}
Run Code Online (Sandbox Code Playgroud)
我建议查看有关参数的文档,因为它们解释得很好。
| 归档时间: |
|
| 查看次数: |
51241 次 |
| 最近记录: |