Fragments

Relay์—์„œ React ๊ตฌ์„ฑ ์š”์†Œ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์ข…์†์„ฑ์„ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•œ ์ฃผ์š” ๋นŒ๋”ฉ ๋ธ”๋ก์€ GraphQL ํ”„๋ž˜๊ทธ๋จผํŠธ์ž…๋‹ˆ๋‹ค. Fragment ๋Š” GraphQL ์Šคํ‚ค๋งˆ์— ์ œ๊ณต๋œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ units ์ž…๋‹ˆ๋‹ค.

์‹ค์ œ๋กœ๋Š” GraphQL์˜ ํ•„๋“œ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค.

fragment UserFragment on User {
  name
  age
  profile_picture(scale: 2) {
    uri
  }
}

JavaScript ์ฝ”๋“œ ๋‚ด์—์„œ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์„ ์–ธํ•˜๋ ค๋ฉด ๋‹ค์Œ graphql ํƒœ๊ทธ ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค .

const {graphql} = require('react-relay');

const userFragment = graphql`
  fragment UserFragment_user on User {
    name
    age
    profile_picture(scale: 2) {
      uri
    }
  }
`;

Rendering ํ”„๋ž˜๊ทธ๋จผํŠธ

ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ Œ๋”๋งํ•˜๊ธฐ ์œ„ํ•ด useFragmentHook ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

import type {UserComponent_user$key} from 'UserComponent_user.graphql';

const React = require('React');

const {graphql, useFragment} = require('react-relay');

type Props = {
  user: UserComponent_user$key,
};

function UserComponent(props: Props) {
  const data = useFragment(
    graphql`
      fragment UserComponent_user on User {
        name
        profile_picture(scale: 2) {
          uri
        }
      }
    `,
    props.user,
  );

  return (
    <>
      <h1>{data.name}</h1>
      <div>
        <img src={data.profile_picture?.uri} />
      </div>
    </>
  );
}

module.exports = UserComponent;
  • useFragmentํ”„๋ž˜๊ทธ๋จผํŠธ ์ •์˜์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ ๋ฅผ ์ทจํ•˜๊ณ  ํ•ด๋‹น ํ”„๋ž˜๊ทธ๋จผํŠธ ๋ฐ ์ฐธ์กฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • Fragment ์ฐธ์กฐ ๋Š” Relay๊ฐ€ Fragment ์ •์˜์— ์„ ์–ธ๋œ ๋ฐ์ดํ„ฐ ๋ฅผ ์ฝ๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

    • ๋ณด์‹œ ๋‹ค์‹œํ”ผ UserComponent_user ํ”„๋ž˜๊ทธ๋จผํŠธ ์ž์ฒด๋Š” ์œ ํ˜•์˜ ํ•„๋“œ๋ฅผ ์„ ์–ธ ํ•˜์ง€๋งŒ ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ฝ์„ ํŠน์ • ์‚ฌ์šฉ์ž๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, Fragment ์ฐธ์กฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์œผ๋ ค๋Š” ์œ ํ˜•์˜ ํŠน์ • ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    • ์ปดํฌ๋„ŒํŠธ๋Š” ์ž๋™์œผ๋กœ ์กฐ๊ฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ตฌ๋…ํ•ฉ๋‹ˆ๋‹ค.

    • ์ด ํŠน์ • ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ User๊ฐ€ ์•ฑ์˜ ์–ด๋””์—์„œ๋‚˜ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ๋˜๋ฉด(์˜ˆ: ์ƒˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ๋˜๋Š” ๊ธฐ์กด ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์„ ํ†ตํ•ด) ๊ตฌ์„ฑ ์š”์†Œ๋Š” ์ž๋™์œผ๋กœ ์ตœ์‹  ์—…๋ฐ์ดํŠธ๋กœ ๋‹ค์‹œ ๋ Œ๋”๋ง๋ฉ๋‹ˆ๋‹ค.

  • Relay๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์„ ์–ธ๋œ ๋ชจ๋“  ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋Œ€ํ•œ type์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ์œ ํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ props ์˜ ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • ์ƒ์„ฑ๋œ Flow ์œ ํ˜•์—๋Š” ์ ‘๋ฏธ์‚ฌ๊ฐ€ ์žˆ๋Š” ์œ ํ˜•์ธ ์กฐ๊ฐ ์ฐธ์กฐ ์œ ํ˜•๊ณผ $key์ ‘๋ฏธ์‚ฌ <fragment_name>$key๊ฐ€ ์žˆ๋Š” ์œ ํ˜•์ธ ๋ฐ์ดํ„ฐ ๋ชจ์–‘ ์œ ํ˜•์ด ํฌํ•จ $data๋ฉ๋‹ˆ๋‹ค <fragment_name>$data. ์ด๋Ÿฌํ•œ ์œ ํ˜•์€ ๋‹ค์Œ ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑ๋œ ํŒŒ์ผ์—์„œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค <fragment_name>.graphql.js. .

    • ์šฐ๋ฆฌ๋Š” ์œ„์˜ ์˜ˆ์—์„œ user Props ๊ฐ€ ํ•„์š”ํ•œ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ๋กœ ์ž…๋ ฅ๋˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด๋Š” UserComponent_user.graphql ์—์„œ ๊ฐ€์ ธ์˜จ UserComponent_user$key ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค.

  • ํ”„๋ž˜๊ทธ๋จผํŠธ ์ด๋ฆ„์€ ์ „์—ญ์ ์œผ๋กœ ๊ณ ์œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‰ฝ๊ฒŒ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“ˆ ์ด๋ฆ„๊ณผ ์‹๋ณ„์ž๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์Œ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ Fragment์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค <module_name>_<property_name>. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์–ด๋–ค ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์–ด๋–ค ๋ชจ๋“ˆ์— ์ •์˜๋˜์–ด ์žˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋™์ผํ•œ ๋ชจ๋“ˆ์— ์—ฌ๋Ÿฌ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ •์˜๋  ๋•Œ ์ด๋ฆ„ ์ถฉ๋Œ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋™์ผํ•œ ์ปดํฌ๋„ŒํŠธ ๋‚ด์˜ ์—ฌ๋Ÿฌ Fragment์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ Œ๋”๋งํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ useFragment์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import type {UserComponent_user$key} from 'UserComponent_user.graphql';
