Neo4j GraphQL with new @apollo/server

Are there examples of how to use Neo4j GraphQL with the new @apollo/server library? Your documentation and examples are out of date and use the apollo-server library that is deprecated and goes obsolete in October.

Here's a simple example I just tried (it ends up being very similar to the getting started example).

Install dependencies:

npm i @apollo/server @neo4j/graphql graphql neo4j-driver

index.js :

const { ApolloServer } = require("@apollo/server");
const { startStandaloneServer } = require("@apollo/server/standalone");
const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

const NEO4J_URI = "neo4j://localhost:7687";
const NEO4J_USER = "neo4j";
const NEO4J_PASSWORD = "letmeinnow";

const driver = neo4j.driver(
  NEO4J_URI,
  neo4j.auth.basic(NEO4J_USER, NEO4J_PASSWORD)
);

const typeDefs = `
type Movie {
  title: String!
  actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
}

type Actor {
  name: String!
  movies: [Movie!]! @relationship(type:"ACTED_IN", direction: OUT)
}
`;

const neoSchema = new Neo4jGraphQL({ typeDefs, driver });

neoSchema.getSchema().then(async (schema) => {
  const server = new ApolloServer({
    schema,
  });

  const { url } = await startStandaloneServer(server);
  console.log(`Server ready at ${url}`);
});

Start the GraphQL server:

node index.js

Open Apollo Studio at localhost:4000 and run a GraphQL query:

query {
  movies(options: { limit: 10 }) {
    title
  }
}
{
  "data": {
    "movies": [
      {
        "title": "Toy Story"
      },
      {
        "title": "Jumanji"
      },
      {
        "title": "Grumpier Old Men"
      },
      {
        "title": "Waiting to Exhale"
      },
      {
        "title": "Father of the Bride Part II"
      },
      {
        "title": "Heat"
      },
      {
        "title": "Sabrina"
      },
      {
        "title": "Tom and Huck"
      },
      {
        "title": "Sudden Death"
      },
      {
        "title": "GoldenEye"
      }
    ]
  }
}

I like the standalone mode - running the Apollo Studio Sandbox locally instead of redirecting to the hosted version is nice.

Thank you! I will put this to use today!

1 Like