GraphQL queries fail when DQL queries work

We have a DB with data in, which we can query via DQL in Ratel. When we try to run the same query in graphql, via the /graphql endpoint, we get no data. We upserted the data via the /mutate endpoint

Here’s a truncated type we are trying to query:

type ImagingDataset {
    hasIdentifier: String! @id
    hasComment: String
}

The DQL query in Ratel that returns data:

{
  qry(func: eq(ImagingDataset.hasIdentifier, "2a860ed6-f795-298e-6206-9a8b844142de")) {
    ImagingDataset.hasIdentifier
  }
}

The GraphQL query, against the /graphql endpoint, that doesn’t work, is below. I’ve also tried this query with filters and tried the getImagingDataset(hasIdentifier: "2a860ed6-f795-298e-6206-9a8b844142de") query too.

query {
  queryImagingDataset {
    hasIdentifier
  }
}

Which returns:

{
  "data": {
    "queryImagingDataset": []
  },
  "extensions": {
    "tracing": {
      "version": 1,
      "startTime": "2021-02-25T20:17:20.084260535Z",
      "endTime": "2021-02-25T20:17:20.086433454Z",
      "duration": 2172924,
      "execution": {
        "resolvers": [
          {
            "path": [
              "queryImagingDataset"
            ],
            "parentType": "Query",
            "fieldName": "queryImagingDataset",
            "returnType": "[ImagingDataset]",
            "startOffset": 122758,
            "duration": 2032037,
            "dgraph": [
              {
                "label": "query",
                "startOffset": 186831,
                "duration": 1938102
              }
            ]
          }
        ]
      }
    }
  }
}

We have found if we mutate data against /graphql, then the GraphQL query works. We work at a large organization that wants to use standard GraphQL for all enterprise APIs, so we really need the /graphql endpoint to work

If you add data with DQL you will need to make sure to add the <dgraph.type>. Also, be sure to manually handle reverse edges, use type dotted predicates, and any implemented interfaces need to use Interface dotted predicate notation.

1 Like

Also please do read this:

https://dgraph.io/docs/graphql/dgraph

LMK if you have any further questions

2 Likes

Thank you both very much indeed, a combination of your suggestions fixed this:

  1. In my JSON mutation, I added "dgraph.type": "ImagingDataset"
  2. In my schema, I added @dgraph(pred: "ImagingDataset.hasIdentifier") next to the predicate:
type ImagingDataset {
    hasIdentifier: String! @id @dgraph(pred: "ImagingDataset.hasIdentifier")
    hasComment: String
}

I’d suggest a feature request, that this should be implicit if you want to have a native GraphQL experience, i.e. it should happen behind the scenes. It feels very complicated and the documentation needs concrete examples, as this took 3 developers a few hours to figure out. :slight_smile:

This change did nothing. In the GraphQL schema, this field, is already mapped to the predicate you define manually. You only need to define a predicate manually if you want it mapped to something that is outside of normal. For example, if you want to map to a hasIdentifier predicate not using the dotted notation, of if you want several types to use the same predicate such as Question.title and Answer.title should both map to title.

Allowing two types to use the same predicate is sort of what interfaces allow already but that is for a deeper conversation. IMO, if you are using DQL and GraphQL together then you probably want to steer clear away from interfaces and unions in GraphQL unless you first dig down into them and figure out how they are being mapped which can be really strange sometimes. One example is that since I am using interfaces when I use Slash’s Data Explorer my types that have implement interfaces do not list the interface data in with that type. This makes exploring the data difficult for one.