{
hero {
name
friends {
name
}
}
}



인스타그램 내 프로필을 생각해보면
이렇게 세 가지를 가져와야 한다.
REST API를 사용하면
이렇게 세 번의 요청이 필요할 것이다.
반면 GraphQL을 사용하면
query {
user(id: "fsdf23"){
name
status
posts{
title
body
}
friends{
name
}
}
}
이렇게 하나의 쿼리로 프로필 페이지에 필요한 정보를 가져올 수 있다.
GraphQL에는 Apollo Client, Relay, urql 등의 클라이언트 라이브러리가 있음
React Query를 사용했음useQuery같은 hook이 있어서npm install @apollo/client graphql
import { ApolloClient, InMemoryCache, ApolloProvider, gql } from '@apollo/client';
const client = new ApolloClient({
uri: 'https://flyby-gateway.herokuapp.com/',
cache: new InMemoryCache(),
});
// index.js
import React from 'react';
import * as ReactDOM from 'react-dom/client';
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
import App from './App';
const client = new ApolloClient({
uri: 'https://flyby-gateway.herokuapp.com/',
cache: new InMemoryCache(),
});
// Supported in React 18+
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
);
// App.js
// Import everything needed to use the `useQuery` hook
import { useQuery, gql } from '@apollo/client';
const GET_LOCATIONS = gql`
query GetLocations {
locations {
id
name
description
photo
}
}
`;
function DisplayLocations() {
const { loading, error, data } = useQuery(GET_LOCATIONS);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error : {error.message}</p>;
return data.locations.map(({ id, name, description, photo }) => (
<div key={id}>
<h3>{name}</h3>
<img width="400" height="250" alt="location-reference" src={`${photo}`} />
<br />
<b>About this location:</b>
<p>{description}</p>
<br />
</div>
));
}
export default function App() {
return (
<div>
<h2>My first Apollo app 🚀</h2>
<DisplayLocations />
</div>
);
}