Skip to main content

Selection

In this section we'll learn how to select nodes and edges for our queries.

Select Attributes#

We use the Select function to select the attributes for our nodes:

db.QueryType("Animal").Select(`
uid
name
animal
`)

produces:

{
rootQuery(func: type(Animal)) {
uid
name
animal
}
}

Query edges#

If we want to query fields that are nested we use the Edge function

db.QueryType("Animal").
Select(`
uid
name
animal
`).
Edge("favorite_food", dqlx.Select(`
uid
brand
type
`)).
Edge("favorite_food->ingredients", dqlx.Select(`
uid
name
family
`)).
Edge("favorite_food->ingredients->nutrition", dqlx.Select(`
kcal
`))

produces

{
rootQuery(func: type(Animal)) {
uid
name
animal
favorite_food {
uid
brand
type
ingredients {
uid
name
family
nutrition {
kcal
}
}
}
}
}

The first parameter of the Edge function must be the full path starting from its upmost ancestor.

Using a slice syntax

If you prefer using a slice syntax over the conventional string (using the symbol ->) you can instead use EdgePath([]string{"favorite_food", ...}) or Edge(dqlx.EdgePath([]string{"favorite_food", ...}))

Aliases#

In Order to alias a field you can simply use the expression alias:field

db.QueryType("Animal").Select(`
uid
alias:name
`)

or use the function Alias

db.QueryType("Animal").Select(
"uid",
dqlx.Alias("alias", "name")
)

Select API#

You have few ways to select fields

Template literal#

Most of the examples we've seen so far uses the template literal for defining fields.

The newline is the delimiter for the next field

db.QueryType("Animal").Select(`
uid
name
`)

Variadic Strings#

db.QueryType("Animal").Select(
"uid",
"name",
)

Slices#

fields := []string{"uid", "name"}
db.QueryType("Animal").Select(fields...)