Neo4j GraphQL
- Neo4j GraphQL Toolbox
- GraphQL Directives
- Query and Aggregation
- Filtering
- Sorting
- Pagination
- Authentication and Authorization
- Excluded Directives OGM
- Introspector
- Codegen
Neo4j supports GraphQL with NodeJS, making it possible to interact with the database like a OGM.
There is the most comprehensive docs I found: https://neo4j.com/docs/graphql/current/
https://www.npmjs.com/package/@neo4j/graphql-ogm is a GraphQL OGM for Neo4j GraphQL.
Neo4j GraphQL Toolbox
https://graphql-toolbox.neo4j.io/
The Neo4j GraphQL Toolbox is an onboarding, low-code tool that can be integrated to Neo4j. It was created for development and experimentation with Neo4j GraphQL APIs. With it, you can:
- Connect to a Neo4j database with valid credentials.
- Define (or introspect) the type definitions.
- Build the Neo4j GraphQL schema.
- Experiment, query, and play.
Enter your GraphQL schema in toolbox, it will generate the full API server for experiment.
You can use the generated Queries and Mutations with type prompts. No real server is required to run the generated API.
Build a local graphql apollo server, with GraphQL codegen library, you can generate TypeScript GraphQL Client code.
Include the operations you've experimented, then you can use the generated client code to interact with the database.
GraphQL Directives
Basics
Direction of relationship means the direction of the arrow in the graph. IN
is the direction the arrow in pointing to.
An edge can also have properties defined in interface.
Autogeneration
https://neo4j.com/docs/graphql/current/type-definitions/directives/autogeneration/
@id
Autogeneration of IDs for the field.
@timestamp
@populatedBy
specify a callback function that gets executed during GraphQL query parsing, to populate fields which have not been provided within the input.
Example (slug)
Pass callback function from client side.
Example (modifiedBy)
Context values can be used in callback function.
@cypher
https://neo4j.com/docs/graphql/current/type-definitions/directives/cypher/
this
This value is a reference to the currently resolved node, and it can be used to traverse the graph.
auth
This value is represented by the following TypeScript interface definition:
You can use the JWT in the request to return the value of the currently logged in User:
cypherParams
Inject values into cypher query from GraphQL context function.
Can be used to parse JWT token and inject user id into context.
alias
maps a GraphQL field to a Neo4j property on a node or relationship
Indexes and Constraints
https://neo4j.com/docs/graphql/current/type-definitions/directives/indexes-and-constraints/
@unique
@fulltext
Use @fulltext
directive to add a Full text index.
This @fulltext
directive will create a full text index on the name
field of the Product
type.
This will generate a new query
Sample Usage
Schema Configuration
Type Configuration
https://neo4j.com/docs/graphql/current/schema-configuration/type-configuration/
@query
This directive is used to limit the availability of query operations in the library.
@mutation
This directive is used to limit the availability of mutation operations in the library.
@subscription
Field Configuration
https://neo4j.com/docs/graphql/current/schema-configuration/field-configuration/#_selectable
@relationship
@selectable
- This directive sets the availability of fields on queries and aggregations. It has two arguments:
onRead
: if disabled, this field is not available on queries and subscriptions.onAggregate
: if disabled, aggregations is not available for this field.
@settable
- This directive sets the availability of the input field on creation and update mutations. It has two arguments:
onCreate
: if disabled, this field is not available on creation operations.onUpdate
: if disabled, this field is not available on update operations.
@filterable
- This directive defines the filters generated for the field to which this directive is applied. It has two arguments:
byValue
: if disabled, this field does not generate value filters.byAggregate
: if disabled, this field does not generate aggregation filters.
Query and Aggregation
Aggregation
https://neo4j.com/docs/graphql/current/queries-aggregations/aggregations/
shortest
,longest
min
,max
,average
,sum
min
,max
Filtering
_LT
_LTE
_GT
_GTE
These features can be disabled or enabled
There are much more aggregate functions.
Sorting
https://neo4j.com/docs/graphql/current/queries-aggregations/sorting/
Pagination
https://neo4j.com/docs/graphql/current/queries-aggregations/pagination/
Authentication and Authorization
https://neo4j.com/docs/graphql/current/authentication-and-authorization/
@authentication
and @authorization
directive can be used.
Authorization without Authentication
Excluded Directives OGM
https://neo4j.com/docs/graphql/current/ogm/directives/#_excluded_directives
The following directives are excluded from the OGM. Reason: OGM is not designed to be exposed API which needs security measures.
@authentication
@authorization
@subscriptionsAuthorization
@query
@mutation
@subscription
@filterable
@selectable
@settable
This doesn't mean you can't use GraphQL for exposed API. OMG is designed for internal use.
This is how ogm work. Like Prisma. It's always used programmatically on server so there is no need for Auth.
https://neo4j.com/docs/graphql/current/ogm/installation/
Introspector
https://github.com/neo4j/graphql/tree/dev/packages/introspector
This is a tool that enables you, with very little effort, to introspect the schema/data model in an existing Neo4j database and build up a set of data structures that can be transformed into any output format.
Basically it will generate the schema from database relationships.
This could be helpful when you have a database and you want to generate the schema from it, but may not help when you need to design the schema first.
Codegen
In order to use codegen (https://the-guild.dev/graphql/codegen) to generate TypeScript types and sdk, you have to run a graphql server and pass the url to schema.
How is this guide?