首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:未知指令“关系”。使用大堆栈的neo4j

错误:未知指令“关系”。使用大堆栈的neo4j
EN

Stack Overflow用户
提问于 2019-10-15 07:48:58
回答 1查看 1.9K关注 0票数 2

我正在尝试从neo4j开始大堆栈,并在完成所有graphql模式部分之后,使用API模块获得下面的错误。它抱怨指令'relation‘和’cypher‘是未知的。我重新安装了新4j-graphql-js,但没有解决这个问题。

代码语言:javascript
复制
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)
**

下面是graphql-schema.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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}`);
});

有人能帮我解决这个问题吗?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-15 11:43:01

正如文档中所指出的

注意:只有在使用现有的augmentSchema对象时才使用GraphQLSchema。在大多数情况下,您应该使用makeAugmentedSchema,它可以根据类型定义构造GraphQLSchema对象。

出现此错误是因为您试图使用makeExecutableSchema创建架构。@relation@cypher指令完全由neo4j-graphql使用。由于它们实际上没有被定义为模式的一部分,使用makeExecutableSchema构建模式将导致一个错误,就像使用任何其他未定义的指令一样。

您应该使用makeAugmentedSchema来代替:

代码语言:javascript
复制
const schema = makeAugmentedSchema({
  typeDefs,
  resolvers,
})
const driver = /* ... */
const server = new ApolloServer({
  context: { driver },
  schema,
})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58389693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档