Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions code/graphql/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { makeAugmentedSchema, inferSchema } = require("neo4j-graphql-js");
const { ApolloServer } = require("apollo-server");
const neo4j = require("neo4j-driver");

const main = async () => {
// Create Neo4j driver instance
const driver = neo4j.driver(
"bolt://<HOST>:<BOLTPORT>",
neo4j.auth.basic("<USERNAME>", "<PASSWORD>")
);

let typeDefs;
// GraphQL type definitions can be inferred from existing database or
// specified explicitly. Uncomment the lines below to specify typedefs explicitly
// otherwise typedefs will be inferred from existing database
//typeDefs = /* GraphQL */ `
//type Person {
// name: String
// knows: [Person] @relation(name: "KNOWS", direction: "OUT")
// friendCount: Int @cypher(statement:"RETURN SIZE( (this)-[:KNOWS]->(:Person))")
//}
//`;

const getInferredTypes = async (driver) => {
const schemaInferenceOptions = {
alwaysIncludeRelationships: false,
};

const results = await inferSchema(driver, schemaInferenceOptions);
return results.typeDefs;
};

if (!typeDefs) {
typeDefs = await getInferredTypes(driver);
}

// Create executable GraphQL schema from GraphQL type definitions,
// using neo4j-graphql.js to autogenerate resolvers
const schema = makeAugmentedSchema({
typeDefs,
});

// Create ApolloServer instance to serve GraphQL schema
// Inject Neo4j driver instance into the context object
// which is passed into each (autogenerated) resolver
const server = new ApolloServer({
context: { driver },
schema,
});

// Start ApolloServer
server.listen().then(({ url }) => {
console.log(`GraphQL server ready at ${url}`);
});
};

main();