Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-19-2022 11:50 AM
Hi everyone
I tried to setup a GraphQL server with Apollo for my neo4j database following the driver configuration tutorial.
The problem is that page is completely obsolete regards to the Apollo part as the tutorial is (probably, and at least in the best case, as there's no version at all mentionned anywhere so we can only play guesses) made for Apollo V3, while latest is now 4.3 so V4 is probably not that new, and there's been some significant changes between the two.
versions installed (all latest as I'm writing) :
resulting code is this :
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { gql } from "graphql-tag"
import neo4j from "neo4j-driver"
import { Neo4jGraphQL } from "@neo4j/graphql"
const typeDefs = gql`
type User
{
uid: ID
lastName: String
firstName: String
dob: Date
email: String
}
type Adhesion
{
year: Int
}
type Club
{
cid: ID
name: String
}
`
const neo4jdriver = neo4j.driver
(
"neo4j://localhost:7687",
neo4j.auth.basic ("neo4j", "testneo4j")
)
// const createQuery = 'CREATE (:Category {name: "verification" })'
// const matchQuery = 'MATCH (p:USER) RETURN p'
// neo4jdriver.session().run(createQuery)
// .then((results) =>
// {
// console.log(results);
// }).catch((errors) =>
// {
// console.log(errors);
// });
// console.log ("Driver : ", neo4jdriver)
const neo4jschema = new Neo4jGraphQL ({typeDefs, neo4jdriver})
const apolloserver = new ApolloServer(
{
schema: await neo4jschema.getSchema(),
context: ({ req }) => ({ req, executionContext: neo4jdriver }),
})
const { url } = await startStandaloneServer(apolloserver,
{
listen: { port: 4000 }
})
console.log (`Server running at ${url}`)
when trying to query in apollo's browser tab
query
{
users
{
uid
lastName
firstName
}
}
I'm getting this error in the browser tab :
var apolloserver // Otherwise it's local to the then block such that startStandaloneServer couldn't access it
neo4jschema.getSchema().then((schema) =>
{
apolloserver = new ApolloServer(
{
schema,
context: ({ req }) => ({ req, executionContext: neo4jdriver }),
})
})
Solved! Go to Solution.
12-21-2022 06:42 AM
ok I finally found my error and I was expecting it actually was a javascript error but not an async problem.
When you have a dictionnary in javascript, if the key and the variable containing the value have the same name you can write
const schema
{
typeDefs,
driver
}
instead of
const schema
{
typeDefs: typeDefs,
driver: driver
}
and as I had renamed the driver (too ambiguous about what it refers to, I had to write
const neo4jgraphql = new Neo4jGraphQL (
{
typeDefs,
driver: neo4jdriver
})
and now it works
12-21-2022 06:42 AM
ok I finally found my error and I was expecting it actually was a javascript error but not an async problem.
When you have a dictionnary in javascript, if the key and the variable containing the value have the same name you can write
const schema
{
typeDefs,
driver
}
instead of
const schema
{
typeDefs: typeDefs,
driver: driver
}
and as I had renamed the driver (too ambiguous about what it refers to, I had to write
const neo4jgraphql = new Neo4jGraphQL (
{
typeDefs,
driver: neo4jdriver
})
and now it works
All the sessions of the conference are now available online