Skip to content

Instantly share code, notes, and snippets.

@mjpitz
Last active May 29, 2021 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjpitz/0df6fd7982129ed620100423ced921ef to your computer and use it in GitHub Desktop.
Save mjpitz/0df6fd7982129ed620100423ced921ef to your computer and use it in GitHub Desktop.
dgraph troubles

Three files:

  • schema.gql contains the type definitions I inserted into dgraph.
  • seed.rdf contains the set definition used to populate the database with associated information.
  • query.gql contains the dql query I'm trying to execute based on the examples...

When I run the query I get no results back. I've tried this at all sorts of different parts of the simple tree. Can't get any information anywhere. Can see data stored in the system just can't access it. Docs to the system aren't great. They reference a lot of things that seem deprecated / unavailable and the server doesn't return super useful error messages. (e.g. "undefined directive index" and yet, they use it everywhere.)

Version information

docker - dgraph/dgraph:latest

Dgraph version   : v21.03.0
Dgraph codename  : rocket
Dgraph SHA-256   : b4e4c77011e2938e9da197395dbce91d0c6ebb83d383b190f5b70201836a773f
Commit SHA-1     : a77bbe8ae
Commit timestamp : 2021-04-07 21:36:38 +0530
Branch           : HEAD
Go version       : go1.16.2
jemalloc enabled : true

For Dgraph official documentation, visit https://dgraph.io/docs.
For discussions about Dgraph     , visit https://discuss.dgraph.io.
For fully-managed Dgraph Cloud   , visit https://dgraph.io/cloud.

Licensed variously under the Apache Public License 2.0 and Dgraph Community License.
Copyright 2015-2021 Dgraph Labs, Inc.

docker - dgraph/ratel:latest

Ratel Version: 20.7.0
Commit ID: 
Commit Info: 
query {
cloud(func: eq(cloud.name, "aws")) {
name
regions {
name
zones {
name
}
}
}
}
type Cloud @dgraph(type: "cloud") {
id: ID!
name: String! @id
regions: [Region!]! @hasInverse(field: cloud)
}
type Region @dgraph(type: "region") {
id: ID!
name: String! @id
cloud: Cloud!
zones: [Zone!]! @hasInverse(field: region)
}
type Zone @dgraph(type: "zone") {
id: ID!
name: String! @id
cloud: Cloud!
region: Region!
}
{
set {
_:aws <dgraph.type> "cloud" .
_:aws <cloud.name> "aws" .
_:us-west-1 <dgraph.type> "region" .
_:us-west-1 <region.name> "us-west-1" .
_:us-west-1 <region.cloud> _:aws .
_:us-west-1a <dgraph.type> "zone" .
_:us-west-1a <zone.name> "us-west-1a" .
_:us-west-1a <zone.region> _:us-west-1 .
_:us-west-1b <dgraph.type> "zone" .
_:us-west-1b <zone.name> "us-west-1b" .
_:us-west-1b <zone.region> _:us-west-1 .
_:us-west-1c <dgraph.type> "zone" .
_:us-west-1c <zone.name> "us-west-1c" .
_:us-west-1c <zone.region> _:us-west-1 .
}
}
@danielmai
Copy link

danielmai commented May 28, 2021

I see you're using both GraphQL and DQL here (DQL for both the mutation and the query).

There's some syntax things for the DQL examples you shared. To see this working, let's start with getting this working using GraphQL.

First, for this example I've updated your schema to simplify the data model here:

type Cloud @dgraph(type: "cloud") {
    id: ID!
    name: String! @id
    regions: [Region!]! @hasInverse(field: cloud)
}

type Region @dgraph(type: "region") {
    id: ID!
    name: String! @id
    cloud: Cloud!
    zones: [Zone!]! @hasInverse(field: region)
}

type Zone @dgraph(type: "zone") {
    id: ID!
    name: String! @id
    region: Region!
}

