{"id":7977,"date":"2024-06-27T11:55:31","date_gmt":"2024-06-27T06:25:31","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7977"},"modified":"2024-06-27T11:55:34","modified_gmt":"2024-06-27T06:25:34","slug":"integrating-graphql-api-using-apollo-client-in-react","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/integrating-graphql-api-using-apollo-client-in-react\/","title":{"rendered":"Integrating Graphql API using Apollo client in React"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Learn how to add a GraphQL API to a React app using Apollo Client and TypeScript. This makes handling data easier and more Powerful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Install Apollo Client<\/strong> <strong>and GraphQL<\/strong><br>yarn add @apollo\/client graphql<br>                    or<br>npm install @apollo\/client graphql<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Setup Apollo Client<\/strong><br>setting up Apollo Client to interact with a Graphql API. This setup is essential for connecting your React application to a GraphQL API using Apollo Client.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-7.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"802\" height=\"173\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-7.png\" alt=\"\" class=\"wp-image-7979\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-7.png 802w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-7-300x65.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-7-768x166.png 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Add ApolloProvider to the APP:<\/strong><br>This will make Apollo Client available to the whole app so we can use it to get data from your GraphQL API.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-8.png\"><img decoding=\"async\" width=\"562\" height=\"227\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-8.png\" alt=\"\" class=\"wp-image-7980\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-8.png 562w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-8-300x121.png 300w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><br><strong>Fetch Data with Apollo Client<\/strong><br>This component shows how to use Apollo Client to get data from a GraphQL API. It also manages showing a loading message while the data is loading and displays an error message if something goes wrong in a React app.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-9.png\"><img decoding=\"async\" width=\"757\" height=\"511\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-9.png\" alt=\"\" class=\"wp-image-7981\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-9.png 757w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-9-300x203.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2024\/06\/image-9-444x300.png 444w\" sizes=\"(max-width: 757px) 100vw, 757px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion:<\/strong><br>Learn how Apollo Client fetches data from a GraphQL API, handles loading states, and manages errors in a React application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to add a GraphQL API to a React app using Apollo Client and TypeScript. This makes handling data [&hellip;]<\/p>\n","protected":false},"author":501,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-7977","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-24F","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/501"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7977"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7977\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}