Created
April 21, 2021 10:53
-
-
Save danstarns/e3956e1633ab9866bd0172dc85470659 to your computer and use it in GitHub Desktop.
adding-constraints-to-neo4jgraphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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