Skip to main content

A GraphQL to Cypher query execution layer for Neo4j and Python GraphQL implementations.

Project description

neo4j-graphql-py

A GraphQL to Cypher query execution layer for Neo4j and Python GraphQL implementations.

Installation and usage

Install

pip install neo4j-graphql-py

Then call neo4j_graphql() in your GraphQL resolver. Your GraphQL query will be translated to Cypher and the query passed to Neo4j.

from neo4j_graphql_py import neo4j_graphql

def resolve(obj, info, **kwargs):
    return neo4j_graphql(obj, info.context, info, **kwargs)

resolvers = {
  'Query': {
    'Movie':resolve
  }
}

What is neo4j-graphql-py

A package to make it easier to use GraphQL and Neo4j together. neo4j-graphql-py translates GraphQL queries to a single Cypher query, eliminating the need to write queries in GraphQL resolvers and for batching queries. It also exposes the Cypher query language through GraphQL via the @cypher schema directive.

How it works

Start with a GraphQL schema

GraphQL First Development is all about starting with a well defined GraphQL schema. Here we'll use the GraphQL schema IDL syntax:

type_defs = '''
directive @cypher(statement: String!) on FIELD_DEFINITION
directive @relation(name:String!, direction:String!) on FIELD_DEFINITION

type Movie {
  movieId: ID!
  title: String
  year: Int
  plot: String
  poster: String
  imdbRating: Float
  similar(first: Int = 3, offset: Int = 0): [Movie] @cypher(statement: "MATCH (this)-[:IN_GENRE]->(:Genre)<-[:IN_GENRE]-(o:Movie) RETURN o")
  degree: Int @cypher(statement: "RETURN SIZE((this)-->())")
  actors(first: Int = 3, offset: Int = 0): [Actor] @relation(name: "ACTED_IN", direction:"IN")
}

type Actor {
  id: ID!
  name: String
  movies: [Movie]
}


type Query {
  Movie(id: ID, title: String, year: Int, imdbRating: Float, first: Int, offset: Int): [Movie]
}
'''

We define two types, Movie and Actor as well as a top level Query Movie which becomes our entry point. This looks like a standard GraphQL schema, except for the use of two directives @relation and @cypher. In GraphQL directives allow us to annotate fields and provide an extension point for GraphQL.

  • @cypher directive - maps the specified Cypher query to the value of the field. In the Cypher query, this is bound to the current object being resolved.
  • @relation directive - used to indicate relationships in the data model. The name argument specifies the relationship type, and direction indicates the direction of the relationship ("IN" or "OUT" are valid values)

Translate GraphQL To Cypher

Inside each resolver, use neo4j-graphql() to generate the Cypher required to resolve the GraphQL query, passing through the query arguments, context and resolveInfo objects.

from neo4j_graphql_py import neo4j_graphql

resolvers = {
  # entry point to GraphQL service
  'Query': {
    'Movie': lambda obj, info, **kwargs: neo4j_graphql(obj, info.context,info, **kwargs)
  }
}

GraphQL to Cypher translation works by inspecting the GraphQL schema, the GraphQL query and arguments. For example, this simple GraphQL query

{
  Movie(title: "River Runs Through It, A") {
    title
    year
    imdbRating
  }
}

is translated into the Cypher query

MATCH (movie:Movie {title: "River Runs Through It, A"})
RETURN movie { .title , .year , .imdbRating } AS movie
SKIP 0

A slightly more complicated traversal

{
  Movie(title: "River Runs Through It, A") {
    title
    year
    imdbRating
    actors {
      name
    }
  }
}

becomes

MATCH (movie:Movie {title: "River Runs Through It, A"})
RETURN movie { .title , .year , .imdbRating, actors: [(movie)<-[:ACTED_IN]-(movie_actors:Actor) | movie_actors { .name }] }
AS movie
SKIP 0

@cypher directive

