GraphQL流类型?

mpe*_*pen 5 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);
                });
            }),
        }
    })
});
Run Code Online (Sandbox Code Playgroud)

这很酷,但是我正在使用也允许我逐行流式传输结果

GraphQL是否有任何便利措施?

据我所知,GraphQLList期望有一个完整的数组,而且我只能解析一次结果集,不能使用Emitter或其他东西进行输入。

nha*_*nha 1

(This is not really a complete answer, but doesn\'t fit into a comment either)

\n\n

According to the graphQL spec. it should be possible :

\n\n
\n

GraphQL provides a number of built\xe2\x80\x90in scalars, but type systems can\n add additional scalars with semantic meaning. For example, a GraphQL\n system could define a scalar called Time which, while serialized as a\n string, promises to conform to ISO\xe2\x80\x908601. When querying a field of type\n Time, you can then rely on the ability to parse the result with an\n ISO\xe2\x80\x908601 parser and use a client\xe2\x80\x90specific primitive for time. Another\n example of a potentially useful custom scalar is Url, which serializes\n as a string, but is guaranteed by the server to be a valid URL.

\n
\n\n

So the structure should look like this :

\n\n
var myStream = new GraphQLScalarType({\n    name: "myStream",\n    serialize: ...\n    parseValue: ...\n    parseLiteral: ...\n  });\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

Now I guess you could try to return promises/streams and see what happens, but I don\'t think it is supported yet. Many people are asking for this, and I think I saw a comment from someone on the graphQL team saying that they are looking into this (cannot find it back unfortunately).

\n\n

个人观点,但如果不将该行为合并到 graphQL 的核心中就无法改变,则可能表明当今的 graphQL 并不那么灵活。

\n