Have a strange problem...
Query to nested types return null.
But, if I return anything in parent type - resolve return right result
My code:
import { GraphQLList, GraphQLString, GraphQLID, GraphQLObjectType, GraphQLSchema } from 'graphql';
import AdminModel from '../models/Admin.model';
const AdminType = new GraphQLObjectType({
name: 'AdminType',
fields: {
_id: { type: GraphQLID },
login: { type: GraphQLString },
password: { type: GraphQLString }
}
});
const AdminRooteType = new GraphQLObjectType({
name: 'AdminRooteType',
fields: {
getAdmins: {
type: new GraphQLList(AdminType),
resolve() {
return AdminModel.find({})
}
}
}
})
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQuery',
fields: {
admin: {
type: AdminRooteType,
resolve() {
// EMPTY RESOLVE - EMPTY RESULT
}
}
}
})
});
Query:
{
admin {
getAdmins {
login
}
}
}
Result:
{
"data": {
"admin": null
}
}
If I changed returned value in fields admin in RootQuery:
import { GraphQLList, GraphQLString, GraphQLID, GraphQLObjectType, GraphQLSchema } from 'graphql';
import AdminModel from '../models/Admin.model';
const AdminType = new GraphQLObjectType({
name: 'AdminType',
fields: {
_id: { type: GraphQLID },
login: { type: GraphQLString },
password: { type: GraphQLString }
}
});
const AdminRooteType = new GraphQLObjectType({
name: 'AdminRooteType',
fields: {
getAdmins: {
type: new GraphQLList(AdminType),
resolve() {
return AdminModel.find({})
}
}
}
})
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQuery',
fields: {
admin: {
type: AdminRooteType,
#resolve() {#
#// RETURN ANYTHING HERE:#
# return 'foobar'#
}
}
}
})
});
I've got expected result:
{
"data": {
"admin": {
"getAdmins": [
{
"login": "123"
},
{
"login": "12asdf3"
}
]
}
}
}
What is right solution for this issue? (without using dummy values in return)
Thank's a lot!