NOTE: The @cypher directive has a dependency on the APOC procedure library, specifically the function apoc.cypher.runFirstColumn to run sub-queries. If you'd like to make use of the @cypher feature you'll need to install appropriate version of APOC in Neo4j

GraphQL is fairly limited when it comes to expressing complex queries such as filtering, or aggregations. We expose the graph querying language Cypher through GraphQL via the @cypher directive. Annotate a field in your schema with the @cypher directive to map the results of that query to the annotated GraphQL field. For example:

type Movie {
  movieId: ID!
  title: String
  year: Int
  plot: String
  similar(first: Int = 3, offset: Int = 0): [Movie] @cypher(statement: "MATCH (this)-[:IN_GENRE]->(:Genre)<-[:IN_GENRE]-(o:Movie) RETURN o ORDER BY COUNT(*) DESC")
}

The field similar will be resolved using the Cypher query

MATCH (this)-[:IN_GENRE]->(:Genre)<-[:IN_GENRE]-(o:Movie) RETURN o ORDER BY COUNT(*) DESC

to find movies with overlapping Genres.

Querying a GraphQL field marked with a @cypher directive executes that query as a subquery:

GraphQL:

{
  Movie(title: "River Runs Through It, A") {
    title
    year
    imdbRating
    actors {
      name
    }
    similar(first: 3) {
      title
    }
  }
}

Cypher:

MATCH (movie:Movie {title: "River Runs Through It, A"})
RETURN movie { .title , .year , .imdbRating,
  actors: [(movie)<-[:ACTED_IN]-(movie_actors:Actor) | movie_actors { .name }],
  similar: [ x IN apoc.cypher.runFirstColumn("
        WITH {this} AS this
        MATCH (this)-[:IN_GENRE]->(:Genre)<-[:IN_GENRE]-(o:Movie)
        RETURN o",
        {this: movie}, true) | x { .title }][..3]
} AS movie
SKIP 0

Query Neo4j

Inject a Neo4j driver instance in the context of each GraphQL request and neo4j-graphql-py will query the Neo4j database and return the results to resolve the GraphQL query.

from neo4j_graphql_py import make_executable_schema
schema = make_executable_schema(type_defs, resolvers)
import neo4j
def context(request):
    global driver
    if driver is None:
        driver = neo4j.GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "neo4j"))

    return {'driver': driver, 'request': request}
from ariadne.asgi import GraphQL
import uvicorn
rootValue = {}
app = GraphQL(schema=schema, root_value=rootValue, context_value=context, debug=True)
uvicorn.run(app)

See /examples for complete examples using different GraphQL server libraries.

Benefits

  • Send a single query to the database
  • No need to write queries for each resolver
  • Exposes the power of the Cypher query language through GraphQL

Examples

See /examples for complete examples using different GraphQL server libraries.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neo4j-graphql-py-0.1.4.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

neo4j_graphql_py-0.1.4-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file neo4j-graphql-py-0.1.4.tar.gz.

File metadata

  • Download URL: neo4j-graphql-py-0.1.4.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for neo4j-graphql-py-0.1.4.tar.gz
Algorithm Hash digest
SHA256 70fbe3ca2f743eddb0f2d32c591896b372f1d3a6388a913011e2a8ea36ab36eb
MD5 d277d154fc41a491c39e37169bc06f2e
BLAKE2b-256 2fadc4b4efb322c4252353e6f85c9309cf766c179b3dd86f31b9a6280ed78370

See more details on using hashes here.

File details

Details for the file neo4j_graphql_py-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: neo4j_graphql_py-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for neo4j_graphql_py-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 56c4df37a287ce4c0c4e3a1fd8682c97e8e41bcb672f7451c4d9f9d63c66a912
MD5 93ae8b5000364d4b497ba34935fbedc5
BLAKE2b-256 efdb0d0b70b3142b5d38b5416d02a35ab8144a0107fc05e7df718d8339961ce7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page