Selection Set

This is a GraphQL specific term. When you preform a query you have the operation;

query {
    myOperation
}

And you also have a Selection Set;

query {
    myOperation {
        # Selection Set start
        id
        name
    } # Selection Set end
}

When using the OGM we do not want users providing a selections sets. Doing so would make the OGM feel more like querying the GraphQL Schema when the OGM is designed as an abstraction on top of it. To combat this we do Autogenerated Selection Sets. Given a Node;

type Node {
    id: ID
    name: String
    relation: [Node] @relationship(...)
    customCypher: [Node] @cypher(...)
}

We pre-generate a pre-defined selection set. We don’t include any relationships or cypher fields, as they could be computationally expensive. Given the above Node the auto pre-defined selection set would be;

{
    id
    name
}

This means that by default, querying for Node(s), you would only get the .id and .name properties returned. If you want to select more you can either define a selection set at execution time or as a static on the Model;

1. Selection set at execution time

const { OGM } = require("@neo4j/graphql-ogm")
const neo4j = require("neo4j-driver");

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

const typeDefs = `
    type Node {
        id: ID
        name: String
        relation: [Node] @relationship(...)
        customCypher: [Node] @cypher(...)
    }
`;

const ogm = new OGM({ typeDefs, driver });
const Node = ogm.model("Node");

const selectionSet = `
    {
        id
        name
        relation {
            id
            name
        }
        customCypher {
            id
            name
        }
    }
`;
const nodes = await Node.find({ selectionSet });

2. Selection set as a static

const { OGM } = require("@neo4j/graphql-ogm")
const neo4j = require("neo4j-driver");

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

const typeDefs = `
    type Node {
        id: ID
        name: String
        relation: [Node] @relationship(...)
        customCypher: [Node] @cypher(...)
    }
`;

const ogm = new OGM({ typeDefs, driver });
const Node = ogm.model("Node");

const selectionSet = `
    {
        id
        name
        relation {
            id
            name
        }
        customCypher {
            id
            name
        }
    }
`;
Node.setSelectionSet(selectionSet)