Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to use graphql api in react

import React from "react";
import { request, gql } from "graphql-request";
import { useQuery } from "react-query";

const endpoint = "https://api.spacex.land/graphql/";
const FILMS_QUERY = gql`
  {
    launchesPast(limit: 10) {
      id
      mission_name
    }
  }
`;

export default function App() {
  const { data, isLoading, error } = useQuery("launches", () => {
    return request(endpoint, FILMS_QUERY);
  });

  if (isLoading) return "Loading...";
  if (error) return <pre>{error.message}</pre>;

  return (
    <div>
      <h1>SpaceX Launches</h1>
      <ul>
        {data.launchesPast.map((launch) => (
          <li key={launch.id}>{launch.mission_name}</li>
        ))}
      </ul>
    </div>
  );
}
Comment

react js and graphql integration

1
2
3
npx create-react-app my-graphql-react-project
cd my-graphql-react-project
yarn add @apollo/client graphql
Comment

PREVIOUS NEXT
Code Example
Javascript :: react select options 
Javascript :: build react app 
Javascript :: last array 
Javascript :: if and else shorthand 
Javascript :: convert javascript to ruby 
Javascript :: string properties in javascript 
Javascript :: web scrape example js 
Javascript :: angular material button color 
Javascript :: save text of div to localStorage, update localStorage when text is changed 
Javascript :: axios get array of urls 
Javascript :: jsdoc object destructuring 
Javascript :: delete method 
Javascript :: visual studio node.js mac 
Javascript :: basics of switch case and if else 
Javascript :: dayjs dayofyear 
Javascript :: promises chaining 
Javascript :: what are built in objects in javascript 
Javascript :: if without else javascript 
Javascript :: invertir un array javascript 
Javascript :: make image onclick in vuejs 
Javascript :: react form validation 
Javascript :: JavaScript Extract Values 
Javascript :: javascript settimeout lambda function 
Javascript :: check variable value and do something 
Javascript :: react native border radius not working ios 
Javascript :: javascript log where function was called 
Javascript :: react table with styles 
Javascript :: javascript symbols 
Javascript :: discord.js check every x minutes 
Javascript :: script refresh js 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =