$npx -y skills add neo4j-contrib/neo4j-skills --skill neo4j-graphql-skillBuild and configure a GraphQL API backed by Neo4j using @neo4j/graphql v7 (current) or v5 (LTS).
| 1 | ## When to Use |
| 2 | |
| 3 | - Creating a GraphQL API from a Neo4j graph schema with `@neo4j/graphql` |
| 4 | - Writing type definitions with `@relationship`, `@cypher`, `@authorization` directives |
| 5 | - Using OGM for server-side programmatic Neo4j access (bypasses GraphQL auth) |
| 6 | - Configuring auto-generated queries, mutations, subscriptions |
| 7 | - Securing types/fields with JWT or JWKS-based `@authorization` rules |
| 8 | - Migrating from v5/v6 to v7 (breaking changes below) |
| 9 | |
| 10 | ## When NOT to Use |
| 11 | |
| 12 | - **Raw Cypher queries outside GraphQL resolvers** → `neo4j-cypher-skill` |
| 13 | - **Spring Data Neo4j / Java entity mapping** → `neo4j-spring-data-skill` |
| 14 | - **Generic GraphQL without Neo4j** — outside scope |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | ## Version Matrix |
| 19 | |
| 20 | | Version | Status | Notes | |
| 21 | |---|---|---| |
| 22 | | v7 | Current | `@node` required; `options` removed; explicit `eq` syntax | |
| 23 | | v5 | LTS | Older syntax; `options: {limit, offset, sort}` still valid | |
| 24 | |
| 25 | Default to v7 unless codebase is on v5. |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | ## Step 1 — Install |
| 30 | |
| 31 | ```bash |
| 32 | npm install @neo4j/graphql neo4j-driver graphql @apollo/server |
| 33 | ``` |
| 34 | |
| 35 | For subscriptions (CDC required): |
| 36 | ```bash |
| 37 | npm install ws graphql-ws express body-parser cors |
| 38 | ``` |
| 39 | |
| 40 | --- |
| 41 | |
| 42 | ## Step 2 — Minimal Server Setup |
| 43 | |
| 44 | ```javascript |
| 45 | import { ApolloServer } from '@apollo/server'; |
| 46 | import { startStandaloneServer } from '@apollo/server/standalone'; |
| 47 | import { Neo4jGraphQL } from '@neo4j/graphql'; |
| 48 | import neo4j from 'neo4j-driver'; |
| 49 | |
| 50 | const typeDefs = `#graphql |
| 51 | type Movie @node { |
| 52 | id: ID! @id |
| 53 | title: String! |
| 54 | actors: [Person!]! @relationship(type: "ACTED_IN", direction: IN) |
| 55 | } |
| 56 | |
| 57 | type Person @node { |
| 58 | id: ID! @id |
| 59 | name: String! |
| 60 | movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) |
| 61 | } |
| 62 | `; |
| 63 | |
| 64 | const driver = neo4j.driver( |
| 65 | process.env.NEO4J_URI, |
| 66 | neo4j.auth.basic(process.env.NEO4J_USERNAME, process.env.NEO4J_PASSWORD) |
| 67 | ); |
| 68 | |
| 69 | const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); |
| 70 | |
| 71 | // assertIndexesAndConstraints syncs @id → UNIQUE constraints; wrap in try/catch |
| 72 | await neoSchema.assertIndexesAndConstraints({ options: { create: true } }); |
| 73 | |
| 74 | const server = new ApolloServer({ schema: await neoSchema.getSchema() }); |
| 75 | |
| 76 | const { url } = await startStandaloneServer(server, { |
| 77 | context: async ({ req }) => ({ token: req.headers.authorization }), |
| 78 | listen: { port: 4000 }, |
| 79 | }); |
| 80 | ``` |
| 81 | |
| 82 | `assertIndexesAndConstraints` throws if constraints missing. Use `{ create: true }` to auto-create, or run `CREATE CONSTRAINT` manually and retry. |
| 83 | |
| 84 | --- |
| 85 | |
| 86 | ## Key Directives |
| 87 | |
| 88 | ### @node (v7 required) |
| 89 | |
| 90 | Every GraphQL type representing a Neo4j node must have `@node`. Without it, v7 ignores the type. |
| 91 | |
| 92 | ```graphql |
| 93 | type Product @node { |
| 94 | id: ID! @id |
| 95 | name: String! |
| 96 | } |
| 97 | |
| 98 | # Custom label (default = type name) |
| 99 | type Article @node(labels: ["Post", "Content"]) { |
| 100 | title: String! |
| 101 | } |
| 102 | ``` |
| 103 | |
| 104 | ### @relationship — Full Syntax |
| 105 | |
| 106 | ```graphql |
| 107 | type Person @node { |
| 108 | # direction: OUT = (this)-[:KNOWS]->(other) |
| 109 | friends: [Person!]! @relationship(type: "KNOWS", direction: OUT) |
| 110 | |
| 111 | # direction: IN = (other)-[:ACTED_IN]->(this) |
| 112 | actedIn: [Movie!]! @relationship(type: "ACTED_IN", direction: IN) |
| 113 | |
| 114 | # direction: UNDIRECTED = matches both directions (use sparingly — double-counts) |
| 115 | colleagues: [Person!]! @relationship(type: "COLLEAGUE_OF", direction: UNDIRECTED) |
| 116 | |
| 117 | # Relationship with properties — reference an @relationshipProperties interface |
| 118 | reviews: [Movie!]! @relationship(type: "REVIEWED", direction: OUT, properties: "ReviewedProps") |
| 119 | } |
| 120 | |
| 121 | interface ReviewedProps @relationshipProperties { |
| 122 | rating: Int! |
| 123 | date: Date |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | Direction rule: `OUT` = arrow leaves this node. `IN` = arrow enters this node. Both sides of a relationship must declare opposite directions. |
| 128 | |
| 129 | ### Querying Relationship Properties — Connection API |
| 130 | |
| 131 | For each relationship with `properties:`, a `{field}Connection` field is auto-generated. Access rel properties via `actorsConnection.edges.properties`, not via `actors`: |
| 132 | |
| 133 | ```graphql |
| 134 | query { |
| 135 | movies(where: { title: { eq: "The Matrix" } }) { |
| 136 | title |
| 137 | actorsConnection { |
| 138 | edges { |
| 139 | properties { role } # maps to @relationshipProperties interface |
| 140 | node { name } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | ``` |
| 146 | |
| 147 | ### @cypher — Custom Resolver |
| 148 | |
| 149 | ```graphql |
| 150 | type Person @node { |
| 151 | name: String! |
| 152 | |
| 153 | # columnName must exactly match the RETURN alias — mismatch returns null silently |
| 154 | friendCount: Int |