我是 GraphQL 和 Apollo Client 的新手。
我正在尝试尽可能多地编写 DRY 查询,并发现 Fragment 可以提供很大帮助。
这是我的 GraphQL 架构的样子:
interface header { # Implemented by multiple types
uuid: ID!
created_at: DateTime
created_by: String
updated_at: DateTime
updated_by: String
}
type account implements header @withSubscription {
id: String! @id @search(by:[term])
name: String @search(by:[fulltext])
description: String @search(by:[fulltext])
...
}
在客户端,我正在尝试按如下方式设置我的 operations.graphql:
fragment headerData on header {
uuid
created_at
created_by
updated_at
updated_by
}
fragment accountNode on account {
...headerData
id
name
description
#.. rest of the fields
}
query getPaginatedAccounts(first: Int, offset: Int) {
queryaccount {
...accountNode
# .. rest of the fields
}
}
但是,当我执行查询时,结果集不会从 accountNode
片段中获取任何数据。
这是嵌套片段的正确方法吗?
我意识到,如果我按如下方式更改 accountNode
片段,它会起作用并为我获取数据:
fragment accountNode on account {
... on header {
uuid
created_at
#..rest of header fields
}
id
name
description
#.. rest of the fields
}
但由于 header
接口由多种类型实现,因此在每个类型片段上重复这些字段并不枯燥。
提前致谢。