错误:未知指令"relation"。与 neo4j 的大堆栈



我正在尝试从neo4j开始的大堆栈启动器,并在完成所有graphql模式部分后使用API模块出现以下错误。 它抱怨指令"关系"和"密码"是未知的。 我重新安装了neo4j-graphql-js,但没有解决问题。 下面是错误消息

grand-stack-starter-api@0.0.1 start C:Usersgrand-stack-starter-masterapi
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:Usersgrand-stack-starter-masterapinode_modulesgraphqlvalidationvalidate.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:UsersN19683grand-stack-starter-masterapinode_modulesgraphqlvalidationvalidate.js:89:11)
at Object.buildASTSchema (C:UsersN19683grand-stack-starter-masterapinode_modulesgraphqlutilitiesbuildASTSchema.js:67:34)
at Object.buildSchemaFromTypeDefinitions (C:UsersN19683grand-stack-starter-masterapinode_modulesgraphql-toolssrcgeneratebuildSchemaFromTypeDefinitions.ts:43:32)
at makeExecutableSchema (C:UsersN19683grand-stack-starter-masterapinode_modulesgraphql-toolssrcmakeExecutableSchema.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:UsersN19683grand-stack-starter-masterapinode_modulesbabel-registerlibnode.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:UsersN19683grand-stack-starter-masterapinode_modulesbabel-registerlibnode.js:154:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
**

下面是 graphql 模式.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
}
};

索引.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}`);
});

任何人都可以帮助解决问题吗!提前致谢

如文档中所述

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

发生此错误是因为您尝试使用makeExecutableSchema创建架构。@relation@cypher指令由neo4j-graphql独家使用。由于它们实际上并未定义为架构的一部分,因此使用makeExecutableSchema构建架构将导致错误,就像使用任何其他未定义的指令一样。

您应该改用makeAugmentedSchema

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

最新更新