The Neo4j GraphQL Library

The toolbox provides an easy-to-use interface over the Neo4j GraphQL Library .

The fundamental goal of the Neo4j GraphQL Library is to make it easier to build GraphQL APIs backed by Neo4j.

Graphs and GraphQL

It’s important to note that GraphQL is an API query language and NOT a database query language. The Neo4j GraphQL Library helps you build an API layer between the client and database, not to execute GraphQL queries directly against the database.

The Neo4j GraphQL Library focuses on the following high-level objectives:

  • Reducing boilerplate

  • Developer productivity

  • Extensibility

  • Performance

Goals of the Neo4j GraphQL Library

GraphQL First Development

GraphQL type definitions can drive the database data model, meaning you don’t need to maintain two separate schemas for your API and database.

GraphQL first development

Auto-generate GraphQL API Operations

With the Neo4j GraphQL Library, GraphQL type definitions provide the starting point for a generated API that includes:

  • Query & Mutation types (an API entry-point for each type defined in the schema)

  • Ordering

  • Pagination

  • Complex filtering

  • DateTime & Spatial types and filtering

Generated CRUD API

Generate Cypher From GraphQL Operations

The Neo4j GraphQL Library automatically generates a single database query for any arbitrary GraphQL request.

Single automatically generated queries:

  • remove the need to implement resolvers

  • reduce boilerplate code

  • optimize the performance of queries

  • GraphQL operations require only a single roundtrip to the database

Generate Cypher from GraphQL
Click to reveal an example of a generated Cypher query
GraphQL
GraphQL Query
{
  movies(where: { title:  {
    eq: "River Runs Through It, A"
  }}) {
    title
    actors(limit: 2) {
      name
    }
    genres {
      name
    }
    directors {
      name
      movies(limit: 3){
        title
      }
    }
  }
}
Cypher
Generated Cypher Query
MATCH (this:Movie)
WHERE this.title = $param0
CALL {
    WITH this
    MATCH (this)<-[this0:ACTED_IN]-(this1:Actor)
    WITH DISTINCT this1
    WITH this1 { .name } AS this1

    LIMIT $param1
    RETURN collect(this1) AS var2
}
CALL {
    WITH this
    MATCH (this)-[this3:HAS_GENRE]->(this4:Genre)
    WITH DISTINCT this4
    WITH this4 { .name } AS this4
    RETURN collect(this4) AS var5
}
CALL {
    WITH this
    MATCH (this)-[this6:DIRECTED]->(this7:Director)
    WITH DISTINCT this7
    CALL {
        WITH this7
        MATCH (this7)<-[this8:DIRECTED]-(this9:Movie)
        WITH DISTINCT this9
        WITH this9 { .title } AS this9

        LIMIT $param2
        RETURN collect(this9) AS var10
    }
    WITH this7 { .name, movies: var10 } AS this7
    RETURN collect(this7) AS var11
}
RETURN this { .title, actors: var2, genres: var5, directors: var11 } AS this

Extend GraphQL With Cypher

To add custom logic beyond CRUD operations, you can use the @cypher GraphQL schema directive to add computed fields bound to a Cypher statement to the GraphQL schema.

GraphQL
recommended(first: Int = 1): [Business] @cypher(
    statement: """
        MATCH (this)-[:RECOMMENDED]->(b:Business)
        RETURN b
    """,
    columnName: "b"
)

Check Your Understanding

1. Neo4j GraphQL Library objectives

Which of the following are the four high-level objectives of the Neo4j GraphQL Library:

  • ✓ Reducing boilerplate

  • ❏ Database agnostic

  • ✓ Developer productivity

  • ❏ No Cypher

  • ✓ Extensibility

  • ✓ Performance

Hint

The key objectives support the fundamental goal of the Neo4j GraphQL Library to make it easier to build GraphQL APIs backed by Neo4j.

Solution

At a high level, the objectives of the Neo4j GraphQL Library are focused on:

  • Reducing boilerplate

  • Developer productivity

  • Extensibility

  • Performance

Summary

In this lesson, you explored the features of the Neo4j GraphQL Library.

In the next module, you will learn how to write GraphQL queries to query the API generated by the Neo4j GraphQL Library