我正在玩弄 GraphQL-JS现在,将它连接到 MariaDB 后端。
我已经弄清楚如何返回整个结果集:
const queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
users: {
type: new GraphQLList(userType),
resolve: (root, args) => new Promise((resolve, reject) => {
db.query('select * from users', (err, rows, fields) => {
if(err) return reject(err);
resolve(rows);
});
}),
}
})
});
这很酷,但是 library I'm using也让我stream results , 逐行。
GraphQL 有什么可以促进这一点吗?
据我所知,GraphQLList
需要一个完整的数组,我只能解析我的结果集一次,而不是使用发射器或其他东西提供。
最佳答案
(这不是一个完整的答案,但也不适合评论)
根据 graphQL 规范。应该可以:
GraphQL provides a number of built‐in scalars, but type systems can add additional scalars with semantic meaning. For example, a GraphQL system could define a scalar called Time which, while serialized as a string, promises to conform to ISO‐8601. When querying a field of type Time, you can then rely on the ability to parse the result with an ISO‐8601 parser and use a client‐specific primitive for time. Another example of a potentially useful custom scalar is Url, which serializes as a string, but is guaranteed by the server to be a valid URL.
所以结构应该是这样的:
var myStream = new GraphQLScalarType({
name: "myStream",
serialize: ...
parseValue: ...
parseLiteral: ...
});
}
现在我猜你可以尝试返回 promises/streams 看看会发生什么,但我认为它还不被支持。很多人都在要求这个,我想我看到 graphQL 团队中有人的评论说他们正在调查这个(不幸的是找不到它)。
个人意见,但如果不将该行为合并到 graphQL 的核心中就无法更改,则可能表明 graphQL 目前的灵 active 并不那么灵活。
关于javascript - GraphQL 流类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160825/