diff --git a/documentation/connect-graphql.adoc b/documentation/connect-graphql.adoc new file mode 100644 index 0000000..2d91890 --- /dev/null +++ b/documentation/connect-graphql.adoc @@ -0,0 +1,72 @@ +== Install + +Install the Neo4j GraphQL library and its peer dependencies with `npm`. + +[source, bash] +---- +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] + + +== Connect to the database + +Connect to the Neo4j server via `neo4j.driver()` and your server credentials. + +[source, javascript] +---- +import neo4j from "neo4j-driver"; + +const driver = neo4j.driver( + "", + neo4j.auth.basic("", "") +); +---- + + +== Set GraphQL type definitions + +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` and relationship `ACTED_IN` +[source, javascript] +---- +import { Neo4jGraphQL } from "@neo4j/graphql"; + +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 neoSchema = new Neo4jGraphQL({ typeDefs, driver }); +---- + + +== Create an instance of Apollo Server + +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'; + +const server = new ApolloServer({ + schema: await neoSchema.getSchema(), +}); + +const { url } = await startStandaloneServer(server, { + listen: { port: 4000 }, +}); + +console.log(`🚀 Server ready at ${url}`); +----