I want to make a GraphQL query that takes in some data from the data field of my Vue app. However, when I set up the variables hook and try to reference the object I need by using this.object, I get a cannot read 'xxx' of undefined.
apollo: {
question: {
query: gql `
query question($id: Int!) {
question(id: $id){
...
}
}
`, variables: { id: this.presentQuestionId}
}
},
Uncaught TypeError: Cannot read property 'presentQuestionId' of undefined is the error message that keeps on coming up.
variables option should be a function that returns an object like :
variables(){
return{
id: this.presentQuestionId
}
}
In pure JS I sometimes use this trick:
const opts = {foo: 1, bar: 2}
const q = gql`
query Query {
getSomething(
par1: ${opts.foo}
${!!opts.bar ? `, par2: ${opts.bar}` : ''}
) {
id
name
}
}
`
If par2 defined it adds to query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With