Neo4j GraphQL Server
A quick way to get started using neo4j-graphql-binding with Apollo Server.
Last updated
A quick way to get started using neo4j-graphql-binding with Apollo Server.
Last updated
This package uses neo4j-graphql-binding with Apollo Server to make it easier to get started using a generated API or setting up multiple bindings.
The following describes the server setup process based on the default configuration:
neo4jIDL is called to update your Neo4j-GraphQL schema.
neo4jAssertConstraints is used to support a @unique
directive by creating constraints in your Neo4j instance. It uses the APOC extension.
buildNeo4jTypeDefs then augments the same typeDefs provided to your Neo4j-GraphQL schema.
neo4jGraphQLBinding is used to create a custom GraphQL Binding over the resulting augmented typeDefs. The binding is added into your server's context parameter (default key: 'neo4j' so you can access it the way you normally would access a GraphQL Binding.
buildNeo4jResolvers then generates any unprovided resolvers for query and mutation types that were generated or that use a @cypher directive. Each resolver uses a created binding to delegate all such queries and mutations to a Neo4j-GraphQL endpoint.
Finally, steps 1-5 are processed for any additional binding configurations provided in bindings
and the resulting typeDefs
and resolvers
are merged and provided to Apollo Server.
This example server setup uses only auto-generated query and mutation types.
typeDefs
Server
If you navigate to http://localhost:4000/, you should see GraphQL Playground.
This example uses nested create and connect mutations and takes advantage of the @unique directive to create the above graph with three Technology nodes.
Request
Response
Now we can run the following query:
Request
Response
The below typeDefs
shows the use of the @cypher
directive for a computed field, a query, and a mutation type. Any query
and mutation
types, or resolvers
, that you provide are not overwritten by the schema augmenting process.
typeDefs
The corresponding resolvers
can be provided if you want to handle the data or be explicit. Otherwise, they will be generated.
See the section on using the GraphQL Community Graph for an example of configuring bindings for multiple local or remote Neo4j instances with the Neo4j-GraphQL extension available.
This mutation creates a Prism Graph! 🌈
All the same arguments as Apollo Server are supported, in addition to the following:
typeDefs
(required): Your GraphQL type definitions in SDL format
driver
(required): Your Neo4j driver instance (More info here).
calls
Configures the use of neo4jAssertConstraints
and neo4jIDL
during setup.
assert
(default: true
): A boolean control that updates the unique property constraints in your Neo4j instance.
idl
(default: true
): A boolean control that updates your Neo4j-GraphQL schema.
augment
Configures the use of buildNeo4jTypeDefs
and buildNeo4jResolvers
during setup.
typeDefs
query
(default: true
) A boolean controlling the generation of query types.
mutation
(default: true
) A boolean controlling the generation of mutation types.
resolvers
query
(default: true
) A boolean controlling the generation of resolvers for query types.
mutation
(default: true
) A boolean controlling the generation of resolvers for mutation types.
indexConfig
Configures the management of generated id
fields.
use
(default/only: 'cuid'
) Configures what method to use when generating id field values.
bindingKey
(default: 'neo4j'
): The key used when storing the created binding into the server's context object.
log
(default: false
): Logs the result of any delegated query or mutation operation, buildNeo4jTypeDefs
,neo4jAssertConstraints
, and neo4jIDL
.
readOnly
(default: false): If you only have read access to a remote server, then you can use this parameter to turn off all processes that assume write access. Mutation types are not generated, idl
and assert
calls are prevented, and id
fields are not generated and managed because we would never be able to write them to the instance. So, this results in forcing the following configuration:
bindings
An object of bindings where each key is the name for a binding and each value is a configuration object containing the parameters: typeDefs
, resolvers
, driver
, calls
, augment,
log
, and readOnly
. This can be used to network together a GraphQL binding for multiple remote Neo4j instances with the Neo4j-GraphQL extension installed.