Search code examples
neo4jgraphqlgrandstack

Error: Unknown directive "relation". with Grand-stack of neo4j


I am trying grand-stack-starter from neo4j and getting the below error with API module after I do all the graphql schema part. it complains that directive 'relation' and 'cypher'are unknown. I reinstalled neo4j-graphql-js but didnt solve the problem. Below is the error message

grand-stack-starter-api@0.0.1 start C:\Users\grand-stack-starter-master\api
nodemon --exec babel-node src/index.js

[nodemon] 1.18.9
[nodemon] to restart at any time, enter rs
[nodemon] watching: .
[nodemon] starting babel-node src/index.js
C:\Users\grand-stack-starter-master\api\node_modules\graphql\validation\validate.js:89
throw new Error(errors.map(function (error) {
^

Error: Unknown directive "relation".

Unknown directive "relation".

Unknown directive "cypher".

Unknown directive "cypher".

Unknown directive "relation".

Unknown directive "relation".

Unknown directive "relation".

Unknown directive "relation".

Unknown directive "relation".

Unknown directive "cypher".
    at assertValidSDL (C:\Users\N19683\grand-stack-starter-master\api\node_modules\graphql\validation\validate.js:89:11)
    at Object.buildASTSchema (C:\Users\N19683\grand-stack-starter-master\api\node_modules\graphql\utilities\buildASTSchema.js:67:34)
    at Object.buildSchemaFromTypeDefinitions (C:\Users\N19683\grand-stack-starter-master\api\node_modules\graphql-tools\src\generate\buildSchemaFromTypeDefinitions.ts:43:32)
    at makeExecutableSchema (C:\Users\N19683\grand-stack-starter-master\api\node_modules\graphql-tools\src\makeExecutableSchema.ts:52:16)
    at Object.<anonymous> (C:/Users/N19683/grand-stack-starter-master/api/src/index.js:18:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at loader (C:\Users\N19683\grand-stack-starter-master\api\node_modules\babel-register\lib\node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (C:\Users\N19683\grand-stack-starter-master\api\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
**

Below is the graphql-schema.js

import { neo4jgraphql } from "neo4j-graphql-js";

export const typeDefs = `
type User {
  id: ID!
  name: String
  friends: [User] @relation(name: "FRIENDS", direction: "BOTH")
  reviews: [Review] @relation(name: "WROTE", direction: "OUT")
  avgStars: Float
    @cypher(
      statement: "MATCH (this)-[:WROTE]->(r:Review) RETURN toFloat(avg(r.stars))"
    )
  numReviews: Int
    @cypher(statement: "MATCH (this)-[:WROTE]->(r:Review) RETURN COUNT(r)")
}

type Business {
  id: ID!
  name: String
  address: String
  city: String
  state: String
  reviews: [Review] @relation(name: "REVIEWS", direction: "IN")
  categories: [Category] @relation(name: "IN_CATEGORY", direction: "OUT")
}

type Review {
  id: ID!
  stars: Int
  text: String
  date: Date
  business: Business @relation(name: "REVIEWS", direction: "OUT")
  user: User @relation(name: "WROTE", direction: "IN")
}

type Category {
  name: ID!
  businesses: [Business] @relation(name: "IN_CATEGORY", direction: "IN")
}

type Query {
  usersBySubstring(substring: String): [User]
    @cypher(
      statement: "MATCH (u:User) WHERE u.name CONTAINS $substring RETURN u"
    )
}
`

export const resolvers = {
  Query: {
    Users: neo4jgraphql,
    Business: neo4jgraphql,
    Category: neo4jgraphql,
    Review: neo4jgraphql
  }
};

index.js

import { typeDefs, resolvers } from "./graphql-schema";
import { ApolloServer, gql, makeExecutableSchema  } from "apollo-server";
import { v1 as neo4j } from "neo4j-driver";
import { augmentSchema } from "neo4j-graphql-js";
import dotenv from "dotenv";

// set environment variables from ../.env
dotenv.config();



const schema = makeExecutableSchema({
  typeDefs,
  resolvers
});

const augmentedSchema = augmentSchema(schema);

const driver = neo4j.driver(
  process.env.NEO4J_URI || "bolt://localhost:7689",
  neo4j.auth.basic(
    process.env.NEO4J_USER || "neo4j",
    process.env.NEO4J_PASSWORD || "letmein"
  )
);


const server = new ApolloServer({
  context: { driver },
  schema: augmentedSchema
});

server.listen(process.env.GRAPHQL_LISTEN_PORT, "0.0.0.0").then(({ url }) => {
  console.log(`GraphQL API ready at ${url}`);
});

Can anyone please help to fix the issue! Thanks in advance


Solution

  • As noted in the docs

    NOTE: Only use augmentSchema if you are working with an existing GraphQLSchema object. In most cases you should use makeAugmentedSchema which can construct the GraphQLSchema object from type definitions.

    The error is occurring because you're attempting to use makeExecutableSchema to create your schema. The @relation and @cypher directives are used exclusively by neo4j-graphql. Since they're not actually defined as part of your schema, building your schema with makeExecutableSchema will result in an error just like using any other undefined directive.

    You should just use makeAugmentedSchema instead:

    const schema = makeAugmentedSchema({
      typeDefs,
      resolvers,
    })
    const driver = /* ... */
    const server = new ApolloServer({
      context: { driver },
      schema,
    })