graphqlgraph-databasesdgraph

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


What I want to do

Add data using DQL mutation https://github.com/dgraph-io/dgo#running-a-mutation and these data to also be visible via graphql.

Dgraph version: v21.03.2

DQL schema:

<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 schema:

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")
}

Using the above schema, graphql queries are working fine.

Graphql mutation

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

query MyQuery {
  queryProduct {
    name
  }
}

response of the graphql query:

{
  "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
              }
            ]
          }
        ]
      }
    }
  }
}

Then I did a mutation using dgo: https://github.com/dgraph-io/dgo#running-a-mutation and the data are shown fine using the ratel tool.

When I try again the Graphql Query:

query MyQuery {
  queryProduct {
    name
  }
}

none of these are returned in the graphql response.


Solution

  • The issue is because the dgraph also wants a DType to be passed via dql mutation like so:

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