Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL streaming type?

I'm playing around with GraphQL-JS right now, wiring it up to a MariaDB backend.

I've figured out how to return an entire result set:

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);
                });
            }),
        }
    })
});

Which is pretty cool, but the library I'm using also lets me stream results, row-by-row.

Does GraphQL have anything to facilitate this?

As far as I can tell, GraphQLList is expecting a full array, and I can only resolve my result set once, not feed using an Emitter or something.

like image 345
mpen Avatar asked Oct 19 '22 04:10

mpen


1 Answers

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

According to the graphQL spec. it should be possible :

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.

So the structure should look like this :

var myStream = new GraphQLScalarType({
    name: "myStream",
    serialize: ...
    parseValue: ...
    parseLiteral: ...
  });
}

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).

A personal opinion, but if it cannot be changed without incorporating that behaviour into the core of graphQL, may show that graphQL as it is today is not that flexible.

like image 176
nha Avatar answered Oct 27 '22 11:10

nha