API Reference

The following sections work as a reference guide to all functionalities in OGM and in the Model class.

OGM

Function Description Example

constructor

Returns an OGM instance. Takes an input object as a parameter, which is then passed to the Neo4jGraphQL constructor.

const ogm = new OGM({
    typeDefs,
});

init

Asynchronous method to initialize the OGM. Internally, calls Neo4jGraphQL.getSchema() to generate a GraphQL schema, and stores the result. Initializes any models which have been created before this execution, and will throw an error if any of them are invalid.

await ogm.init();

model

Returns a model instance matching the passed in name, or (if the OGM has been initialized) throws an Error if one can’t be found. Accepts a single argument name of type string.

Type definition
type User {
    username: String!
}
Query return
const User = ogm.model("User");
Wrong query
const User = ogm.model("NotFound");

generate

Either writes to specified outFile or returns a string - if noWrite is set.

Writing to outFile
import { OGM, generate } from "@neo4j/graphql-ogm";

const typeDefs = `
    type Movie {
        id: ID
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://localhost:7687",
    neo4j.auth.basic("username", "password")
);

const ogm = new OGM({ typeDefs, driver });

await generate({
    ogm,
    outFile: "path/to/my/file.ts",
});

console.log("Types Generated");
Writing with noWrite
import { OGM, generate } from "@neo4j/graphql-ogm";

const typeDefs = `
    type Movie {
        id: ID
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://localhost:7687",
    neo4j.auth.basic("username", "password")
);

const ogm = new OGM({ typeDefs, driver });

const source = await generate({
    ogm,
    noWrite: true,
});

console.log("Types Generated ", source);

assertIndexesAndConstraints

Asynchronous method to assert the existence of database constraints, that either resolves to void in a successful scenario, or throws an error if the necessary constraints do not exist following its execution. It takes an input object as a parameter, being the supported fields the following examples.

Given the type definitions saved to the variable typeDefs and a valid driver instance saved to the variable driver:

type Book {
    isbn: String! @unique
}

And the construction and initialization of an OGM, using:

const ogm = new OGM({
    typeDefs,
});
await ogm.init();

The following checks whether a unique node property constraint exists for label "Book" and property "isbn", and throws an error if it does not:

await ogm.assertIndexesAndConstraints();

The next example creates the constraint if it does not exist:

await ogm.assertIndexesAndConstraints({ options: { create: true } });

Input

Accepts the argument:

Name Type Description

options

AssertConstraintsOptions

Options for the execution of assertIndexesAndConstraints.

AssertConstraintsOptions

Name Type Description

create

boolean

Whether or not to create constraints if they do not yet exist. Disabled by default.

Model

aggregate

This method can be used to aggregate nodes, and maps to the underlying schema Aggregate.

Arguments

Name Type Description

where

GraphQLWhereArg

A JavaScript object representation of the GraphQL where input type used for Filtering.

Example

Here is how you can write a query to find the longest User name:

const User = ogm.model("User");

const usersAggregate = await User.aggregate({
    aggregate: {
        name: {
            longest: true
        }
    }
});

And this one is to find the longest User name where name starts with the letter "D":

const User = ogm.model("User");

const usersAggregate = await User.aggregate({
    where: {
        name_STARTS_WITH: "D"
    },
    aggregate: {
        name: {
            longest: true
        }
    }
});

create

This method can be used to update nodes, and maps to the underlying create Mutation. It returns a Promise that resolves to the equivalent of the Mutation response for this operation.

Arguments

Name Type Description

input

any

JavaScript object representation of the GraphQL input input type used for Create mutations.

selectionSet

string or DocumentNode or SelectionSetNode

Selection set for the Mutation, see Selection Set for more information.

args

any

The args value for the GraphQL Mutation.

context

any

The context value for the GraphQL Mutation.

rootValue

any

The rootValue value for the GraphQL Mutation.

Example

Here is an example on how to create a Movie with title "The Matrix":

const Movie = ogm.model("Movie");

await Movie.create({ input: [{ title: "The Matrix" }] })

delete

This method can be used to delete nodes, and maps to the underlying Delete Mutation. It returns a Promise which resolvers to a DeleteInfo object:

Name Type Description

nodesDeleted

number

The number of nodes deleted.

relationshipsDeleted

number

The number of relationships deleted.

Arguments

Name Type Description

where

GraphQLWhereArg

A JavaScript object representation of the GraphQL where input type used for Filtering.

delete

string or DocumentNode or SelectionSetNode

A JavaScript object representation of the GraphQL delete input type used for delete mutations.

context

any

The context value for the GraphQL mutation.

rootValue

any

The rootValue value for the GraphQL mutation.

Example

This is how you can delete all User nodes where the name is "Dan":

const User = ogm.model("User");

await User.delete({ where: { name: "Dan" }});

find

This method can be used to find nodes, and maps to the underlying schema Queries. It returns a Promise which resolvers to an array of objects matching the type of the Model.

Arguments

Name Type Description

where

GraphQLWhereArg

A JavaScript object representation of the GraphQL where input type used for Filtering.

options

GraphQLOptionsArg

A JavaScript object representation of the GraphQL options input type used for Sorting and Pagination.

selectionSet

string or DocumentNode or SelectionSetNode

Selection set for the Mutation, see Selection Set for more information.

args

any

The args value for the GraphQL Mutation.

context

any

The context value for the GraphQL Mutation.

rootValue

any

The rootValue value for the GraphQL Mutation.

Example

Here is how to find all user nodes in the database:

const User = ogm.model("User");

const users = await User.find();

In case you want to find users with name "Jane Smith", here is how to do it:

const User = ogm.model("User");

const users = await User.find({ where: { name: "Jane Smith" }});

update

This method can be used to update nodes, and maps to the underlying update mutation. It returns a Promise that resolves to the equivalent of the mutation response for this operation.

Arguments

Name Type Description

where

GraphQLWhereArg

A JavaScript object representation of the GraphQL where input type used for Filtering.

update

any

A JavaScript object representation of the GraphQL update input type used for update Mutations.

connect

any

A JavaScript object representation of the GraphQL connect input type used for update Mutations.

disconnect

any

A JavaScript object representation of the GraphQL disconnect input type used for update Mutations.

create

any

A JavaScript object representation of the GraphQL create input type used for update Mutations.

options

GraphQLOptionsArg

A JavaScript object representation of the GraphQL options input type used for Sorting and Pagination.

selectionSet

string or DocumentNode or SelectionSetNode

Selection set for the Mutation, see Selection set for more information.

args

any

The args value for the GraphQL Mutation.

context

any

The context value for the GraphQL Mutation.

rootValue

any

The rootValue value for the GraphQL Mutation.

Example

Here is how to update the User with name "John" to be "Jane":

const User = ogm.model("User");

const { users } = await User.update({
    where: { name: "John" },
    update: { name: "Jane" },
});