我正在尝试使用graphql将许多休息端点绑定在一起,而我仍然坚持如何过滤,排序和分页结果数据.具体来说,我需要通过嵌套值进行过滤和/或排序.
在所有情况下,我都无法对其余端点进行过滤,因为它们是具有单独数据库的独立微服务.(即我可以title在其余端点中过滤文章,但不能在author.name上过滤).同样排序.如果没有过滤和排序,也无法在其余端点上进行分页.
为了说明问题,并尝试解决方案,我formatResponse在apollo-server中使用了以下内容,但我想知道是否有更好的方法.
我已经将解决方案归结为我能想到的最小的文件集:
data.js表示2个虚构的休息端点返回的内容:
export const Authors = [{ id: 1, name: 'Sam' }, { id: 2, name: 'Pat' }];
export const Articles = [
{ id: 1, title: 'Aardvarks', author: 1 },
{ id: 2, title: 'Emus', author: 2 },
{ id: 3, title: 'Tapir', author: 1 },
]
Run Code Online (Sandbox Code Playgroud)
模式定义为:
import _ from 'lodash';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLInt,
} from 'graphql';
import {
Articles,
Authors,
} from './data';
const …Run Code Online (Sandbox Code Playgroud) 是否可以将查询输入中的字段标记为需要由不同服务扩展的字段?
使用联邦演示来说明我的问题,但保持更简单一点。只有一个帐户服务和一个评论服务,并向karma用户添加一个字段,是否可以根据用户业力过滤评论。
帐户服务,向用户添加 karma int:
extend type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String
username: String
karma: Int!
}
Run Code Online (Sandbox Code Playgroud)
评论服务,添加评论查询:
extend type Query {
reviews(minKarma: Int): [Review!]!
}
type Review @key(fields: "id") {
id: ID!
body: String
author: User @provides(fields: "username")
product: Product
}
extend type User @key(fields: "id") {
id: ID! @external
username: String @external
karma: Int! @external
reviews: [Review]
}
extend type Product @key(fields: "upc") {
upc: …Run Code Online (Sandbox Code Playgroud)