From b47bd0bc0a1ac1df1a88fdd51325e08b2eb3c04b Mon Sep 17 00:00:00 2001 From: Richard Sill Date: Wed, 9 Apr 2025 17:01:11 +0200 Subject: [PATCH 1/7] 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/7] 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/7] 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: From 958594e3f736c9959b69524fc8fdba138bf556e6 Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 11 Jun 2025 10:20:58 +0200 Subject: [PATCH 4/7] shorten --- documentation/connect-drivers-graphql.adoc | 96 ++++------------------ 1 file changed, 15 insertions(+), 81 deletions(-) diff --git a/documentation/connect-drivers-graphql.adoc b/documentation/connect-drivers-graphql.adoc index 14b0a86..cbddb10 100644 --- a/documentation/connect-drivers-graphql.adoc +++ b/documentation/connect-drivers-graphql.adoc @@ -1,45 +1,39 @@ -== Install the Neo4j GraphQL Library and dependencies +== Install -[source, bash, copy=true] +Install the Neo4j GraphQL library and its peer dependencies with `npm`. + +[source, bash] ---- 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 ``: +Connect to the Neo4j server via `neo4j.driver()` and your server credentials. -[source, javascript, indent=0] +[source, javascript] ---- 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 -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": +Provide the GraphQL type definitions mapping to the nodes and relationships in your Neo4j database through `Neo4jGraphQL`. -[source, javascript, indent=0] +.Type definitions for node labels `Actor` and `Movie` +[source, javascript] ---- +import { Neo4jGraphQL } from "@neo4j/graphql"; + const typeDefs = `#graphql type Movie @node { title: String @@ -51,88 +45,28 @@ const typeDefs = `#graphql movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) } `; ----- - - -== Create an instance of `ApolloServer` - -Add the following to 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}`); +const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); ---- -== Start the server +== Create an instance of Apollo Server -Make sure that your JavaScript file looks like this: +Start an `ApolloServer` instance drawing its schema from the Neo4j type definitions. +Execute mutations and link:https://neo4j.com/docs/graphql/current/getting-started/#_create_nodes_in_the_database[create nodes in the database] through the Apollo server. [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`: - -[source, bash, indent=0] ----- -node [yourJavaScriptFile].js ----- - -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 ac8a087be78cfbd885071222b346d9df8acc3ffe Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 11 Jun 2025 10:36:01 +0200 Subject: [PATCH 5/7] rename (it's not a driver). --- .../{connect-drivers-graphql.adoc => connect-graphql.adoc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename documentation/{connect-drivers-graphql.adoc => connect-graphql.adoc} (100%) diff --git a/documentation/connect-drivers-graphql.adoc b/documentation/connect-graphql.adoc similarity index 100% rename from documentation/connect-drivers-graphql.adoc rename to documentation/connect-graphql.adoc From 9bc08afb011bfafe1522a6d64f27d14539a8bf1f Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 11 Jun 2025 11:28:26 +0200 Subject: [PATCH 6/7] Remove `alpha` tag --- documentation/connect-graphql.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/connect-graphql.adoc b/documentation/connect-graphql.adoc index cbddb10..a843286 100644 --- a/documentation/connect-graphql.adoc +++ b/documentation/connect-graphql.adoc @@ -4,7 +4,7 @@ Install the Neo4j GraphQL library and its peer dependencies with `npm`. [source, bash] ---- -npm install @neo4j/graphql@alpha graphql neo4j-driver @apollo/server +npm install @neo4j/graphql graphql neo4j-driver @apollo/server ---- link:https://neo4j.com/docs/graphql/current/getting-started/[More info on installing the Neo4j GraphQL Library] From a258a44fc70d0368b7658dd3a833268f702e0cc1 Mon Sep 17 00:00:00 2001 From: Stefano Ottolenghi Date: Wed, 11 Jun 2025 11:31:27 +0200 Subject: [PATCH 7/7] rel type in example title --- documentation/connect-graphql.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/connect-graphql.adoc b/documentation/connect-graphql.adoc index a843286..2d91890 100644 --- a/documentation/connect-graphql.adoc +++ b/documentation/connect-graphql.adoc @@ -29,7 +29,7 @@ const driver = neo4j.driver( Provide the GraphQL type definitions mapping to the nodes and relationships in your Neo4j database through `Neo4jGraphQL`. -.Type definitions for node labels `Actor` and `Movie` +.Type definitions for node labels `Actor` and `Movie` and relationship `ACTED_IN` [source, javascript] ---- import { Neo4jGraphQL } from "@neo4j/graphql";