From b47bd0bc0a1ac1df1a88fdd51325e08b2eb3c04b Mon Sep 17 00:00:00 2001 From: Richard Sill Date: Wed, 9 Apr 2025 17:01:11 +0200 Subject: [PATCH] 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