Lambda Fields
Start with lambda resolvers by defining it in your GraphQL schema. Then define your JavaScript mutation function and add it as a resolver in your JS source code.
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Schema
To set up a lambda function, first you need to define it on your GraphQL schema
by using the @lambda
directive.
For example, to define a lambda function for the rank
and bio
fields in
Author
:
You can also define @lambda
fields on interfaces, as follows:
Resolvers
After the schema is ready, you can define your JavaScript mutation function and
add it as a resolver in your JS source code. To add the resolver you can use
either the addGraphQLResolvers
or addMultiParentGraphQLResolvers
methods.
A Lambda Field resolver can use a combination of parents
, parent
, dql
,
or graphql
inside the function.
This example uses parent
for the resolver function. You can find additional
resolver examples using dql
in the Lambda queries article, and
using graphql
in the Lambda mutations article.
For example, to define JavaScript lambda functions for
Author
Character
Human
Droid
and add them as resolvers, do the following
For example, you can add a resolver for rank
using a graphql
call, as
follows:
The following example demonstrates using the client-provided JWT to return
true
if the custom claim for USER
from the JWT matches the id
of the
Author
.
Example
For example, if you execute the following GraphQL query:
You should see a response such as the following:
In the same way, if you execute the following GraphQL query on the Character
interface:
You should see a response such as the following:
The Human
and Droid
types inherit the bio
lambda field from the
Character
interface.
For example, if you execute a queryHuman
query with a selection set containing
bio
, then the lambda function registered for Human.bio
is executed, as
follows:
This query generates the following response:
Was this page helpful?