微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 GrandStack 中设置关系类型

如何解决在 GrandStack 中设置关系类型

我正在尝试在 Grandstack 中设置关系类型。我在让事情正确运行时遇到问题。即使我将指南复制到我的项目中并尝试运行它......事情也不起作用。这是他们在 https://grandstack.io/docs/guide-graphql-schema-design

上的东西
type Actor {
    actorId: ID!
    name: String
    movies: [Movie] @relation(name: "ACTED_IN",direction: OUT)
}

type Movie {
    movieId: ID!
    title: String
    description: String
    year: Int
    actors(limit: Int = 10): [Actor] @relation(name: "ACTED_IN",direction: IN)
    similarMovies(limit: Int = 10): [Movie] @cypher(statement: """
        MATCH (this)<-[:ACTED_IN]-(:Actor)-[:ACTED_IN]->(rec:Movie)
        WITH rec,COUNT(*) AS score ORDER BY score DESC
        RETURN rec LIMIT $limit
    """)
}

type User {
    userId: ID!
    name: String
    rated: [Rated]
}

type Rated @relation(name: "RATED") {
    from: User
    to: Movie
    rating: Float
    review: String
}

问题是当我运行它时出现以下错误

ApolloError: The 'from' argument of the @relation directive on the _UserRated type is "User",but a "User" field is not defined.
    at validateRelationTypeNodeField (/Users/kennyv/git/skoutal-server/node_modules/neo4j-graphql-js/dist/augment/types/relationship/relationship.js:221:13)
    at validateRelationTypeDirective (/Users/kennyv/git/skoutal-server/node_modules/neo4j-graphql-js/dist/augment/types/relationship/relationship.js:183:5)

不知道为什么它认为 User 字段未定义。 这是我的 server.js 文件

require("dotenv").config();
const { ApolloServer,PubSub } = require("apollo-server");
const jwt = require("jsonwebtoken");
const neo4j = require("neo4j-driver");
const { augmentSchema,makeAugmentedSchema } = require("neo4j-graphql-js");
const typeDefs = require("./graphql/typeDefs.js");
const resolvers = require("./graphql/resolvers");

require("console-stamp")(console,{ pattern: "mm/dd/yyyy HH:MM:ss.l" });
// Create an instance of Express

const pubsub = new PubSub();
const PORT = process.env.PORT || 5000;
const schema = makeAugmentedSchema({
  typeDefs,resolvers,// config: {
  //   query: true,//   mutation: true,// },});
const augmentedSchema = augmentSchema(schema);

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

const server = new ApolloServer({
  schema: schema,context: ({ req }) => {
    const token = req?.headers?.authorization?.slice(7);
    let userId;
    if (token) {
      const decoded = jwt.verify(token,process.env.APP_SECRET);
      userId = decoded.id;
    }
    return {
      cypherParams: { userId },driver,neo4jDatabase: process.env.NEO4J_DATABASE,req,pubsub,};
  },});
"apollo-server": "^2.20.0","graphql": "^15.5.0","neo4j-driver": "^4.2.2","neo4j-graphql-js": "^2.19.2",

解决方法

我不小心跑了

const augmentedSchema = augmentSchema(schema);

在我已经运行过的架构上

makeAugmentedSchema({
  typeDefs,resolvers,// config: {
  //   query: true,//   mutation: true,// },});

这是远离 makeExecutableSchema 的产物,我不得不将其删除,因为它会导致问题。

即:

Error: Unknown directive "@relation".

Unknown directive "@relation".

Unknown directive "@cypher".

Unknown directive "@relation".

Unknown directive "@relation".

Unknown directive "@relation".

所以当我注释掉增加模式时,事情开始正常了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。