Reputation: 501
My GraphQL query is resolving docID and docTitle to null. Though the data is being retrieved from the DB. Only the _id field is being returned. When I console log I can see the data.
GraphQL query
query{
docs{
docId
docTitle
_id
}
}
My resolver
const getDocs = async (_parent, _args, context, _info) => {
const docs = await context.app.models.docs.find()
return docs
}
Docs schema
type Docs {
_id: ID!
docId: String
docTitle: String
}
On the GraphQL playground, this is what I get
{
"data": {
"getDocs": [
{
"docId": null,
"docTitle": null,
"_id": "5e7c9007f4c3"
}
]
}
}
But when I console log I am able to see data for all the fields
[
{
_id: 5e7c9007f4c3,
docId: 'c18c-57e4b5134cbe',
docTitle: 'cloud',
__v: 0
}
]
What am I missing please?
Upvotes: 1
Views: 538
Reputation: 501
Using the aggregate parameter also resolved the query correctly
const getDocs = async (_parent, _args, context, _info) => {
return await context.app.models.docs.aggregate([
{
$project: { docId:1, docTitle:1 },
},
])}
Upvotes: 0
Reputation: 2573
I'm guessing the types of docId
and docTitle
returned from the DB are not explicitly strings, consequently, some type checking is happening somewhere that causes their values to be stripped out of the output docs
object. Try adding the lean option to the query so that each document in the query result is transformed into a javascript object, that should help ensure the values docId
and docTitle
are cast down to strings.
Update your DB query to something like this:
await context.app.models.docs.find({}, {}, { lean: true })
Upvotes: 2