javascript - GraphQL 流类型?

标签 javascript streaming graphql graphql-js

我正在玩弄 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/

相关文章:

javascript - d3.js 错误 data.map 不是函数

android - Android 的这个音频流播放代码有什么问题?

reactjs - 无法使用上下文提供程序对已卸载的组件执行 React 状态更新

javascript - 如何在 Apollo GraphQL 解析器上的一个函数中解析 2 个不同的方法?

javascript - 单个 View 上的多个 ng-repeat

javascript - 如何在类中的(作用域)addEventListener 上应用removeEventListener?

vb6 - VB6广播网络摄像头的解决方案

javascript - 如何处理流式 HTTP GET 数据?

Python GraphQL 如何声明自引用 Graphite 烯对象类型

javascript - 安装 MEAN 堆栈时出现问题