import type {UserComponent_viewer$key} from 'UserComponent_viewer.graphql';

const React = require('React');
const {graphql, useFragment} = require('react-relay');

type Props = {
  user: UserComponent_user$key,
  viewer: UserComponent_viewer$key,
};

function UserComponent(props: Props) {
  const userData = useFragment(
    graphql`
      fragment UserComponent_user on User {
        name
        profile_picture(scale: 2) {
          uri
        }
      }
    `,
    props.user,
  );

  const viewerData = useFragment(
    graphql`
      fragment UserComponent_viewer on Viewer {
        actor {
          name
        }
      }
    `,
    props.viewer,
  );

  return (
    <>
      <h1>{userData.name}</h1>
      <div>
        <img src={userData.profile_picture?.uri} />
        Acting as: {viewerData.actor?.name ?? 'Unknown'}
      </div>
    </>
  );
}

module.exports = UserComponent;

Composing ํ”„๋ž˜๊ทธ๋จผํŠธ

GraphQL์—์„œ ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋‹ค๋ฅธ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ๋‹ค๋ฅธ ํ”„๋ž˜๊ทธ๋จผํŠธ ๋˜๋Š” ์ฟผ๋ฆฌ๋‚ด์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

fragment UserFragment on User {
  name
  age
  profile_picture(scale: 2) {
    uri
  }
  ...AnotherUserFragment
}

fragment AnotherUserFragment on User {
  username
  ...FooUserFragment
}

๊ฐ React ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹์˜ ๋ฐ์ดํ„ฐ ์ข…์†์„ฑ์„ ๊ฐ€์ ธ์˜ฌ ์ฑ…์ž„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ Œ๋”๋งํ•˜๋ ค๋ฉด ์ž์‹์˜ props์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ด ํŒจํ„ด์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ปดํฌ๋„ŒํŠธ(ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ, ๋ Œ๋”๋งํ•˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ)์— ๋Œ€ํ•ด ๋กœ์ปฌ๋กœ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ Relay๋Š” ์ „์ฒด UI ํŠธ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ ์ข…์†์„ฑ์— ๋Œ€ํ•œ ์ „์—ญ ๋ณด๊ธฐ๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

  • Co-location ๊ณผ ๊ด€๋ จ์žˆ์Šต๋‹ˆ๋‹ค.

