Skip to content

light9639/React-GraphQl-TypeScript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“Š React TypeScript๋ฅผ GraphQl์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“  ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

:octocat: ๋ฐ”๋กœ ๊ฐ€๊ธฐ : https://light9639.github.io/React-GraphQl-TypeScript/

light9639 github io_React-GraphQl-TypeScript_

โœจ ๐Ÿ“Š React TypeScript๋ฅผ GraphQl์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“  ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. โœจ

๐ŸŽ‰ React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

  • React ์ƒ์„ฑ
npm create-react-app my-app
# or
yarn create react-app my-app
  • vite๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด
npm create vite@latest
# or
yarn create vite
  • ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ ํ›„ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ๋งŒ๋“  ํ›„ React ์„ ํƒ, Typescirpt-SWC ์„ ํƒํ•˜๋ฉด ์ƒ์„ฑ ์™„๋ฃŒ.

๐Ÿ›ฐ๏ธ apollo-boost ๋ฐ react-apollo ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

  • apollo-boost ๋ฐ react-apollo ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ํ•˜๊ธฐ
$ npm install apollo-boost react-apollo
# or
$ yarn add apollo-boost react-apollo

โœ’๏ธ main.tsx, App.tsx ์ˆ˜์ • ๋ฐ ์ž‘์„ฑ

โšก main.tsx

  • ApolloProvider๋ฅผ import ํ•œ ํ›„ App ํŒŒ์ผ์„ ๊ฐ์‹ผ๋‹ค.
  • ApolloClient๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„ uri ๋ถ€๋ถ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ Graphql ๋งํฌ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'
import ApolloClient, { InMemoryCache } from "apollo-boost";
import { ApolloProvider } from "react-apollo";

const client = new ApolloClient({
  uri: "https://countries.trevorblades.com",
  cache: new InMemoryCache()
});

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
  <React.StrictMode>
    <ApolloProvider client={client}>
      <App />
    </ApolloProvider>
  </React.StrictMode>,
)

โšก App.tsx

  • Continents ์ปดํฌ๋„ŒํŠธ๋ฅผ import ํ•œ ํ›„ App ์ปดํฌ๋„ŒํŠธ ์•ˆ์— ๋„ฃ๋Š”๋‹ค.
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'
import Continents from "./components/Continents";

export default function App(): JSX.Element {
  return (
    <div className="App">
      <div>
        <a href="https://graphql.org/" target="_blank">
          <img src="https://camo.githubusercontent.com/07c382b68200c1a86d52d1682346e73e038b2f160c9afbc0af773fb3646882c8/68747470733a2f2f7777772e766563746f726c6f676f2e7a6f6e652f6c6f676f732f6772617068716c2f6772617068716c2d69636f6e2e737667" className="logo" alt="Vite logo" />
        </a>
        <a href="https://reactjs.org" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>GraphQl + React</h1>
      <Continents></Continents>
    </div>
  )
}

๐Ÿ“ components ํŒŒ์ผ ์† Continents.tsx ์ˆ˜์ • ๋ฐ ์ž‘์„ฑ

โšก Continents.tsx

  • gql ์•ˆ์— query๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์†์˜ ์ฝ”๋“œ๋ช…๊ณผ ์ด๋ฆ„์„ GET_CONTINENTS ๋ณ€์ˆ˜๋ช…์— ํ• ๋‹นํ•œ๋‹ค.
  • graphType์ด๋ผ๋Š” ํƒ€์ž…์„ ์„ค์ •ํ•œ ๋’ค ํƒ€์ž…์„ ์ง€์ •ํ•ด์ค€๋‹ค.
  • Query ์ปดํฌ๋„ŒํŠธ ์•ˆ์— query={GET_CONTINENTS}๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋ฐ‘์˜ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
  • ๋กœ๋”ฉ์ค‘์ผ ๋•Œ๋Š” <p>Loading...</p>, ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„ ๋•Œ๋Š” <p>Error!</p>, ์ •์ƒ์ ์ผ ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋ณต๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.
import React from "react";
import { ApolloError, gql } from "apollo-boost";
import { Query } from "react-apollo";

const GET_CONTINENTS = gql`
    query {
        continents {
            code
            name
        }
    }
`;

interface graphType {
    loading?: boolean;
    error?: ApolloError | undefined;
    data: any;
}

interface mapType {
    code: string;
    name: string
}

export default function Continents(): JSX.Element {
    return (
        <React.Fragment>
            <h2>Continents</h2>
            <Query query={GET_CONTINENTS}>
                {({ loading, error, data }: graphType) => {

                    if (loading) return <p>Loading...</p>;

                    if (error) return <p>Error!</p>;

                    return (
                        <React.Fragment>
                            {data.continents.map(({ code, name }: mapType) => (
                                <p key={code}>{name}</p>
                            ))}
                        </React.Fragment>
                    );
                }}
            </Query>
        </React.Fragment>
    );
}

๐Ÿ“Ž ์ถœ์ฒ˜

About

๐Ÿ“Š React TypeScript๋ฅผ GraphQl์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“  ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published