Make a graph with many nodes at once

Hello, I’m pretty new to GraphQL/Dgraph.

I’m wondering whether there is an easy way to make a very large graph (1000, 10000, 100000 nodes long) in the query/mutate window in the cloud version. I then want to connect 3 edges randomly per node.

Could anyone help me do this?

1 Like

Or is there any documentation that shows how to create a simple graph with n nodes with k connections?

1 Like

Hello @junyoon and welcome to the community,

I think the only limitation you might face is the maximum mutations at once(I think that’s 2M nquads at the same)

1 Like

DQL doesn’t have iterators or enumeration that would allow you to do that in a query. I recommend you pick one of the clients, whether its the go, python, JS client or whatever then write a script that iterates through a range of numbers firing off a query for each number. You might need to leave it running for a few minutes before you have that number of nodes in the database.

1 Like

Would that be under the “Lambdas” tab in the cloud version?

Also, is there anywhere / a library where I can find an existing script like this that makes a graph?

lambdas are basically serverless functions that you can trigger from your schema.

What I’m talking about is using GitHub - dgraph-io/dgraph-js: Official Dgraph JavaScript client or https://dgraph.io/docs/clients/go/ to interact with the API.

First you gotta add a schema to your instance.

Go to the schema section, and paste in this, and deploy it:

type Thing {
  edge1: Thing!
  edge2: Thing!
  edge3: Thing!
}

Then you just want to create a script, so if you’re using dgraph-js: (this is messy psuedo code)

const dgraph = require("dgraph-js");

const clientStub = dgraph.clientStubFromCloudEndpoint(
  "https://frozen-mango.eu-central-1.aws.cloud.dgraph.io/graphql",
  "<api-key>"
);
const dgraphClient = new dgraph.DgraphClient(clientStub);

// Find 3 random Things

const query = `query things($rand1: int, $rand2: int) {
  things(func: type(Thing)) (first: $rand1, offset: $rand2) {
    id
  }
}`;
const vars = { $rand1: randomNumber(), $rand2: randomNumber() };

const res = await dgraphClient.newTxn().queryWithVars(query, vars);
const randomThings = res.getJson();
let threeRandomThingsIds = [];

threeRandomThings.push(randomThings.things[randomNumber()].id)
threeRandomThings.push(randomThings.things[randomNumber()].id)
threeRandomThings.push(randomThings.things[randomNumber()].id)

// Create data.
const thing = {
    edge1: threeRandomThings[0]
    edge2: threeRandomThings[1]
    edge3: threeRandomThings[2]
};

// Run mutation.
[...Array(10000).keys()].forEach(_ => {
  const mu = new dgraph.Mutation();
  mu.setSetJson(p);
  await txn.mutate(mu);
}
1 Like

I don’t understand how to connect it with the API – is there a place to learn how?

Also, is there a way to do the above in python?

1 Like

he just means to connect to dgraph via REST. you can either use the javascript library or directly the http/REST endpoint

yes you can also do http requests with python

@junyoon that is example JS pseudo code he wrote for you which you could run within a react app ,javascript app, google cloud function, and so on, whatever you want.

BTW there is also a python library https://dgraph.io/docs/clients/python/

BTW2: if you want to use raw HTTP-request check out From Data to API Using Dgraph - Dgraph Blog and https://dgraph.io/docs/clients/raw-http/

1 Like