获取其他相关记录(id 与查询的不同)

Get other related records (with id different that queried)

作为 GraphQL 的新手,我希望得到以下帮助:

我有一个检索其作者和该作者的书籍的查询。我希望作者的书是作者的 other 书,意思是 - 除了被查询的那本书。它涉及什么?

apollo-angular查询:

const getBookQuery = gql`
    query($id: ID){
        book(id: $id){
            id
            name
            year
            author {
                id
                firstName
                lastName
                books {        # <-- give me all _except_ the one with $id
                    name
                    year
                    id
                }
            }
        }
    }
`;

在 schema.js(node.js 服务器)我有类似的东西:

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        book: {
            type: BookType,
            args: { id: { type: GraphQLID } },
            resolve(parent, args) {
                const { id } = args;
                return Book.findById(id);
            },
        },
        books: {
            type: GraphQLList(BookType),
            resolve() {
                return Book.find({});
            },
        },
        // ... other queries ...
     }
})

显然,我正在寻找的解决方案应该不会破坏 books.

的其他查询

您应该能够通过向 Author 类型 def 添加一个参数,然后在书籍的解析器(应该是您的 Author 类型上的嵌套解析器)中适当地使用该参数来实现排除。需要调整 apollo-angular.

的语法
    type Author {
       id: 
       firstName: String
       lastName: String 
       books(exclude: ID): [Book]
     }

    const resolverMap = {
      Query: {
        book(arent, args, ctx, info) {
          ...
        }
     },
     Author: {
        books(obj, args, ctx, info) {
          // Use args.exclude passed to filter results
        },
      },
    };

   const getBookQuery = gql`
    query($id: ID){
        book(id: $id){
            id
            name
            year
            author {
                id
                firstName
                lastName
                books(exclude: $id) {        
                    name
                    year
                    id
                }
            }
        }
    }
`;