Web

How to Setup Apollo Graphql Client with Angular

Apollo Client is a GraphQL client for Angular, JavaScript, and native platforms such as some of the following. Apollo client for Angular is used to build Angular UI components that fetch data with GraphQL.

  • Javascript
    • React
    • VueJS
    • Meteor
    • Ember
    • Polymer
  • Native platforms
    • Native iOS with Swift
    • Native Android with Java

In this post, you will learn about setting up Apollo GraphQL client for using it in your Angular App.

Instructions on setting up Apollo GraphQL Client with Angular App

The instructions given below is worked out with the Angular 4.* app. However, this should also apply to the Angular 5 and later angular versions’ app. As per instructions on this page, setting up Apollo with Angular, the following command needs to be executed NPM packages in relation to Apollo Angular client:

npm install apollo-angular apollo-angular-link-http apollo-client apollo-cache-inmemory graphql-tag graphql --save

However, as you start your angular app using command such as ng serve –open, you may run through error such as Could not find a declaration file for module ‘graphql’.Try npm install @types/graphql if it exists or add a new declaration (.d.ts) file containing `declare module ‘graphql’. The following is the screenshot:

Figure 1. apollo client error – could not find a declaration file for module graphql

Solution: In order to fix the above error, you need to execute the following command for installing @types/graphql.

npm install --save @types/graphql

However, as you start your angular app with ng serve –open, you may run through error such as Cannot find name ‘AsyncIterator’ and/or Cannot find name ‘AsyncIterable’. The following is the screenshot:

Figure 2. Apollo Client Angular Error – Cannot find name AsyncIterator

Solution: In order to fix the above issue, you need to include “esnext” as one of the additional compilerOptions/lib options in src/tsconfig.json. The following is how your src/tsconfig.json file would look like:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom", "esnext"],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

Further Reading / References

Summary

In this post, you learned about how to setup/install Apollo GraphQL client for using it in your Angular app.

Did you find this article useful? Do you have any questions about this article or suggestions regarding setting up or installing Apollo Graphql client for the Angular app? Leave a comment and ask your questions and I shall do my best to address your queries.

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar

Recent Posts

Autoencoder vs Variational Autoencoder (VAE): Differences

Last updated: 08th May, 2024 In the world of generative AI models, autoencoders (AE) and…

1 day ago

Linear Regression T-test: Formula, Example

Last updated: 7th May, 2024 Linear regression is a popular statistical method used to model…

2 days ago

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

6 days ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

7 days ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

1 week ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

1 week ago