Skip to content

Instantly share code, notes, and snippets.

@danstarns
Created April 21, 2021 10:53
Show Gist options
  • Save danstarns/e3956e1633ab9866bd0172dc85470659 to your computer and use it in GitHub Desktop.
Save danstarns/e3956e1633ab9866bd0172dc85470659 to your computer and use it in GitHub Desktop.
adding-constraints-to-neo4jgraphql
const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");
const { ApolloServer } = require("apollo-server");
const { getResolversFromSchema } = require("@graphql-tools/utils");
const { mergeTypeDefs } = require("@graphql-tools/merge");
const { makeExecutableSchema } = require("@graphql-tools/schema");
const { printSchema } = require("graphql");
const {
constraintDirective,
constraintDirectiveTypeDefs,
} = require("graphql-constraint-directive");
const typeDefs = `
type Movie {
title: String!
actors: [Person] @relationship(type: "ACTED_IN", direction: IN)
}
type Person {
name: String
}
`;
const driver = neo4j.driver(
"bolt://localhost:7687",
neo4j.auth.basic("admin", "password")
);
const neoSchema = new Neo4jGraphQL({
typeDefs,
driver,
});
const extendedTypeDefs = `
extend input MovieCreateInput {
title: String! @constraint(minLength: 5, format: "email")
}
`;
const schema = makeExecutableSchema({
typeDefs: mergeTypeDefs(
[
constraintDirectiveTypeDefs,
printSchema(neoSchema.schema),
extendedTypeDefs,
],
{ ignoreFieldConflicts: true }
),
resolvers: getResolversFromSchema(neoSchema.schema),
schemaTransforms: [constraintDirective()],
});
const server = new ApolloServer({
schema,
});
async function main() {
await server.listen(4000);
console.log("http://localhost:4000");
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment