While each Modus SDK offers similar capabilities, the APIs and usage may vary between languages.

Modus Dgraph APIs documentation is available on the following pages:

The Modus Dgraph APIs allow you to run queries and mutations against a Dgraph database.

Import

To begin, import the dgraph package from the SDK:

import "github.com/hypermodeinc/modus/sdk/go/pkg/dgraph"

Dgraph APIs

The APIs in the dgraph package are below, organized by category.

We’re constantly introducing new APIs through ongoing development with early users. Please open an issue if you have ideas on what would make Modus even more powerful for your next app!

Functions

AlterSchema

Alter the schema of a Dgraph database.

func AlterSchema(connection, schema string) error
connection
string
required

Name of the connection, as defined in the manifest.

schema
string
required

The schema to apply to the Dgraph database.

DropAll

Drop all data from a Dgraph database.

func DropAll(connection string) error
connection
string
required

Name of the connection, as defined in the manifest.

DropAttr

Drop an attribute from a Dgraph schema.

func DropAttr(connection, attr string) error
connection
string
required

Name of the connection, as defined in the manifest.

attr
string
required

The attribute to drop from the Dgraph schema.

EscapeRDF

Ensures proper escaping of RDF string literals.

func EscapeRDF(value string) string
value
string
required

The RDF string literal to escape.

ExecuteMutations

Execute one or more mutations, without a filtering query.

If you need to filter the mutations based on a query, use the ExecuteQuery function instead, and pass the mutations after the query.

func ExecuteMutations(
  connection string,
  mutations ...*Mutation
) (*Response, error)
connection
string
required

Name of the connection, as defined in the manifest.

mutations
...*Mutation
required

One or more pointers to Mutation objects to execute.

ExecuteQuery

Execute a DQL query to retrieve data from a Dgraph database.

Also used to execute a filtering query and apply one or more mutations to the result of the query.

If you need apply mutations without a filtering query, use the ExecuteMutations function instead.

func ExecuteQuery(
  connection string,
  query *Query,
  mutations ...*Mutation
) (*Response, error)
connection
string
required

Name of the connection, as defined in the manifest.

request
*Query*
required

A pointer to a Query object describing the DQL query to execute.

mutations
...*Mutation

Optional parameters specifying pointers to one or more Mutation objects to execute on the nodes matched by the query.

NewMutation

Creates a new Mutation object.

func NewMutation() *Mutation

NewQuery

Creates a new Query object from a DQL query string.

func NewQuery(query string) *Query
query
string
required

The DQL query to execute.

Types

Mutation

A Dgraph mutation object, used to execute mutations.

type Mutation struct {
  SetJson   string
  DelJson   string
  SetNquads string
  DelNquads string
  Condition string
}

// methods
func (*Mutation) WithSetJson(json string) *Mutation
func (*Mutation) WithDelJson(json string) *Mutation
func (*Mutation) WithSetNquads(nquads string) *Mutation
func (*Mutation) WithDelNquads(nquads string) *Mutation
func (*Mutation) WithCondition(cond string) *Mutation
SetJson
string

A JSON string representing the data to set in the mutation.

DelJson
string

A JSON string representing the data to delete in the mutation.

SetNquads
string

A string representing the data to set in the mutation in RDF N-Quads format.

DelNquads
string

A string representing the data to delete in the mutation in RDF N-Quads format.

Condition
string

A string representing the condition query for the mutation, as a DQL @if directive.

condition
string

A string representing the condition query for the mutation, as a DQL @if directive.

WithSetJson(json)

Sets the SetJson field of the mutation to the given json string. Returns the Mutation object to allow for method chaining.

WithDelJson(json)

Sets the DelJson field of the mutation to the given json string. Returns the Mutation object to allow for method chaining.

WithSetNquads(nquads)

Sets the SetNquads field of the mutation to the given nquads string. Returns the Mutation object to allow for method chaining.

WithDelNquads(nquads)

Sets the DelNquads field of the mutation to the given nquads string. Returns the Mutation object to allow for method chaining.

WithCondition(cond)

Sets the condition field of the mutation to the given cond string, which should be a DQL @if directive. Returns the Mutation object to allow for method chaining.

Query

A Dgraph query object, used to execute queries.

type Query struct {
  Query     string
  Variables map[string]string
}

// methods
func (*Query) WithVariable(key string, value any) *Query
Query
string

The DQL query to execute.

Variables
map[string]string

A map of query variables, with values encoded as strings.

WithVariable(name, value)

Sets a query variable with the given name and value. name is of type string, and value can be a string, number, or boolean.

Returns the *Query object to allow for method chaining.

The WithVariable method is the preferred way to set query variables in a Query object, and allows for fluent method chaining to add multiple variables. For example:

query := NewQuery(`
  query all($name: string, $age: int) {
    all(func: eq(name, $name)) {
      name
      age
    }
  }
`).
  WithVariable("name", "Alice").
  WithVariable("age", 30)

response := dgraph.ExecuteQuery("my-dgraph-connection", query)

Request

A Dgraph request object, used to execute queries and mutations.

This object was used by the Execute function, which has been replaced by the ExecuteQuery and ExecuteMutations functions. You should no longer need to create a Request object directly.

type Request struct {
  Query     *Query
  Mutations []*Mutation
}
Query
*Query

A pointer to a Dgraph query object.

Mutations
[]*Mutation

An slice of pointers to Dgraph mutation objects.