/**
 * UsernameSection.react.js
 *
 * Child Fragment Component
 */

import type {UsernameSection_user$key} from 'UsernameSection_user.graphql';

const React = require('React');
const {graphql, useFragment} = require('react-relay');

type Props = {
  user: UsernameSection_user$key,
};

function UsernameSection(props: Props) {
  const data = useFragment(
    graphql`
      fragment UsernameSection_user on User {
        username
      }
    `,
    props.user,
  );

  return <div>{data.username ?? 'Unknown'}</div>;
}

module.exports = UsernameSection;
/**
 * UserComponent.react.js
 *
 * Parent Fragment Component
 */

import type {UserComponent_user$key} from 'UserComponent_user.graphql';

const React = require('React');
const {graphql, useFragment} = require('react-relay');

const UsernameSection = require('./UsernameSection.react');

type Props = {
  user: UserComponent_user$key,
};

function UserComponent(props: Props) {
  const user = useFragment(
    graphql`
      fragment UserComponent_user on User {
        name
        age
        profile_picture(scale: 2) {
          uri
        }

        # Include child fragment:
        ...UsernameSection_user
      }
    `,
    props.user,
  );

  return (
    <>
      <h1>{user.name}</h1>
      <div>
        <img src={user.profile_picture?.uri} />
        {user.age}

        {/* Render child component, passing the _fragment reference_: */}
        <UsernameSection user={user} />
      </div>
    </>
  );
}

module.exports = UserComponent;

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์‚ฌํ•ญ์ด ๋ช‡ ๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • UsernameSection๋Š” user prop์œผ๋กœ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ๋Š” Relay๊ฐ€ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ •์˜์— ์„ ์–ธ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋ณด์‹œ๋‹ค์‹œํ”ผ ์ž์‹ UsernameSection_user ํ”„๋ž˜๊ทธ๋จผํŠธ ์ž์ฒด๋Š” ์œ ํ˜•์˜ ํ•„๋“œ๋ฅผ ์„ ์–ธ ํ•˜์ง€๋งŒ ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ฝ์„ ํŠน์ • User๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

    • ์ฆ‰, ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์œผ๋ ค๋Š” ์œ ํ˜•์˜ ํŠน์ • ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์ด ๊ฒฝ์šฐ UsernameSection์— ์ „๋‹ฌ๋œ User, ์ฆ‰ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ์—๋Š” ์‹ค์ œ๋กœ ์ž์‹ UsernameSection ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ์„ ์–ธํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • ๋Œ€์‹  UsernameSection์€ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ useFragment๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด๋ถ€์ ์œผ๋กœ ์„ ์–ธ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.

  • ์ด๊ฒƒ์€ ๋ถ€๋ชจ๊ฐ€ ์ž์‹์ด ์„ ์–ธํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ข…์†์„ฑ์„ ์•”์‹œ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ณ  ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋ฏ€๋กœ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์— ์˜ํ–ฅ์„ ๋ฏธ์น  ์—ผ๋ ค ์—†์ด ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•ด ๋กœ์ปฌ๋กœ ์ถ”๋ก ํ•˜๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ๋ถ€๋ชจ๊ฐ€ ์ž์‹์˜ ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ž์‹์ด ์„ ์–ธํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋ถ€๋ชจ๊ฐ€ ์†์ƒ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐ์ดํ„ฐ ๋งˆ์Šคํ‚น์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  • ์ž์‹์ด ์˜ˆ์ƒํ•˜๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ๋Š” ์ž์‹ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ถ€๋ชจ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ฝ์€ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ์œ„์˜ ์˜ˆ์—์„œ ์ด๋Š” ...UsernameSection_user๋ฅผ ํฌํ•จํ•˜๋Š” ๋‹จํŽธ์„ ์ฝ์€ ๊ฒฐ๊ณผ๊ฐ€ UsernameSection์ด ์˜ˆ์ƒํ•˜๋Š” ๋‹จํŽธ ์ฐธ์กฐ๊ฐ€ ๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, useFragment๋ฅผ ํ†ตํ•ด ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ฝ์€ ๊ฒฐ๊ณผ ์–ป์€ ๋ฐ์ดํ„ฐ๋Š” ํ•ด๋‹น ํ”„๋ž˜๊ทธ๋จผํŠธ์— ํฌํ•จ๋œ ๋ชจ๋“  ์ž์‹ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋Œ€ํ•œ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ ์—ญํ• ๋„ ํ•ฉ๋‹ˆ๋‹ค.

