Error using cypher directive in GraphQL schema

Hello,
I'm new to grandstack. I faced an issue while following along the book "Fullstack graphql applications".
Everything works fine before the introduction of "@cypher" directive in my graphql schema. But once I write a field which makes use of this directive, my graphql api application fails to start.
Here's my code:

const typeDefs = /* GraphQL */  `
  type Business {
    businessId: ID!
    averageStars: Float!
      @cypher(
        statement: "MATCH (this)<-[:REVIEWS]-(r:Review) RETURN avg(r.stars)"
      )
    name: String!
    city: String!
    state: String!
    address: String!
    location: Point!
    reviews: [Review!]! @relationship(type: "REVIEWS", direction: IN)
    categories: [Category!]! @relationship(type: "IN_CATEGORY", direction: OUT)
  }

  type User {
    userID: ID!
    name: String!
    reviews: [Review!]! @relationship(type: "WROTE", direction: OUT)
  }

  type Review {
    reviewId: ID!
    stars: Float!
    date: Date!
    text: String
    user: User! @relationship(type: "WROTE", direction: IN)
    business: Business! @relationship(type: "REVIEWS", direction: OUT)
  }

  type Category {
    name: String!
    businesses: [Business!]! @relationship(type: "IN_CATEGORY", direction: IN)
  }
`; 

const driver = neo4j.driver(
  "bolt://localhost:7687",
  neo4j.auth.basic("neo4j", "*******")
);

const neoSchema = new Neo4jGraphQL({ typeDefs, driver });

neoSchema.getSchema().then((schema) => {
  const server = new ApolloServer({
    schema,
  });
  server.listen().then(({ url }) => {
    console.log(`GraphQL server ready at ${url}`);
  });
});

The error I get upon running the app:

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "[object Array]".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

Hi avitussweetbert213!

It looks like you're missing the columnName from your @cypher directive, so try adding that.

You'll want to catch the errors thrown from the library so you can get better error messages. I suggest you either add .catch(e => console.error(e)) or better yet wrap it all in a try/catch block like this:

try {
    const driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "password"));

    const neoSchema = new Neo4jGraphQL({ typeDefs, driver });

    const schema = await neoSchema.getSchema();
    const server = new ApolloServer({
        schema,
    });
    const { url } = await server.listen();
    console.log(`GraphQL server ready at ${url}`);
} catch (error) {
    console.log(error);
}

Finally, it looks like you posted your password, so I highly recommend changing that!

Best regards,

Michael

Hello Michael!
Thanks for your valuable post. After wrapping my code in "try/catch" block, it seems that is the problem. Can you explain a bit more about "colunmName" in cypher directive please?. I didn't find the argument from the original code I'm using.

We have a page in our documentation dedicated to this here: @cypher - Neo4j GraphQL Library

1 Like

Thank you, the issue is solved