-
Notifications
You must be signed in to change notification settings - Fork 12
added graphql connect #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
== Install the Neo4j GraphQL Library and dependencies | ||
|
||
[source, bash, copy=true] | ||
---- | ||
npm install @neo4j/graphql@alpha graphql neo4j-driver @apollo/server | ||
---- | ||
|
||
. `@neo4j/graphql` is the official Neo4j GraphQL Library package. | ||
It takes your GraphQL type definitions and generates a schema backed by a Neo4j database. | ||
. `graphql` generates a schema and execute queries and mutations. | ||
. `neo4j-driver` is the official Neo4j Driver package for JavaScript, of which an instance must be passed into the Neo4j GraphQL Library. | ||
. The https://www.apollographql.com/docs/apollo-server/[`@apollo/server`] is the default GraphQL server package for Apollo Server. | ||
|
||
link:https://neo4j.com/docs/graphql/current/getting-started/[More info on installing the Neo4j GraphQL Library] | ||
|
||
|
||
== Connect to the database | ||
|
||
The following JavaScript snippet connects to a Neo4j database. | ||
Set your values for `<neo4j-database-uri>`, `<username>` and `<password>`: | ||
|
||
[source, javascript, indent=0] | ||
---- | ||
import neo4j from "neo4j-driver"; | ||
import { Neo4jGraphQL } from "@neo4j/graphql"; | ||
|
||
const driver = neo4j.driver( | ||
"<neo4j-database-uri>", | ||
neo4j.auth.basic("<username>", "<password>") | ||
); | ||
|
||
const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); | ||
---- | ||
|
||
|
||
== Set GraphQL type definitions | ||
|
||
Add a constant to your JavaScript that holds GraphQL type definitions. | ||
Here is a simple example with two node types, one with label "Actor" and the other "Movie": | ||
|
||
[source, javascript, indent=0] | ||
---- | ||
const typeDefs = `#graphql | ||
type Movie @node { | ||
title: String | ||
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN) | ||
} | ||
|
||
type Actor @node { | ||
name: String | ||
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) | ||
} | ||
`; | ||
---- | ||
|
||
|
||
== Create an instance of `ApolloServer` | ||
|
||
Add the following to your JavaScript to create an Apollo Server instance: | ||
|
||
[source, javascript, indent=0] | ||
---- | ||
import { ApolloServer } from '@apollo/server'; | ||
import { startStandaloneServer } from '@apollo/server/standalone'; | ||
|
||
const server = new ApolloServer({ | ||
schema: await neoSchema.getSchema(), | ||
}); | ||
|
||
const { url } = await startStandaloneServer(server, { | ||
listen: { port: 4000 }, | ||
}); | ||
|
||
console.log(`🚀 Server ready at ${url}`); | ||
---- | ||
|
||
|
||
== Start the server | ||
|
||
Make sure that your JavaScript file looks like this: | ||
|
||
[source, javascript] | ||
---- | ||
import { ApolloServer } from '@apollo/server'; | ||
import { startStandaloneServer } from '@apollo/server/standalone'; | ||
import { Neo4jGraphQL } from "@neo4j/graphql"; | ||
import neo4j from "neo4j-driver"; | ||
|
||
const typeDefs = `#graphql | ||
type Movie @node { | ||
title: String | ||
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN) | ||
} | ||
|
||
type Actor @node { | ||
name: String | ||
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT) | ||
} | ||
`; | ||
|
||
const driver = neo4j.driver( | ||
"neo4j://localhost:7687", | ||
neo4j.auth.basic("username", "password") | ||
); | ||
|
||
const neoSchema = new Neo4jGraphQL({ typeDefs, driver }); | ||
|
||
const server = new ApolloServer({ | ||
schema: await neoSchema.getSchema(), | ||
}); | ||
|
||
const { url } = await startStandaloneServer(server, { | ||
context: async ({ req }) => ({ req }), | ||
listen: { port: 4000 }, | ||
}); | ||
|
||
console.log(`🚀 Server ready at ${url}`); | ||
---- | ||
|
||
You are ready to start up your GraphQL server. | ||
Execute your JavaScript with `node`: | ||
|
||
[source, bash, indent=0] | ||
---- | ||
node [yourJavaScriptFile].js | ||
---- | ||
|
||
If successful, you should see the following output: | ||
|
||
[source, bash, indent=0] | ||
---- | ||
🚀 Server ready at http://localhost:4000/ | ||
---- | ||
|
||
That is where the Apollo server starts. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a common, and good, practice to have the full code example at the end. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was trying to shorten the content, but i also agree that a full example is useful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added it |
||
|
||
Use the Apollo server to execute mutations and populate your database. | ||
See link:https://neo4j.com/docs/graphql/current/getting-started/#_create_nodes_in_the_database[Create nodes in the database]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not, but is it worth pointing out how you might get this information?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that can be left to the user in this case