{"id":8599,"date":"2023-08-29T04:51:52","date_gmt":"2023-08-29T04:51:52","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8599"},"modified":"2023-08-29T04:51:52","modified_gmt":"2023-08-29T04:51:52","slug":"apollo-graphql-queries-in-react-apps","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/apollo-graphql-queries-in-react-apps","title":{"rendered":"Apollo GraphQL Queries in React Apps"},"content":{"rendered":"<p>Apollo in React provides <mark>useQuery<\/mark> to fetch GraphQL data and <mark>useMutation<\/mark> to execute mutations.<\/p>\n<p>Following is the example of useQuery and useMutation hooks:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import { gql, useQuery, useMutation } from \"@apollo\/client\";\r\n\r\nconst GET_CARS = gql`\r\n  query GetCards {\r\n    cars {\r\n      id\r\n      brand\r\n    }\r\n  }\r\n`;\r\n\r\nconst ADD_CAR = gql`\r\n  mutation AddCar($brand: String!) {\r\n    addCar(brand: $brand) {\r\n      id\r\n      brand\r\n    }\r\n  }\r\n`;\r\n\r\nfunction Cars({ onCarSelected }) {\r\n  const { loading, error, data } = useQuery(GET_CARS);\r\n  const [addCar, otherDetails] = useMutation(ADD_CAR);\r\n  const inputRef = useRef();\r\n\r\n  if (loading) return \"Loading...\";\r\n  if (error) return `Error! ${error.message}`;\r\n\r\n  return (\r\n    &lt;div&gt;\r\n      &lt;select name=\"car\" onChange={onCarSelected}&gt;\r\n        {data.cars.map(({ id, brand }) =&gt; (\r\n          &lt;option key={id} value={brand}&gt;\r\n            {brand}\r\n          &lt;\/option&gt;\r\n        ))}\r\n      &lt;\/select&gt;\r\n\r\n      &lt;form\r\n        onSubmit={(e) =&gt; {\r\n          e.preventDefault();\r\n          addCar({ variables: { brand: inputRef.current.value } });\r\n          \/\/ post details related to mutation like { data, loading, error } can be accessed from otherDetails\r\n        }}\r\n      &gt;\r\n        &lt;input ref={inputRef} \/&gt;\r\n        &lt;button type=\"submit\"&gt;Add Car&lt;\/button&gt;\r\n      &lt;\/form&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Apollo in React provides useQuery to fetch GraphQL data and useMutation to execute mutations. Following is the example of useQuery and useMutation hooks: import { gql, useQuery, useMutation } from &#8220;@apollo\/client&#8221;; const GET_CARS = gql` query GetCards { cars { id brand } } `; const ADD_CAR = gql` mutation AddCar($brand: String!) { addCar(brand: $brand) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8599","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8599"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8599"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8599\/revisions"}],"predecessor-version":[{"id":8603,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8599\/revisions\/8603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8600"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}