Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
Frequent errors with Apollo Client Apollo Client

Query definition in GraphQL and frequent errors with Apollo…

  • February 18, 2019March 11, 2019
  • by Coder Kai

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,

  1. Most of the error logs are not descriptive and makes no clue on where the real error is.
  2. 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 😉

Basics of Query Definition in GraphQL

The basic query has following structural reference in its queries.

query Operation($variable: Type! = DefaultVariable, $directive) {
  alias: listEvents(argument: $variable) {
    field1
    field2
    field3
    fieldList @include(if: $directive) {
      field4
    }
    ...fragmentFields
  }
}

fragment fragmentFields on Type {
  fragmentField1
  fragmentField2
}
  • fields:  Attributes which are returned as response from an object representation
  • arguments: Values which determines the conditions that the data is returned
  • aliases: Remap/Categorize the returned data on a provided condition
  • directive: Conditions to include or exclude fields
  • fragments: Get fragment of data from same type definition

Note that above is a simple representation and this can be supplemented with more complex or matured APIs. You can refer these at https://graphql.org/learn/queries/#directives

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. finally

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.

frequent Query errors when coding GraphQL

For example take this query.

const SubscribeToPosition = gql`subscription OnUpdatePosition($userId: String!) {
  onUpdatePosition(userId: $userId) {
    placeId
    position
    lastUpdated
  }
}`

To subscribe to the position updates we need to provide the userIdas a parameter when client is subscribing to the event.

apolloClient.hydrated().then((client) => {
      client.subscribe({ query: SubscribeToPosition, variables: {
        placeId,
      }})
        .subscribe({
          next: (data) => {
            dispatch(Actions.position.onUpdatePosition(data));
          },
          //complete: complete action,
          //error: error action,
        });
    });
});

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.

(bdw, checkout this blog if you need to setup GraphQL: https://sandny.com/2018/09/24/appsync-graphql-amplify-iam/)

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.

query Frequent errors with Apollo Client

 

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.

mutation CreateUser {
  createUser(input: {
    userId: "uuid-123",
    jobId: "uuid-jobId",
    firstName: "Tony",
    lastName: "Stark",
    lastUpdated: "74640371801",
  }) {
    userId
  }
}

subscription OnCreateUser($jobId: String!) {
  createUser(jobId: $jobId) {
    userId
    jobId
    firstName
    lastName
    lastUpdated
  }
}`;

This subscription wont be able to receive any data since the mutation doesn’t have the return fields. This is specific factor for AppSync

  • Not authorized or credential errors
  • Other Errors

Following will be some errors that you will face when implementing your code with AWS AppSync. I will update it frequently with the errors

  1. The provided key element does not match the schema:
    Not Providing the whole key structure to the query. Probably you have missed out the sort key.

 

 

(PS: I will be updating the list as I find errors)

OCaml Data Structures
How to setup FCM Push Notification with React Native App
Coder Kai
A humble developer

Related articles

Multiple refs for an array of React Elements
Using multiple refs for an…
Immutable and Mutable Values in Javascript
07. Immutable and Mutable Values…
wrapper objects in javascript
06. Wrapper objects in Javascript
globals undefined and null values in javascript
05 Global, null and undefined…
Javascript Booleans and Equality
04. Javascript Guide Booleans and…
How to add Chakra UI Animations
Chakra UI Animations
Copy files from S3 to PostgreSQL RDS
How to copy files from…
SSL Websocket using Nginx Proxy
SSL Websocket proxy with Nginx…
Change python version correctly
Python is not setting correct…
optimize React.js load time
How to optimize React.js app…
Multiple refs for an array of React Elements
How to use IntersectionObserver to…
Multiple refs for an array of React Elements
How to dismiss dropdowns when…
Javascript guide Strings
03. Javascript Guide – Strings
How to fetch data with useEffect
How to fetch data with…
add styles to stripe elements
How to add styles to…
Typescript
How to use Typescript with…
how to optimize react-native map view
How to optimize react-native map…
debounce with react hooks
Avoid multiple clicks using debounce…
Numbers inJavascript
02. Javascript Guide – Numbers
Introduction to Javascript
01. Javascript Guide – Introduction…

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • binance register on Chakra UI Animations
  • binance account on SSL Websocket proxy with Nginx for Graphql Subscriptions
  • Binance Pag-signup on How to fetch data with useEffect

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress