Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Autogenerate GraphQL for ArangoDB

DZone's Guide to

Autogenerate GraphQL for ArangoDB

Learn how to make the tedious process of querying ArangoDB with GraphQL easier for users so that they only have to define the GraphQL IDL file and simple AQL queries.

· Database Zone
Free Resource

Read why times series is the fastest growing database category.

Currently, querying ArangoDB with GraphQL requires building a GraphQL.js schema. This is tedious, and the resulting JavaScript schema file can be long and bulky. Here, we will demonstrate a short proof of concept that reduces the user-related part to only defining the GraphQL IDL file and simple AQL queries.

The Apollo GraphQL project built a library that takes a GraphQL IDL and resolver functions to build a GraphQL.js schema. Resolve functions are called by GraphQL to get the actual data from the database. I modified the library in a way that before the resolvers are added, I read the IDL AST and create resolver functions.

In order to simplify things and not depend on special "magic," let's introduce the directive @aql. With this directive, it's possible to write an AQL query that gets the needed data. With the bind parameter @current, it is possible to access the current parent object to do JOINs or related operations.

A GraphQL IDL

type BlogEntry {
  _key: String!
  authorKey: String!
 
  author: Author @aql(exec: "FOR author IN Author FILTER author._key == @current.authorKey RETURN author")
}
 
type Author {
  _key: String!
  name: String
}
 
type Query {
  blogEntry(_key: String!): BlogEntry
}

This IDL describes a BlogEntry and an Author object. The BlogEntry holds an Author object that is fetched via the AQL query in the directive @aql. The type Query defines a query that fetches one BlogEntry

Now let's have a look at a GraphQL query:

{
  blogEntry(_key: "1") {
    _key
    authorKey
    author {
      name
    }
  }
}

This query fetches the BlogEntry with _key 1. The generated AQL query is:

FOR doc IN BlogEntry FILTER doc._key == '1' RETURN doc

And with the fetched BlogEntry document, the corresponding Author is fetched via the AQL query defined in the directive.

The result will approximately look like this:

{
  "data" : {
    "blogEntry" : {
      "_key" : "1",
      "authorKey" : "2",
      "author" : {
        "name" : "Author Name"
      }
    }
  }
}

As a conclusion of this short demo, we can claim that with the usage of GraphQL's IDL, it is possible to reduce effort on the user side to query ArangoDB with GraphQL. For simple GraphQL queries and IDLs, it's possible to automatically generate resolvers to fetch the necessary data.

The effort resulted in an npm package is called graphql-aql-generator.

ArangoDB Foxx Example

Now let's have a look at the same example, but with using ArangoDB javascript framework - Foxx. To do so, we have to follow the simple steps listed below:

  1. Open the ArangoDB web interface and navigate to SERVICES.
  2. Then click Add Service. Select New Service and fill out all fields with *. Very important is the Mount field. I will use /test. Then click Generate.
  3. Click on the service to open its settings. Click Settings and then go to Set Development to enable the development mode.
  4. Then click Info and open the path at Path:

Now we have to install the npm package:

npm install --save graphql-aql-generator

We also need the collections Author and BlogEntry. And the following documents:

Author collection:

{
  "_key":"2"
  "name": "Author Name"
}

BlogEntry collection:

{
  "_key":"1"
  "authorKey": "2"
}

Foxx has a built-in GraphQL router that we can use to serve GraphQL queries. We assemble a new route called /graphql that serves the incoming GraphQL queries. With graphiql: true, we enable the GraphiQL explorer so we can test-drive our queries.

const createGraphQLRouter = require('@arangodb/foxx/graphql');
const generator = require('graphql-aql-generator');
 
const typeDefs = [`...`]
 
const schema = generator(typeDefs);
 
router.use('/graphql', createGraphQLRouter({
schema: schema,
graphiql: true,
graphql: require('graphql-sync')
}));

Open 127.0.0.1:8529/test/graphql and the GraphiQL explorer is loaded so we can execute a query to fetch a BlogEntry with an Author.

{
  blogEntry(_key: "1") {
    _key
    authorKey
    author {
	  name
	}
  }
}
```
The result is:
 
```
{
  "data": {
    "blogEntry": {
	  "_key": "1",
	  "authorKey": "2",
	  "author": {
	    "name": "Author Name"
	  }
    }
  }
}

For the sake of completeness, here is the full Foxx example that works by copy-and-paste. Do not forget to do
npm install graphql-aql-generator and create the collections and documents.

// main.js code
'use strict';
 
const createRouter = require('@arangodb/foxx/router');
const router = createRouter();
module.context.use(router);
 
const createGraphQLRouter = require('@arangodb/foxx/graphql');
const generator = require('graphql-aql-generator');
 
const typeDefs = [`
type BlogEntry {
_key: String!
authorKey: String!
 
author: Author @aql(exec: "FOR author in Author filter author._key == @current.authorKey return author")
}
 
type Author {
_key: String!
name: String
}
 
type Query {
blogEntry(_key: String!): BlogEntry
}
`]
 
const schema = generator(typeDefs);
 
router.use('/graphql', createGraphQLRouter({
schema: schema,
graphiql: true,
graphql: require('graphql-sync')
}));

Learn how to get 20x more performance than Elastic by moving to a Time Series database.

Topics:
database ,graphql ,arangodb ,autogenerated key ,tutorial ,querying

Published at DZone with permission of Manuel Baesler, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}