The one thing I've changed is I've removed the cloud field from your Zone type (there's no @hasInverse for it so the mutation would look more redundant otherwise). To write your mutation in GraphQL, it'd look like this:

mutation {
  addCloud(input: [
    {
      name: "aws",
      regions: [
        {
          name: "us-west-1",
          zones: [
            {name: "us-west-1a"},
            {name: "us-west-1b"},
            {name: "us-west-1c"}
          ]
        }
      ]
    }
  ]) {
    cloud {
      name
      regions {
        name
        zones {
          name
        }
      }
    }
  }
}

And you'll get a response like this:

{
  "data": {
    "addCloud": {
      "cloud": [
        {
          "name": "aws",
          "regions": [
            {
              "name": "us-west-1",
              "zones": [
                {
                  "name": "us-west-1b"
                },
                {
                  "name": "us-west-1c"
                },
                {
                  "name": "us-west-1a"
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

And, you can run this GraphQL query to get this same data:

{
  getCloud(name: "aws") {
    name
    regions {
      name
      zones {
        name
      }
    }
  }
}

Response:

{
  "data": {
    "getCloud": {
      "name": "aws",
      "regions": [
        {
          "name": "us-west-1",
          "zones": [
            {
              "name": "us-west-1b"
            },
            {
              "name": "us-west-1c"
            },
            {
              "name": "us-west-1a"
            }
          ]
        }
      ]
    }
  }
}

@mjpitz
Copy link
Author

mjpitz commented May 28, 2021

So am I not able to do partial updates to nested components and have the inverse relationship properly formed? (part of the reason I was using mixed forms was because the documentation wasn't really straightforward for this stuff)

The reason I ask is because there are parts of my graph that I need to update independently from one another.

@mjpitz
Copy link
Author

mjpitz commented May 28, 2021

also... this is the error I get when I execute your mutation in ratel:

{
  "name": "t",
  "url": "http://localhost:8080/mutate?commitNow=true",
  "errors": [
    {
      "message": "while lexing mutation {\n    addCloud(input: [\n        {\n            name: \"aws\",\n            regions: [\n                {\n                    name: \"us-west-1\",\n                    zones: [\n                        {name: \"us-west-1a\"},\n                        {name: \"us-west-1b\"},\n                        {name: \"us-west-1c\"}\n                    ]\n                }\n            ]\n        }\n    ]) {\n        cloud {\n            name\n            regions {\n                name\n                zones {\n                    name\n                }\n            }\n        }\n    }\n}\n at line 1 column 0: Invalid block: [mutation]",
      "extensions": {
        "code": "ErrorInvalidRequest"
      }
    }
  ]
}

@danielmai
Copy link

@mjpitz For these GraphQL mutations, you'd run them via GraphQL in a tool like GraphiQL, GraphQL Playground, or directly from the Dgraph Cloud dashboard for your cloud backend. Ratel doesn't accept these GraphQL queries today.

You can update nested components. In your original schema you wanted to establish a direct connection from the Zone to Cloud nodes. You can always traverse the relationships to get the data you need (from Zone to Region to Cloud, in this case).

@danielmai
Copy link

If you want to run your seed.rdf DQL mutation in Ratel, then you'd need to tweak your mutation a bit. Here's what you'd need to change in your mutation:

  1. The cloud.regions field needs to be set
  2. The region.zones needs to be set
diff --git a/seed.rdf b/seed.rdf
index 5e2f851..255021a 100644
--- a/seed.rdf
+++ b/seed.rdf
@@ -2,10 +2,14 @@
     set {
         _:aws <dgraph.type> "cloud" .
         _:aws <cloud.name> "aws" .
+        _:aws <cloud.regions> _:us-west-1 .
 
         _:us-west-1 <dgraph.type> "region" .
         _:us-west-1 <region.name> "us-west-1" .
         _:us-west-1 <region.cloud> _:aws .
+        _:us-west-1 <region.zones> _:us-west-1a .
+        _:us-west-1 <region.zones> _:us-west-1b .
+        _:us-west-1 <region.zones> _:us-west-1c .
 
         _:us-west-1a <dgraph.type> "zone" .
         _:us-west-1a <zone.name> "us-west-1a" .

And here's the full copy for convenience:

{
    set {
        _:aws <dgraph.type> "cloud" .
        _:aws <cloud.name> "aws" .
        _:aws <cloud.regions> _:us-west-1 .

        _:us-west-1 <dgraph.type> "region" .
        _:us-west-1 <region.name> "us-west-1" .
        _:us-west-1 <region.cloud> _:aws .
        _:us-west-1 <region.zones> _:us-west-1a .
        _:us-west-1 <region.zones> _:us-west-1b .
        _:us-west-1 <region.zones> _:us-west-1c .

        _:us-west-1a <dgraph.type> "zone" .
        _:us-west-1a <zone.name> "us-west-1a" .
        _:us-west-1a <zone.region> _:us-west-1 .

        _:us-west-1b <dgraph.type> "zone" .
        _:us-west-1b <zone.name> "us-west-1b" .
        _:us-west-1b <zone.region> _:us-west-1 .

        _:us-west-1c <dgraph.type> "zone" .
        _:us-west-1c <zone.name> "us-west-1c" .
        _:us-west-1c <zone.region> _:us-west-1 .
    }
}

Similarly, for your DQL query, need to use the correct names for your query to return the data. The naming convention here stems from your GraphQL schema and follows the rules documented in the GraphQL to DQL schema mapping: https://dgraph.io/docs/graphql/dgraph/

diff --git a/query.gql b/query.gql
index d715c48..bd258cb 100644
--- a/query.gql
+++ b/query.gql
@@ -1,12 +1,12 @@
 query {
     cloud(func: eq(cloud.name, "aws")) {
-        name
+        cloud.name
     
-        regions {
-            name
+        cloud.regions {
+            region.name
     
-            zones {
-                name
+            region.zones {
+                zone.name
             }
         }
     }

And the full query:

query {
    cloud(func: eq(cloud.name, "aws")) {
        cloud.name
    
        cloud.regions {
            region.name
    
            region.zones {
                zone.name
            }
        }
    }
}

Query response:

{
  "data": {
    "cloud": [
      {
        "cloud.name": "aws",
        "cloud.regions": [
          {
            "region.name": "us-west-1",
            "region.zones": [
              {
                "zone.name": "us-west-1b"
              },
              {
                "zone.name": "us-west-1c"
              },
              {
                "zone.name": "us-west-1a"
              }
            ]
          }
        ]
      }
    ]
  }
}

@danielmai
Copy link

I forgot that GitHub gists are forkable, so, instead of pasting diffs, I've forked this gist with the changes discussed above: https://gist.github.com/danielmai/7db7b0c9e971eb748ce3d483278e820c. I hope this helps.

@mjpitz
Copy link
Author

mjpitz commented May 29, 2021

OK. I think I understand the layers a lot better and I have this running. Thanks a lot for your help!

@danielmai
Copy link

@mjpitz Awesome! Glad to hear that you have this running now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment