From b47bd0bc0a1ac1df1a88fdd51325e08b2eb3c04b Mon Sep 17 00:00:00 2001 From: Richard Sill Date: Wed, 9 Apr 2025 17:01:11 +0200 Subject: [PATCH 1/3] added graphql connect --- documentation/connect-drivers-graphql.adoc | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 documentation/connect-drivers-graphql.adoc diff --git a/documentation/connect-drivers-graphql.adoc b/documentation/connect-drivers-graphql.adoc new file mode 100644 index 0000000..2248480 --- /dev/null +++ b/documentation/connect-drivers-graphql.adoc @@ -0,0 +1,93 @@ +== Install the Neo4j GraphQL Library and dependencies + +[source, bash, copy=true] +---- +npm install @neo4j/graphql@alpha graphql neo4j-driver @apollo/server +---- + +. `@neo4j/graphql` is the official Neo4j GraphQL Library package. + It takes your GraphQL type definitions and generates a schema backed by a Neo4j database. +. `graphql` generates a schema and execute queries and mutations. +. `neo4j-driver` is the official Neo4j Driver package for JavaScript, of which an instance must be passed into the Neo4j GraphQL Library. +. The https://www.apollographql.com/docs/apollo-server/[`@apollo/server`] is the default GraphQL server package for Apollo Server. + +link:https://neo4j.com/docs/graphql/current/getting-started/[More info on installing the Neo4j GraphQL Library] + + +== Connect to the database + +The following JavaScript snippet connects to a Neo4j database. +Set your values for ``, `` and ``: + +[source, javascript, indent=0] +---- +import neo4j from "neo4j-driver"; +import { Neo4jGraphQL } from "@neo4j/graphql"; + +const driver = neo4j.driver( + "", + neo4j.auth.basic("", "") +); + +const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); +---- + + +== Set GraphQL type definitions + +Extend your JavaScript with a constant that holds GraphQL type definitions. +Here is a simple example with two node types, one with label "Actor" and the other "Movie": + +[source, javascript, indent=0] +---- +const typeDefs = `#graphql + type Movie @node { + title: String + actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN) + } + + type Actor @node { + name: String + movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) + } +`; +---- + + +== Create an instance of `ApolloServer` + +Extend your JavaScript to create an Apollo Server instance: + +[source, javascript, indent=0] +---- +import { ApolloServer } from '@apollo/server'; +import { startStandaloneServer } from '@apollo/server/standalone'; + +const server = new ApolloServer({ + schema: await neoSchema.getSchema(), +}); + +const { url } = await startStandaloneServer(server, { + listen: { port: 4000 }, +}); + +console.log(`🚀 Server ready at ${url}`); +---- + + +== Start the server + +You are ready to start up your GraphQL server. +Execute your JavaScript with `node`. + +If successful, you should see the following output: + +[source, bash, indent=0] +---- +🚀 Server ready at http://localhost:4000/ +---- + +That is where the Apollo server starts. + +Use the Apollo server to execute mutations and populate your database. +See link:https://neo4j.com/docs/graphql/current/getting-started/#_create_nodes_in_the_database[Create nodes in the database]. \ No newline at end of file From 7575b03e0718df4f4b2de56a65f9ed82ba430c16 Mon Sep 17 00:00:00 2001 From: Richard Sill <156673635+rsill-neo4j@users.noreply.github.com> Date: Thu, 15 May 2025 15:50:57 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- documentation/connect-drivers-graphql.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/connect-drivers-graphql.adoc b/documentation/connect-drivers-graphql.adoc index 2248480..9b69072 100644 --- a/documentation/connect-drivers-graphql.adoc +++ b/documentation/connect-drivers-graphql.adoc @@ -35,7 +35,7 @@ const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); == Set GraphQL type definitions -Extend your JavaScript with a constant that holds GraphQL type definitions. +Add a constant to your JavaScript that holds GraphQL type definitions. Here is a simple example with two node types, one with label "Actor" and the other "Movie": [source, javascript, indent=0] From 1e116036ed0e17a032f87c6fbef158050e5076e9 Mon Sep 17 00:00:00 2001 From: Richard Sill Date: Fri, 16 May 2025 14:08:34 +0200 Subject: [PATCH 3/3] review suggestions --- documentation/connect-drivers-graphql.adoc | 49 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/documentation/connect-drivers-graphql.adoc b/documentation/connect-drivers-graphql.adoc index 9b69072..14b0a86 100644 --- a/documentation/connect-drivers-graphql.adoc +++ b/documentation/connect-drivers-graphql.adoc @@ -56,7 +56,7 @@ const typeDefs = `#graphql == Create an instance of `ApolloServer` -Extend your JavaScript to create an Apollo Server instance: +Add the following to your JavaScript to create an Apollo Server instance: [source, javascript, indent=0] ---- @@ -77,8 +77,53 @@ console.log(`🚀 Server ready at ${url}`); == Start the server +Make sure that your JavaScript file looks like this: + +[source, javascript] +---- +import { ApolloServer } from '@apollo/server'; +import { startStandaloneServer } from '@apollo/server/standalone'; +import { Neo4jGraphQL } from "@neo4j/graphql"; +import neo4j from "neo4j-driver"; + +const typeDefs = `#graphql + type Movie @node { + title: String + actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN) + } + + type Actor @node { + name: String + movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) + } +`; + +const driver = neo4j.driver( + "neo4j://localhost:7687", + neo4j.auth.basic("username", "password") +); + +const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); + +const server = new ApolloServer({ + schema: await neoSchema.getSchema(), +}); + +const { url } = await startStandaloneServer(server, { + context: async ({ req }) => ({ req }), + listen: { port: 4000 }, +}); + +console.log(`🚀 Server ready at ${url}`); +---- + You are ready to start up your GraphQL server. -Execute your JavaScript with `node`. +Execute your JavaScript with `node`: + +[source, bash, indent=0] +---- +node [yourJavaScriptFile].js +---- If successful, you should see the following output: