Hello! I'm trying to wire up a User model with two GraphQL query fields.
The type:
const userType = new GraphQLObjectType(BookshelfType({
name: 'User',
description: 'A user',
fields: model => ({
id: globalIdField('User'), // Relay stuff
email: model.attr({
type: GraphQLString,
}),
role: model.attr({
type: GraphQLString,
}),
}),
interfaces: [nodeInterface], // Relay stuff
}));
The schema:
const queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
users: {
type: new GraphQLList(userType),
resolve: () => User.fetchAll(),
},
user: {
args: {
id: { type: GraphQLInt },
},
type: userType,
resolve: (_, args) => User.forge({ id: args.id }).fetch(),
},
}),
});
export default new GraphQLSchema({
query: queryType,
});
I can query one user just fine with { user(id: 1) { id, email, role } }
.
However, if I query the all users field { users { id, email, role } }
I get this error: User Error: expected iterable, but did not find one for field Query.users.
I guess it's complaining about type: new GraphlQLList(userType)
as return type? It's expecting an iterable but gets a Bookshelf Collection, right?
If I force it to return an array instead of a collection it works:
// I change
resolve: () => User.fetchAll()
// to:
resolve: () => User.fetchAll().then(users => users.reduce((arr, user) => { arr.push(user); return arr; }, [])),
What am I doing wrong here?