如何为各种对象执行graphql @relation指令

时间:2020-03-30 17:52:47

标签: neo4j graphql cypher undefined apollo-server

我有以下typeDefs:

const typeDefs = `
    type Movie {
        genres: String
        movieId: Int!
        title: String
        seenBy: [User] @cypher(statement: "with $this as m match (m)<-[:RATED]-(u:User) return u")
    }

    type User {
        userId: Int!
        name: String
        seen: [Movie] @relation(name: "RATED", direction: "OUT")
        recommended(first: Int = 5): [Movie] @cypher(statement: "with $this as u match (u)-->(:Movie)<--(:User)-->(reco:Movie) where not (u)-[:RATED]->(reco) return reco, count(*) as score order by score desc limit $first")
    }

    type Query {
        movieById(movieId: Int!): Movie,
        movieBySubstring(subString: String!): [Movie]
        userById(userId: Int!): User
        userBySubstring(subString: String!): [User]
    }
`;

问题出在Movie和User上带有指令的字段上(见过,见过和推荐过)。 当我执行movieById或userById查询并仅返回一个对象时,这些指令可以正常工作。

但是,当执行movieBySubstring或userBySubstring并返回对象数组时,出现错误

\“ Movie.seenBy \”的解析函数返回未定义

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

我找到了解决该问题的方法。我已经为movieBySubstring和userBySubstring查询实现了自定义解析器,这就是导致问题的原因。

解决方案是在查询定义中使用@cypher指令,如下所示:

    type Query {
        movieById(movieId: Int!): Movie
        moviesBySubstring(subString: String!): [Movie] @cypher (statement: "match (m:Movie) where m.title contains $subString return m")
        userById(userId: Int!): User
        usersBySubstring(subString: String!): [User] @cypher (statement: "match (u:User) where u.name contains $subString return u")
    }

这为我解决了问题,但是由于我没有使用cypher或neo4j的经验,所以我不知道这是否可能会遇到局限性。