Expand predicates not returning any values

Hi,

What you wanted to do

I want to write a json object to dgraph that looks like the following. I do this using the python example provided in github.

{
            "model": "334_sfds",
            "deviceType": "accelerometer",
            "deviceDescription": "3 axis accelerometer",
            "properties": [
                {
                    "manufacturer": "siemens",
                    "installDate": "12/02/2021",
                    "wifiPass": "dafdfd"
                }],
            "configuration": [
                {
                    "dataRate0": ["ODR0", 0],
                    "dataRate1": ["ODR1", 0],
                    "dataRate2": ["ODR2", 0],
                    "dataRate3": ["ODR3", 0]
 }]
}

What you actually did

Modifying the create_data function so that it looks like the following successfully writes to dgraph - I can query the model name.

def create_data(client):
    # Create a new transaction.
    txn = client.txn()
    try:
        # Create data.
        p = {
            "model": "334_sfds",
            "deviceType": "accelerometer",
            "deviceDescription": "3 axis accelerometer",
            "properties": [
                {........

The actual json I’m writing has 50 configuration entries so I don’t want to manually specify the predicates I want. This led me to expand(all). I type these queries into Ratel - Query tab.

{
q(func: has(model)){
  deviceType
  expand(_all_)
  }
}

This only returns the deviceType which is not actually what I want. It is just there to make sure the json was written into the db.

Why that wasn’t great, with examples

The few resources I’ve found seem to indicate that it’s an issue with using DQL vs graphql but I have not confirmed this.

Looking at the tutorial the following code can be found:

{
  expand(func: allofterms(name, "Ivy")) {
    expand(_all_) {
      expand(_all_) {
        expand(_all_)
      }
    }
  }
}

Which I modify to allofterms(model, “334_sfds”)… but it still does not return any other predicates.

I have watched the full youtube series from dgraph and I have not been able to find a solution.

Have all of your objects a type? It is necessary to give your nodes a type https://dgraph.io/docs/query-language/type-system/ in order to functions like delete, expand, and others to work.

Cheers.

No I have not. I’ll quickly look into it.

Is it possible to define that json as the type?

Thanks for the fast reply :+1:

Is the only way to create types through this endpoint?

curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphql'

So I see after posting the json I was able to easily make the type in Ratel’s GUI type creator.

Is the proper/intended method the schema.graphql method?

Sorry for the message bombardment. I now have the expanding working.
Regarding the arrays in the configuration element - how does one access them in dgraph?

{
  expand(func: allofterms(model, "334_sfds")) {
    expand(_all_) {
      expand(_all_) {
        expand(_all_){
          expand(_all_)
        }
      }
    }
  }
}

Does not return the contents of properties and configuration - Which I understand are now nodes connected through the edges “properties” and “configuration”. A snippet of properties is given below.

  "properties": [
    {
    "manufacturer": "siemens",
    "installDate": "12/02/2021",
    "wifiPass": "dafdfd"
  }],

If you are using GraphQL, it isn’t necessary to be bothered with it. GraphQL will add those types automatically.

Remember, DQL and GraphQL are two different things. Try not to mix those concepts. If you know GraphQL, good, it is very easy to build something with Dgraph’s GraphQL feature. But if you wanna do something with DQL. You need some time studying. For example, use our Tour. A Tour of Dgraph

You can use any GraphQL client out there to send Admin operations to Dgraph. We tend to use Insomnia and Postman.

No problem at all.

Not sure what you mean.

properties edge needs to be part of the type to be able to expand it. Instead of use expand, try to hard type it. So you have a notion and control of what is happening to your structure. After that, you can use expand as you wish. But in general, I do not recommend using expand in your applications. Cuz it will return inconsistent responses sometimes depending of your data and business logic. And It isn’t friendly for others that might read your code in the future.

Okay thanks for the clarification on DQL and GraphQL.

Not sure what you mean.

I misunderstood the structure. It works now - I had to make a configuration/property type so that the expand expands those values.

But I see why you would rather hardcode the properties. My use case is for nodes with 50+ entries (IC registry entries) where the data is sent to a receiver that only processes what it needs so I would rather not hardcode them.

Appreciate the help - I am redoing our back-end in Dgraph currently.

1 Like