I'm trying out Adonis and finding it pretty cool coming from a Laravel background; However, I am trying to set it up to work with graphQL which is proving somewhat difficult.
I've got the schema registered, and it running the queries, graphiql rendering out and sending across the queries.
My schema looks like this
'use strict'
const {
GraphQLObjectType,
GraphQLNonNull,
GraphQLSchema,
GraphQLString,
GraphQLList,
GraphQLInt,
GraphQLBoolean
} = require('graphql/type')
const User = use('App/Model/User')
const userType = new GraphQLObjectType({
name: 'user',
description: 'A User',
fields: () => ({
id: {
type: GraphQLInt,
description: 'The ID of the User'
},
username: {
type: GraphQLString,
description: 'The Username of the User'
},
email: {
type: GraphQLString,
description: 'The Email of the User'
}
})
})
const rootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
users: {
type: new GraphQLList(userType),
args: {},
resolve (root, args, source, fieldASTs) {
return User.all()
}
}
}
})
const schema = new GraphQLSchema({
query: rootQuery
})
module.exports = schema
For the remainder of this issue I am going to focus on this specific section.
resolve (root, args, source, fieldASTs) {
return User.all()
}
No matter what I try I can't seem to get it to work and it comes down to the generators.
If I use
resolve (root, args, source, fieldASTs) {
return [{id: 123, email: '[email protected]', 'username': 'username'}]
}
Then I get the data I would expect; However the moment I use Lucid depending on what I do I either get an empty array, or null (note that the database is populated, and a standard route with User.all() shows me the data).
Is there some way we can convert these "generator queries" into "promises"?
I imagine graphQL isn't the only library that isn't going to work nicely with generators.
Hey, I got it!
const genToPromise = require('co')
//...
resolve (root, args, source, fieldASTs) {
return genToPromise(function * () {
return yield User.all()
})
}
Hi @hailwood I have question how do you mount the graphql endpoint /graphql into the routes ? thanks.
Hi @pikitgb,
I'll push up my current working repository in about an hour and drop you a link here.
Hey @pikitgb,
This is what you'll be looking for.
Note this repo is heavily a WIP as I'm just playing around at this point for feasibility but it will give you the information you need to get going.
https://github.com/hailwood/adonis-graphql/blob/master/app/Http/routes.js
@hailwood thanks ! I will test this for sure. It looks amazing.
Most helpful comment
Hey @pikitgb,
This is what you'll be looking for.
Note this repo is heavily a WIP as I'm just playing around at this point for feasibility but it will give you the information you need to get going.
https://github.com/hailwood/adonis-graphql/blob/master/app/Http/routes.js