如果通过 DQL 突变添加,则无法通过 graphql 访问 Dgraph 数据

Dgraph data is not accessible via graphql if added via DQL mutation

我想做什么

使用 DQL 突变添加数据 https://github.com/dgraph-io/dgo#running-a-mutation 并且这些数据也可以通过 graphql 显示。

Dgraph 版本: v21.03.2

DQL 模式:

<Product.created_at>: datetime .
<Product.image>: string .
<Product.name>: string @index(hash) @upsert .
<Product.slug>: string @index(hash) @upsert .
<Product.updated_at>: datetime .
<dgraph.drop.op>: string .
<dgraph.graphql.p_query>: string @index(sha256) .
<dgraph.graphql.schema>: string .
<dgraph.graphql.xid>: string @index(exact) @upsert .
type <Product> {
    Product.name
    Product.slug
    Product.image
    Product.created_at
    Product.updated_at
}
type <dgraph.graphql> {
    dgraph.graphql.schema
    dgraph.graphql.xid
}
type <dgraph.graphql.persisted_query> {
    dgraph.graphql.p_query
}

GraphQL 架构:

type Product {
    id: ID!
    name: String! @id @dgraph(pred: "Product.name")
    slug: String! @id @dgraph(pred: "Product.slug")
    image: String @dgraph(pred: "Product.image")
    created_at: DateTime! @dgraph(pred: "Product.created_at")
    updated_at: DateTime! @dgraph(pred: "Product.updated_at")
}

使用上述模式,graphql 查询工作正常。

Graphql 突变

mutation MyMutation {
  addProduct(input: {name: "product 1", slug: "prod-1", created_at: "2021-10-04T06:37:57.707227339Z", updated_at: "2021-10-04T06:37:57.707227339Z"}) {
    numUids
  }
}

Graphql 查询

query MyQuery {
  queryProduct {
    name
  }
}

graphql 查询的响应:

{
  "data": {
    "queryProduct": [
      {
        "name": "product 1"
      }
    ]
  },
  "extensions": {
    "touched_uids": 2,
    "tracing": {
      "version": 1,
      "startTime": "2021-10-04T06:42:01.064395081Z",
      "endTime": "2021-10-04T06:42:01.065675778Z",
      "duration": 1280687,
      "execution": {
        "resolvers": [
          {
            "path": [
              "queryProduct"
            ],
            "parentType": "Query",
            "fieldName": "queryProduct",
            "returnType": "[Product]",
            "startOffset": 110469,
            "duration": 1164739,
            "dgraph": [
              {
                "label": "query",
                "startOffset": 149859,
                "duration": 1123999
              }
            ]
          }
        ]
      }
    }
  }
}

然后我使用 dgo 做了一个突变:https://github.com/dgraph-io/dgo#running-a-mutation 并且使用 ratel 工具可以很好地显示数据。

当我再次尝试 Graphql 查询时:

query MyQuery {
  queryProduct {
    name
  }
}

none 个在 graphql 响应中返回。

问题是因为 dgraph 还希望通过 dql 突变传递 DType,如下所示:

models.Product{
                Name:      "product name",
                Slug:      "product-slug",
                Image:     "test.jpg",
                CreatedAt: time.Now(),
                UpdatedAt: time.Now(),
                DType:     []string{"Product"}, // here is the trick
            }