DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Think Reactive and Native With Quarkus, Kotlin, and GraphQL
  • How To Set Up a Scalable and Highly-Available GraphQL API in Minutes
  • GraphQL: How Easy Is It?
  • Unleash the power of Anypoint DataGraph

Trending

  • How To Verify Database Connection From a Spring Boot Application
  • Breaking Down Silos: The Importance of Collaboration in Solution Architecture
  • 5 Web3 Trends to Follow in 2023
  • Deploy a Session Recording Solution Using Ansible and Audit Your Bastion Host
  1. DZone
  2. Data Engineering
  3. Databases
  4. Autogenerate GraphQL for ArangoDB

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.

Manuel Baesler user avatar by
Manuel Baesler
·
Oct. 06, 17 · Tutorial
Like (3)
Save
Tweet
Share
4.89K Views

Join the DZone community and get the full member experience.

Join For Free

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')
}));
GraphQL Database ArangoDB

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

Opinions expressed by DZone contributors are their own.

Related

  • Think Reactive and Native With Quarkus, Kotlin, and GraphQL
  • How To Set Up a Scalable and Highly-Available GraphQL API in Minutes
  • GraphQL: How Easy Is It?
  • Unleash the power of Anypoint DataGraph

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: