Query definition in GraphQL and frequent errors with Apollo Client

Query definition in GraphQL is will be something you know now but not mastered. If you were going through the pages to troubleshoot frequent errors with Apollo Client which makes no sense, this post might help. First, the reason to create this post is,

Most of the error logs are not descriptive and makes no clue on where the real error is.

There can be so many different mistakes in coding the queries and difficulty in keeping on track of it.

And most importantly if I get into a trouble again, I can refer this 😉

Query mistakes when coding GraphQL

If you have seen following frequent errors with Apollo Client when trying to subscribe you can check it and validate whether these mistakes are with it.

1. Cannot read property ‘subscription’ of undefined

What can you understand by this error message? First impression you get is the subscription is not initialized. But this is not the case here. This happens when the query parameters are missing or not provided properly.

If your attributes in the variables sent with the client are not similar to the variables in the query, this error will be occurring. In this example its placeId. I really hope this error message will be changed in future releases.

2. Subscriptions are not registered and no events are received

This is a vague problem and it can occur due to several reasons.

The subscription query, syntax has errors.

These errors are mostly throws an error. Or does return false data. You can always code these subscriptions and check with AppSync queries whether its working or not. Follow the query syntax and code the subscription. Then test whether its working with the AppSync query browser.

If the subscriptions are activated when mutating the data, so will it with Apollo Client. So that will be the kind of 1st step of diagnosing subscriptions

The subscription fields are not defined in the mutation query

One of the common mistake is that you face when creating subscriptions is this one. The reason why you will loose your hair count is, it doesn’t have any clue! If you have a mutation and you dont want to receive any data, you will create a mutation query like this.