|
| 1 | +== Install |
| 2 | + |
| 3 | +Install the Neo4j GraphQL library and its peer dependencies with `npm`. |
| 4 | + |
| 5 | +[source, bash] |
| 6 | +---- |
| 7 | +npm install @neo4j/graphql@alpha graphql neo4j-driver @apollo/server |
| 8 | +---- |
| 9 | + |
| 10 | +link:https://neo4j.com/docs/graphql/current/getting-started/[More info on installing the Neo4j GraphQL Library] |
| 11 | + |
| 12 | + |
| 13 | +== Connect to the database |
| 14 | + |
| 15 | +Connect to the Neo4j server via `neo4j.driver()` and your server credentials. |
| 16 | + |
| 17 | +[source, javascript] |
| 18 | +---- |
| 19 | +import neo4j from "neo4j-driver"; |
| 20 | +
|
| 21 | +const driver = neo4j.driver( |
| 22 | + "<neo4j-database-uri>", |
| 23 | + neo4j.auth.basic("<username>", "<password>") |
| 24 | +); |
| 25 | +---- |
| 26 | + |
| 27 | + |
| 28 | +== Set GraphQL type definitions |
| 29 | + |
| 30 | +Provide the GraphQL type definitions mapping to the nodes and relationships in your Neo4j database through `Neo4jGraphQL`. |
| 31 | + |
| 32 | +.Type definitions for node labels `Actor` and `Movie` |
| 33 | +[source, javascript] |
| 34 | +---- |
| 35 | +import { Neo4jGraphQL } from "@neo4j/graphql"; |
| 36 | +
|
| 37 | +const typeDefs = `#graphql |
| 38 | + type Movie @node { |
| 39 | + title: String |
| 40 | + actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN) |
| 41 | + } |
| 42 | +
|
| 43 | + type Actor @node { |
| 44 | + name: String |
| 45 | + movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) |
| 46 | + } |
| 47 | +`; |
| 48 | +
|
| 49 | +const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); |
| 50 | +---- |
| 51 | + |
| 52 | + |
| 53 | +== Create an instance of Apollo Server |
| 54 | + |
| 55 | +Start an `ApolloServer` instance drawing its schema from the Neo4j type definitions. |
| 56 | +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. |
| 57 | + |
| 58 | +[source, javascript] |
| 59 | +---- |
| 60 | +import { ApolloServer } from '@apollo/server'; |
| 61 | +import { startStandaloneServer } from '@apollo/server/standalone'; |
| 62 | +
|
| 63 | +const server = new ApolloServer({ |
| 64 | + schema: await neoSchema.getSchema(), |
| 65 | +}); |
| 66 | +
|
| 67 | +const { url } = await startStandaloneServer(server, { |
| 68 | + listen: { port: 4000 }, |
| 69 | +}); |
| 70 | +
|
| 71 | +console.log(`🚀 Server ready at ${url}`); |
| 72 | +---- |
0 commit comments