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 native firebase email verification 
Javascript :: how to update node in terminal 
Javascript :: acheck angular version 
Javascript :: let var diferencia 
Javascript :: jquery cheat sheet 
Javascript :: map function react 
Javascript :: react detect page width 
Javascript :: javascript detectar la pagina 
Javascript :: how to run a function infinite time in javascript 
Javascript :: Javascript - convert string value to lowercase 
Javascript :: document.getelementbyid 
Javascript :: javascript auto scroll horizontal 
Javascript :: logical operators in js 
Javascript :: jquery datepicker 
Javascript :: ndjson to json javascript 
Javascript :: textarea onclick select all 
Javascript :: How can I check if an object is an array 
Javascript :: mongoose make array required 
Javascript :: get milliseconds since epoch for 12am today javascript 
Javascript :: getting values for metaboxes in wordpress 
Javascript :: set body id js 
Javascript :: is knex built into node js 
Javascript :: difference between undefined and null javascript 
Javascript :: disabling ctrl + s using javascript 
Javascript :: Find duplicate or repeat elements in js array 
Javascript :: multiple styles in react native 
Javascript :: for:each in lwc js 
Javascript :: how to make page scroll to the top jsx 
Javascript :: leaflet tile service 
Javascript :: javascript string to array 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =