2 years ago
#61944
john
GraphQL { "message": "booking is not defined", "locations": [ { "line": 6, "column": 21 } ], "path": [ "bookings", 0, "event" ] }
I am fairly new to GraphQL, and I am having trouble reading the errors.
#1. I don't know how to locate this error ( find the line and column).
#2. "booking is not defined" I am having a hard time finding what causes the trouble.
This is the query from front end:
//this is the query from the client
fetchedBookedEventQuery : {
query:`
query {
bookings {
_id
createdAt
event {
_id
title
date
}
}
}
`
},
back end structure:
// Booking Resolver
bookings:async (args, req)=>{
//adds protected route
if(!req.isAuth){ throw new Error('Not authenticated!') }
console.log('you are fetching bookings')
try{
const bookings = await Booking.find()
const newRes = bookings.map(each => transformBooking(each) )
console.log('bookings:', newRes)
return newRes
}catch(err) {
console.log('Err! ****************', err)
throw err}
},
// merge data resolver
// function to populate the booking
const transformBooking = booking =>{
const obj1 = booking
// I replace booking with obj1, in eforts to troubleshoot
return{
...booking._doc,
_id: booking.id,
user: user.bind(this, obj1.user),
event: singleEvent.bind(this, obj1.event),
createAt:booking._doc.createAt,
updatedAt:booking._doc.updatedAt,
}
}
Response on backend:
{}
{
query: '\n' +
' query {\n' +
' bookings {\n' +
' _id\n' +
' createdAt\n' +
' event {\n' +
' _id\n' +
' title\n' +
' date\n' +
' }\n' +
' }\n' +
' }\n' +
' '
}
you are fetching bookings
bookings: [
{
_id: '61e21bcfed8572f550c31dfb',
event: [Function: bound singleEvent] AsyncFunction,
user: [Function: bound user] AsyncFunction,
createdAt: 2022-01-15T00:56:47.582Z,
updatedAt: 2022-01-15T00:56:47.582Z,
__v: 0,
createAt: undefined
},
{
_id: '61e21beded8572f550c31dfe',
event: [Function: bound singleEvent] AsyncFunction,
user: [Function: bound user] AsyncFunction,
createdAt: 2022-01-15T00:57:17.684Z,
updatedAt: 2022-01-15T00:57:17.684Z,
__v: 0,
createAt: undefined
}
]
No error!
response on front end / or http://localhost:5000/graphql:
{
"errors": [
{
"message": "booking is not defined",
"locations": [
{
"line": 5,
"column": 21
}
],
"path": [
"bookings",
0,
"event"
]
}
],
"data": null
}
javascript
mongodb
graphql
bind
resolver
0 Answers
Your Answer