Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
93 changes: 93 additions & 0 deletions documentation/connect-drivers-graphql.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
== 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>`:
Copy link

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?


[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

Extend your JavaScript with a constant that holds GraphQL type definitions.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Extend" in the programming context is a word which already has a lot of meaning, perhaps we can rephrase this?

I am thinking something as simple as "Add onto" or so.

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`

Extend 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

You are ready to start up your GraphQL server.
Execute your JavaScript with `node`.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how far we want to go, we can also provide an example of how to use node to start the server, though I do note at no point did we mention "create a file called x.js" etc.


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.
Copy link

Choose a reason for hiding this comment

The 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.


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].