Dgraph Lambda Overview
Lambda provides a way to write custom logic in JavaScript, integrate it with your GraphQL schema, and execute it using the GraphQL API in a few easy steps.
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.
Lambda provides a way to write your custom logic in JavaScript, integrate it with your GraphQL schema, and execute it using the GraphQL API in a few easy steps:
- Set up a Dgraph cluster with a working lambda server
- Declare lambda queries, mutations, and fields in your GraphQL schema as needed
- Define lambda resolvers for them in a JavaScript file
This also simplifies the job of developers, as they can build a complex backend that’s rich with business logic, without setting up multiple different services. Also, you can build your backend in JavaScript, which means you can build both your frontend and backend using the same language.
Dgraph doesn’t execute your custom logic itself. It makes external HTTP requests to a user-defined lambda server.
If you want to deploy your own lambda server, you can find the implementation of Dgraph Lambda in our open source repository.
Declaring lambda in a GraphQL schema
There are three places where you can use the @lambda
directive and thus tell
Dgraph where to apply custom JavaScript logic.
- You can add lambda fields to your types and interfaces, as follows:
- You can add lambda queries to the Query type, as follows:
- You can add lambda mutations to the Mutation type, as follows:
Defining lambda resolvers in JavaScript
A lambda resolver is a user-defined JavaScript function that performs custom actions over the GraphQL types, interfaces, queries, and mutations. There are two methods to register JavaScript resolvers:
self.addGraphQLResolvers
self.addMultiParentGraphQLResolvers
Functions self.addGraphQLResolvers
and self.addMultiParentGraphQLResolvers
can be called multiple times in your resolver code.
addGraphQLResolvers
The self.addGraphQLResolvers
method takes an object as an argument, which maps
a resolver name to the resolver function that implements it. The resolver
functions registered using self.addGraphQLResolvers
receive
{ parent, args, graphql, dql }
as argument:
parent
, the parent object for which to resolve the current lambda field registered usingaddGraphQLResolver
. Theparent
receives all immediate fields of that object, whether or not they were actually queried. Available only for types and interfaces (null
for queries and mutations)args
, the set of arguments for lambda queries and mutationsgraphql
, a function to execute auto-generated GraphQL API calls from the lambda server. The user’s auth header is passed back to thegraphql
function, so this can be used securelydql
, provides an API to execute DQL from the lambda serverauthHeader
, provides the JWT key and value of the auth header passed from the client
The addGraphQLResolvers
can be represented with the following TypeScript
types:
self.addGraphQLResolvers
is the default choice for registering resolvers
when the result of the lambda for each parent is independent of other parents.
Each resolver function should return data in the exact format as the return type of GraphQL field, query, or mutation for which it’s being registered.
In the following example, the resolver function myTypeResolver
registered for
the customField
field in MyType
returns a string because the return type of
that field in the GraphQL schema is String
:
Another resolver example using a graphql
call:
addMultiParentGraphQLResolvers
The self.addMultiParentGraphQLResolvers
is useful in scenarios where you want
to perform computations involving all the parents returned from Dgraph for a
lambda field. This is useful in two scenarios:
- When you want to perform a computation between parents
- When you want to execute a complex query, and want to optimize it by firing a single query for all the parents
This method takes an object as an argument, which maps a resolver name to the
resolver function that implements it. The resolver functions registered using
this method receive { parents, args, graphql, dql }
as argument:
parents
, a list of parent objects for which to resolve the current lambda field registered usingaddMultiParentGraphQLResolvers
. Available only for types and interfaces (null
for queries and mutations)args
, the set of arguments for lambda queries and mutations (null
for types and interfaces)graphql
, a function to execute auto-generated GraphQL API calls from the lambda serverdql
, provides an API to execute DQL from the lambda serverauthHeader
, provides the JWT key and value of the auth header passed from the client
The addMultiParentGraphQLResolvers
can be represented with the following
TypeScript types:
This method shouldn’t be used for lambda queries or lambda mutations.
Each resolver function should return data as a list of the return type of GraphQL field for which it’s being registered.
In the following example, the resolver function rank()
registered for the
rank
field in Author
, returns a list of integers because the return type of
that field in the GraphQL schema is Int
:
Scripts containing import packages (such as this example) require compilation using Webpack.
The following example resolver uses a dql
call:
The following resolver example uses a graphql
call and manually overrides the
authHeader
provided by the client:
Example
For example, if you execute the following lambda query:
You should see a response such as the following:
Learn more
To learn more about the @lambda
directive, see:
Was this page helpful?