A reference implementation of GraphQL for JavaScript
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.
Source: (StackOverflow)
I am trying out Relay and GraphQL. When I am doing the schema I am doing this:
let articleQLO = new GraphQLObjectType({
name: 'Article',
description: 'An article',
fields: () => ({
_id: globalIdField('Article'),
title: {
type: GraphQLString,
description: 'The title of the article',
resolve: (article) => article.getTitle(),
},
author: {
type: userConnection,
description: 'The author of the article',
resolve: (article) => article.getAuthor(),
},
}),
interfaces: [nodeInterface],
})
So, when I ask for an article like this:
{
article(id: 1) {
id,
title,
author
}
}
Will it do 3 queries to the database? I mean, each field has a resolve method (getTitle, getAuthor...) which does a request to the database. Am I doing this wrong?
This is an example of getAuthor (I use mongoose):
articleSchema.methods.getAuthor = function(id){
let article = this.model('Article').findOne({_id: id})
return article.author
}
Source: (StackOverflow)