graphql - 在另一个查询中重用 GraphQL 查询而不重复
问题描述
假设我有两个 GraphQL 查询。
查询一:
{
entry(section: [privacyStatement]) {
... on PrivacyStatement {
title
slug
pageTitle
metaDescription
metaImage {
id
title
url
}
}
}
}
查询 B:
{
entry(section: [contact]) {
... on Contact {
title
slug
pageTitle
metaDescription
metaImage {
id
title
url
}
}
}
}
现在我希望两个查询都包含另一个查询:
查询 C:
{
services: categories(groupId: 1, level: 1) {
id
title
slug
children {
id
title
slug
}
}
}
如何在查询 A 和 B 中不复制查询 C 的情况下做到这一点(这不会很干燥)?如果我理解正确,您只能在一个查询中使用片段。
更新:
所以我的意思是这样的:
Query A {
entry(section: [privacyStatement]) {
... on PrivacyStatement {
title
slug
pageTitle
metaDescription
metaImage {
id
title
url
}
}
}
}
QueryC
和:
Query B {
entry(section: [contact]) {
... on Contact {
title
slug
pageTitle
metaDescription
metaImage {
id
title
url
}
}
}
}
QueryC
解决方案
您可以在 Query 和 Mutations 上定义片段并像这样使用它们:
Query A {
entry(section: [privacyStatement]) {
... on PrivacyStatement {
title
slug
pageTitle
metaDescription
metaImage {
id
title
url
}
}
}
...C
}
fragment C on Query {
services: categories(groupId: 1, level: 1) {
id
title
slug
children {
id
title
slug
}
}
}
你不能定义这样的东西!
query A(...){...}
query B(...){...}
推荐阅读
- group-by - 匹配函数的分组结果
- typescript - 具有多个源映射的项目找不到它的原始 TS-source-map
- kdb - 在选择查询中,根据 q kdb 中的 if -else if 条件设置列值
- python - scipy 最小化 SLSQP - 'LSQ 子问题中的奇异矩阵 C'
- wordpress - WordPress .htaccess:如何从 url 中删除目录?
- azure - 如何通过脚本删除 Azure Databricks 工作区
- css - 如何在 Firefox 中缩放 div 时修复不一致的边框
- algorithm - 有向无环图算法中单源最短路径的运行时间
- wordpress - 重力形式 Razorpay 支付网关电子邮件通知
- java - 如何更改 ExpandableListView 中展开的 GroupView 背景颜色?