Skip to content
Go back
🔬 스터디

React Typescript Setup Tutorial 1. Apollo 세팅하기 (작성 중..)

핵심 요약

프론트엔드 프로젝트를 위해 자주 사용하는 기본 개발 환경 구축 Typescript, Apollo, Styled Components 를 이용한 React 튜토리얼 기본 개발 환경 구축 s create react app sample client typescript ````s yarn add...

기본 개발 환경 구축

create-react-app sample-client --typescript
yarn add apollo-boost graphql react-apollo
// apollo.ts

import ApolloClient, { Operation } from "apollo-boost";

const client = new ApolloClient({
  uri: "http://localhost:4000/graphql",
});

export default client;
// index.tsx

import React from 'react'
import { ApolloProvider } from 'react-apollo'
import ReactDOM from 'react-dom'
import client from './apollo'

import App from './App'

ReactDOM.render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById('root')
)
import ApolloClient, { Operation } from "apollo-boost";

const client = new ApolloClient({
  clientState: {
    defaults: {
      auth: {
        __typename: "Auth",
        isLoggedIn: Boolean(localStorage.getItem("authToken")),
      },
    },
    resolvers: {
      Mutation: {
        logUserIn: (_, { token }, { cache }) => {
          localStorage.setItem("authToken", token);
          cache.writeData({
            data: {
              auth: {
                __typename: "Auth",
                isLoggedIn: true,
              },
            },
          });
          return null;
        },
        logUserOut: (_, __, { cache }) => {
          localStorage.removeItem("authToken");
          cache.writeData({
            data: {
              auth: {
                __typename: "Auth",
                isLoggedIn: false,
              },
            },
          });
          return null;
        },
      },
    },
  },
  request: async (operation: Operation) => {
    operation.setContext({
      headers: {
        "X-User-Token": localStorage.getItem("authToken") || "",
      },
    });
  },
  uri: "http://localhost:4000/graphql",
});

export default client;

Reference

[[ 2. Styled Components 세팅하기]]

Tony Cho profile image

About the author

Tony Cho

Indie Hacker, Product Engineer, and Writer

제품을 만들고 회고를 남기는 개발자. AI 코딩, 에이전트 워크플로우, 스타트업 제품 개발, 팀 빌딩과 리더십에 대해 쓴다.


Share this post on:

댓글


Previous Post
린 고객 개발을 위한 고객 인터뷰 기본 질문 5가지
Next Post
비전공 통계학 입문자들을 위한 스터디 교재 & 교양 서적 추천