通过DQL mutation添加的Dgraph数据无法通过GraphQL访问问题求助
问题解决方案
核心原因
Dgraph GraphQL 层查询时会自动过滤不符合类型定义规则的节点,你通过 DQL 插入的数据未满足 GraphQL 层的校验条件,因此无法被查询到。
具体修复要求
你在执行 DQL Mutation 时需要满足以下所有规则:
- 必须为节点显式设置
dgraph.type字段,值固定为Product,用于让 GraphQL 层识别节点所属类型 - 必须完整赋值所有 GraphQL Schema 中标记为非空(带
!)的字段:Product.name、Product.slug、Product.created_at、Product.updated_at,缺少任意一个非空字段都会被 GraphQL 查询过滤 - 所有字段的 predicate 名称必须和你定义的保持一致,和 GraphQL Schema 中
@dgraph(pred)的配置匹配
正确DQL Mutation示例
JSON格式(dgo支持直接传入)
{ "set": [ { "dgraph.type": "Product", "Product.name": "product 2", "Product.slug": "prod-2", "Product.image": "https://example.com/prod2.jpg", "Product.created_at": "2021-10-05T06:37:57Z", "Product.updated_at": "2021-10-05T06:37:57Z" } ] }
RDF格式
_:prod2 <dgraph.type> "Product" . _:prod2 <Product.name> "product 2" . _:prod2 <Product.slug> "prod-2" . _:prod2 <Product.created_at> "2021-10-05T06:37:57Z" . _:prod2 <Product.updated_at> "2021-10-05T06:37:57Z" . _:prod2 <Product.image> "https://example.com/prod2.jpg" .
按上述规则插入数据后,再执行你提供的 GraphQL 查询即可返回新增的 Product 数据。
内容的提问来源于stack exchange,提问作者Panagiotis Koursaris
相关产品推荐
相关产品推荐