Composing ํ”„๋ž˜๊ทธ๋จผํŠธ into Queries

Relay์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์ข…์†์„ฑ์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ž์ฒด์ ์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ์ฟผ๋ฆฌ์— ์ง์ ‘ ๋˜๋Š” ๊ฐ„์ ‘์ ์œผ๋กœ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋ชจ๋“  ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ๋ Œ๋”๋ง๋  ๋•Œ ์ฟผ๋ฆฌ์— ์†ํ•ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค . ๋‹จ์ผ ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ์—ฌ์ „ํžˆ ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ํ”„๋ž˜๊ทธ๋จผํŠธ ๊ตฌ์„ฑ ์š”์†Œ์˜ ํŠน์ • ์ธ์Šคํ„ด์Šค๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ ํŠน์ • ์ฟผ๋ฆฌ ์š”์ฒญ์˜ ์ผ๋ถ€๋กœ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ํฌํ•จ๋œ ์ฟผ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๋ Œ๋”๋งํ•˜๋ ค๋ฉด ํ”„๋ž˜๊ทธ๋จผํŠธ ๊ตฌ์„ฑ ์„น์…˜ ์— ํ‘œ์‹œ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค

/**
 * UserComponent.react.js
 *
 * Fragment Component
 */

import type {UserComponent_user$key} from 'UserComponent_user.graphql';

const React = require('React');
const {graphql, useFragment} = require('react-relay');

type Props = {
  user: UserComponent_user$key,
};

function UserComponent(props: Props) {
  const data = useFragment(
    graphql`...`,
    props.user,
  );

  return (...);
}

/**
 * App.react.js
 *
 * Query Component
 */

import type {AppQuery} from 'AppQuery.graphql';
import type {PreloadedQuery} from 'react-relay';

const React = require('React');
const {graphql, usePreloadedQuery} = require('react-relay');

const UserComponent = require('./UserComponent.react');

type Props = {
  appQueryRef: PreloadedQuery<AppQuery>,
}

function App({appQueryRef}) {
  const data = usePreloadedQuery<AppQuery>(
    graphql`
      query AppQuery($id: ID!) {
        user(id: $id) {
          name

          # Include child fragment:
          ...UserComponent_user
        }
      }
    `,
    appQueryRef,
  );

  return (
    <>
      <h1>{data.user?.name}</h1>
      {/* Render child component, passing the fragment reference: */}
      <UserComponent user={data.user} />
    </>
  );
}

์ฐธ๊ณ :

  • ์˜ˆ์ƒ ๋˜๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ ๋Š” UserComponentํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒ์œ„ ์ฟผ๋ฆฌ๋ฅผ ์ฝ์€ ๊ฒฐ๊ณผ์ด๋ฉฐ, ์ด ๊ฒฝ์šฐ์—๋Š” ...UsernameSection_user. ์ฆ‰, ์˜ data๊ฒฐ๊ณผ๋กœ ์–ป์€ ๊ฒƒ์€ usePreloadedQueryํ•ด๋‹น ์ฟผ๋ฆฌ์— ํฌํ•จ๋œ ๋ชจ๋“  ํ•˜์œ„ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋Œ€ํ•œ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ฐธ์กฐ ์—ญํ• ๋„ ํ•ฉ๋‹ˆ๋‹ค.

  • ์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๋ชจ๋“  ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ๋ Œ๋”๋ง๋  ๋•Œ ์ฟผ๋ฆฌ์— ์†ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    • ์ฆ‰, ๋ชจ๋“  ํ”„๋ž˜๊ทธ๋จผํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” ์ฟผ๋ฆฌ์˜ ํ•˜์œ„ ํ•ญ๋ชฉ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค .

Last updated