项目作者: neo4j-graphql

项目描述 :
NOTE: This project is no longer actively maintained. Please consider using the official Neo4j GraphQL Library (linked in README).
高级语言: JavaScript
项目地址: git://github.com/neo4j-graphql/neo4j-graphql-js.git
创建时间: 2017-10-02T19:51:20Z
项目社区:https://github.com/neo4j-graphql/neo4j-graphql-js

开源协议:Other

下载


⚠️ NOTE: This project is no longer actively maintained. Please consider using the official Neo4j GraphQL Library.

CI status npm version Docs link

neo4j-graphql.js

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

neo4j-graphql.js is facilitated by Neo4j Labs.

Installation and usage

Install

  1. npm install --save neo4j-graphql-js

Usage

Start with GraphQL type definitions:

  1. const typeDefs = `
  2. type Movie {
  3. title: String
  4. year: Int
  5. imdbRating: Float
  6. genres: [Genre] @relation(name: "IN_GENRE", direction: "OUT")
  7. }
  8. type Genre {
  9. name: String
  10. movies: [Movie] @relation(name: "IN_GENRE", direction: "IN")
  11. }
  12. `;

Create an executable schema with auto-generated resolvers for Query and Mutation types, ordering, pagination, and support for computed fields defined using the @cypher GraphQL schema directive:

  1. import { makeAugmentedSchema } from 'neo4j-graphql-js';
  2. const schema = makeAugmentedSchema({ typeDefs });

Create a neo4j-javascript-driver instance:

  1. import { v1 as neo4j } from 'neo4j-driver';
  2. const driver = neo4j.driver(
  3. 'bolt://localhost:7687',
  4. neo4j.auth.basic('neo4j', 'letmein')
  5. );

Use your favorite JavaScript GraphQL server implementation to serve your GraphQL schema, injecting the Neo4j driver instance into the context so your data can be resolved in Neo4j:

  1. import { ApolloServer } from 'apollo-server';
  2. const server = new ApolloServer({ schema, context: { driver } });
  3. server.listen(3003, '0.0.0.0').then(({ url }) => {
  4. console.log(`GraphQL API ready at ${url}`);
  5. });

If you don’t want auto-generated resolvers, you can also call neo4jgraphql() in your GraphQL resolver. Your GraphQL query will be translated to Cypher and the query passed to Neo4j.

  1. import { neo4jgraphql } from 'neo4j-graphql-js';
  2. const resolvers = {
  3. Query: {
  4. Movie(object, params, ctx, resolveInfo) {
  5. return neo4jgraphql(object, params, ctx, resolveInfo);
  6. }
  7. }
  8. };

What is neo4j-graphql.js

A package to make it easier to use GraphQL and Neo4j together. neo4j-graphql.js 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.

Goals

  • Translate GraphQL queries to Cypher to simplify the process of writing GraphQL resolvers
  • Allow for custom logic by overriding of any resolver function
  • Work with graphql-tools, graphql-js, and apollo-server
  • Support GraphQL servers that need to resolve data from multiple data services/databases
  • Expose the power of Cypher through GraphQL via the @cypher directive

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

Contributing

See our detailed contribution guidelines.

Examples

See /examples

Documentation

Full docs can be found in docs/

Debugging and Tuning

You can log out the generated cypher statements with an environment variable:

  1. DEBUG=neo4j-graphql-js node yourcode.js

This helps to debug and optimize your database statements. E.g. visit your Neo4J
browser console at http://localhost:7474/browser/ and paste the following:

  1. :params :params { offset: 0, first: 12, filter: {}, cypherParams: { currentUserId: '42' } }

and now profile the generated query:

  1. EXPLAIN MATCH (`post`:`Post`) WITH `post` ORDER BY post.createdAt DESC RETURN `post` { .id , .title } AS `post`

You can learn more by typing:

  1. :help EXPLAIN
  2. :help params