neo4j-graphql.js API Reference

This reference documents the exports from neo4j-graphql-js:

Exports

makeAugmentedSchema(options): <GraphQLSchema>

Wraps makeExecutableSchema to create a GraphQL schema from GraphQL type definitions (SDL). Will generate Query and Mutation types for the provided type definitions and attach neo4jgraphql as the resolver for these queries and mutations. Either a schema or typeDefs must be provided. resolvers can optionally be implemented to override any of the generated Query/Mutation fields. Additional options are passed through to makeExecutableSchema.

Parameters

  • options: <Object>
    • schema: <GraphQLSchema>
    • typeDefs: <String>
    • resolvers: <Object>
    • logger: <Object>
    • allowUndefinedInResolve = false
    • resolverValidationOptions = {}
    • directiveResolvers = null
    • schemaDirectives = null
    • parseOptions = {}
    • inheritResolversFromInterfaces = false
    • config: <Object> = {}

config

config is an object that can contain several optional keys as detailed in the table below.

KeyDescriptionDefaultOptionsExample
queryConfigure the autogenerated Query fields. Can be enabled/disabled for all types or a list of individual types to exclude can be passed. Commonly used to exclude payload types.trueBoolean or Object{query: false}

{query: {exclude: ["MyPayloadType"]}
mutationConfigure the autogenerated Mutation fields. Can be enabled/disabled for all types or a list of individual types to exclude can be passed. Commonly used to exclude payload types.trueBoolean or Object{mutation: false}

{query: {exclude: ["MyPayloadType"]}
debugEnable/disable logging of generated Cypher queries and parameters.TrueBoolean{debug: false}
authUsed to enable authorization schema directives (@isAuthenticated, @hasRole, @hasScope). If enabled, directives from the graphql-auth-directives are declared and can be used in the schema. If @hasScope is enabled it is automatically added to all generated query and mutation fields. See the authorization guide for more information.falseBoolean or Object{auth: true}

{auth: {isAuthenticated: true}, {hasRole: true}

For example:

const schema = makeAugmentedSchema({
typeDefs,
config: {
query: true, //default
mutation: false
}
});

or

const schema = makeAugmentedSchema({
typeDefs,
config: {
query: {
exclude: ["MyPayloadType"]
},
mutation: {
exclude: ["MyPayloadType"]
}
}
});

Returns

GraphQLSchema


assertSchema(options):null

This function uses the @id, @unique, and @index schema directives present in the GraphQL type definitions to add any database constraints and indexes.

Parameters

  • options: <Object>
    • schema: <GraphQLSchema>
    • driver: <Neo4jDriver>
    • debug: <Bool> = false
    • dropExisting: <Bool> = true

Example

import {
makeAugmentedSchema,
assertSchema
} from 'neo4j-graphql-js';
const driver = neo4j.driver(...);
const schema = makeAugmentedSchema(...);
assertSchema({ schema, driver, debug: true });

neo4jgraphql(object, params, context, resolveInfo, debug): <ExecutionResult>

This function's signature matches that of GraphQL resolver functions. and thus the parameters match the parameters passed into resolve by GraphQL implementations like graphql-js.

It can be called within a resolver to generate a Cypher query and handle the database call to Neo4j to completely resolve the GraphQL request. Alternatively, use cypherQuery or cypherMutation within a resolver to only generate the Cypher query and handle the database call yourself.

Parameters

  • object: <Object>

The previous object being resolved. Rarely used for a field on the root Query type.

  • params: <Object>

The arguments provided to the field in the GraphQL query.

  • context: <Object>

Value provided to every resolver and hold contextual information about the request, such as the currently logged in user, or access to a database. neo4j-graphql-js assumes a neo4j-javascript-driver instance exists in this object, under the key driver.

  • resolveInfo: <GraphQLResolveInfo>

Holds field-specific infomation relevant to the current query as well as the GraphQL schema.

  • debug: Boolean (default: true)

Specifies whether to log the generated Cypher queries for each GraphQL request. Logging is enabled by default.

Returns

ExecutionResult


augmentSchema(schema, config): <GraphQLSchema>

Takes an existing GraphQL schema object and adds neo4j-graphql-js specific enhancements, including auto-generated mutations and queries, and ordering and pagination fields. See this guide for more information.

NOTE: Only use augmentSchema if you are working with an existing GraphQLSchema object. In most cases you should use makeAugmentedSchema which can construct the GraphQLSchema object from type definitions.

Parameters

  • schema: <GraphQLSchema>
  • config: <Object>

config is an object that can contain several optional keys. See the details in the table below for makeAugmentedSchema

For example:

const augmentedSchema = augmentSchema(schema, {
query: true, //default
mutation: false
});

or

const augmentedSchema = augmentSchema(schema, {
query: {
exclude: ["MyPayloadType"]
},
mutation: {
exclude: ["MyPayloadType"]
}
});

Returns

GraphQLSchema


cypherQuery(params, context, resolveInfo)

Generates a Cypher query (and associated parameters) to resolve a given GraphQL request (for a Query). Use this function when you want to handle the database call yourself, use neo4jgraphql for automated database call support.

Parameters

  • params: <Object>
  • context: <Object>
  • resolveInfo: <GraphQLResolveInfo>

Returns

[<String>, <Object>]

Returns an array where the first element is the genereated Cypher query and the second element is an object with the parameters for the generated Cypher query.


cypherMutation(params, context, resolveInfo

Similar to cypherQuery, but for mutations. Generates a Cypher query (and associated parameters) to resolve a given GraphQL request (for a Mutation). Use this function when you want to handle the database call yourself, use neo4jgraphql for automated database call support.

Parameters

  • params: <Object>
  • context: <Object>
  • resolveInfo: <GraphQLResolveInfo>

Returns

[<String>, <Object>]

Returns an array where the first element is the genereated Cypher query and the second element is an object with the parameters for the generated Cypher query.


inferSchema(driver, [options]): <Promise>

Used to generate GraphQL type definitions from an existing Neo4j database by inspecting the data stored in the database. When used in combination with makeAugmentedSchema this can be used to generate a GraphQL CRUD API on top of an existing Neo4j database without writing any resolvers or GraphQL type definitions. See example/autogenerated/autogen.js for an example of using inferSchema and makeAugmentedSchema with Apollo Server.

Parameters

  • driver: <Neo4jDriver>
  • options: <Object>
    • alwaysIncludeRelationships: Boolean - specifies whether relationships should always be included in the type definitions as relationship types, even if the relationships do not have properties.

Returns

Promise that resolves to an object that contains:

  • typeDefs: String - a string representation of the generated GraphQL type definitions in Schema Definition Language (SDL) format, inferred from the existing Neo4j database.