Variables

์œ„์˜ ์˜ˆ์‹œ์—์„œ ์ฟผ๋ฆฌ ์„ ์–ธ์— $id ์‹ฌ๋ณผ์ด ์‚ฌ์šฉ๋˜์–ด ์ฐธ์กฐ๊ฐ’์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

GraphQL ๋ณ€์ˆ˜๋Š” ์ฟผ๋ฆฌ ์•ˆ์—์„œ ๋™์ ์ธ ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์„œ๋ฒ„์—์„œ ์ฟผ๋ฆฌ๋ฅผ ํŒจ์น˜ํ•  ๋•Œ, ์‹ค์ œ ๊ฐ’๋“ค์˜ ์„ธํŠธ๋ฅผ input์œผ๋กœ ์ œ๊ณตํ•˜์—ฌ ์ฟผ๋ฆฌ ๋‚ด์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

query UserQuery($id: ID!) {

  # $id: user()์˜ input
  user(id: $id) {
    id
    name
  }

}

์œ„์˜ ์˜ˆ์‹œ์—์„œ ID! ๋Š” $id ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ์˜๋ฏธํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ! ๋Š” ID ํƒ€์ž… ๊ฐ’์ด ํ•„์ˆ˜๋กœ ์š”๊ตฌ๋˜๋Š” ํ•„๋“œ๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

๋„คํŠธ์›Œํฌ์— ์ฟผ๋ฆฌ ํŒจ์นญ์„ ์š”์ฒญํ•  ๋•Œ, ์šฐ๋ฆฌ๋Š” ์ฟผ๋ฆฌ์™€ ํŠน์ • ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ํ•จ๊ป˜ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.

# Query:
query UserQuery($id: ID!) {
  # ...
}

# Variables:
{"id": 4}

์œ„์˜ ์ฟผ๋ฆฌ์™€ ๋ณ€์ˆ˜๋ฅผ ํŒจ์นญํ•˜๋ฉด ์„œ๋ฒ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‘๋‹ต์„ ๊ฐ€์ ธ์˜จ๋‹ค:

{
  "data": {
    "user": {
      "id": "4",
      "name": "Mark Zuckerberg"
    }
  }
}
  • input์œผ๋กœ ์“ฐ์ธ id ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋‹น์—ฐํžˆ, ๋‹ค๋ฅธ ์‘๋‹ต์ด ๋Œ์•„์˜จ๋‹ค.


ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ์ฟผ๋ฆฌ์—์„œ ์ •์˜ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

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

query ViewerQuery($scale: Float!) {
  viewer {
    actor {
      ...UserFragment
    }
  }
}
  • ์œ„์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ $scale ๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ์ •์˜ํ•˜์ง€ ์•Š์•„๋„, ์—ฌ์ „ํžˆ ์ฐธ์กฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ง€๋‹Œ ์ฟผ๋ฆฌ๋Š” ์ง์ ‘์ ์ด๋“  ๊ฐ„์ ‘์ ์ด๋“  ๋ฌด์กฐ๊ฑด ๋ณ€์ˆ˜์™€ ๊ทธ์˜ ํƒ€์ž…์„ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. (์ด๋Ÿฌ์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ ์ƒ์„ฑ)

  • ์ฆ‰, ์ฟผ๋ฆฌ ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น ์ฟผ๋ฆฌ์˜ ํ›„์†์ธ ์–ด๋–ค ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ๋“  globally ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ํ•ด๋‹น ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•œ ์ฟผ๋ฆฌ ๋‚ด์—๋งŒ ์กด์žฌ ๊ฐ€๋Šฅํ•˜๋‹ค.

Relay์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ •์˜ํ•  ์‹œ ์ฟผ๋ฆฌ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.:

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

  return (...);
}
  • ์œ„์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ๋‹ค๋Ÿ‰์˜ ์ฟผ๋ฆฌ์— ํฌํ•จ๋˜๊ณ  ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์— ์˜ํ•ด ๋ Œ๋”๋ง ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ์œ„์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š”/ ํฌํ•จํ•˜๋Š” ์ฟผ๋ฆฌ๋Š” ๋ฐ˜๋“œ์‹œ $scale ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

  • ๋งŒ์•ฝ ์œ„์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” ์ฟผ๋ฆฌ๊ฐ€ $scale ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๋นŒ๋“œ ํƒ€์ž„ ๋•Œ Relay ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋ฉฐ, ์ด๋Š” ์ž˜๋ชป๋œ ์ฟผ๋ฆฌ๋Š” ์ ˆ๋Œ€ ์„œ๋ฒ„์— ์ „์†ก๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค. (๋ณ€์ˆ˜ ์„ ์–ธ์ด ๋น ์ง„ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์€ ์„œ๋ฒ„์—์„œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.)

@arguments and @argumentDefinitions

ํ•˜์ง€๋งŒ, ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•˜๋ฉด์„œ ์ฟผ๋ฆฌ์˜ ๋ถ€ํ”ผ๊ฐ€ ์ปค์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด, Relay๋Š” ์ง€์—ญ์ ์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ์ข…์†๋˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๋Š” @arguments ์™€ @argumentDefinitions directive๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.:

/**
 * @argumentDefinitions ๋ฅผ ์ด์šฉํ•ด arguments๋ฅผ ๊ฐ–๋Š” fragment ์ •์˜
 */

function PictureComponent(props) {
  const data = useFragment(
    graphql`
      fragment PictureComponent_user on User
        @argumentDefinitions(scale: {type: "Float!"}) {

        # *`**$scale**`* is a local variable here, declared above
        # as an argument *`**scale**`*, of type *`**Float!`*
        profile_picture(scale: $scale) {
          uri
        }
      }
    `,
    props.user,
  );
}
/**
 * @arguments ๋ฅผ ์ด์šฉํ•ด fragment ํฌํ•จ์‹œํ‚ค๊ธฐ
 */

function UserComponent(props) {
  const data = useFragment(
    graphql`
      fragment UserComponent_user on User {
        name

        # Pass value of 2.0 for the *`*scale*`* variable
        ...PictureComponent_user @arguments(scale: 2.0)
      }
    `,
    props.user,
  );
}
/**
 * ๋‹ค๋ฅธ @arguments ๋ฅผ ์ด์šฉํ•œ ์œ„์™€ ๊ฐ™์€ fragment
 */

function OtherUserComponent(props) {
  const data = useFragment(
    graphql`
      fragment OtherUserComponent_user on User {
        name

        # Pass a different value for the scale variable.
        # The value can be another local or global variable:
        ...PictureComponent_user @arguments(scale: $pictureScale)
      }
    `,
    props.user,
  );
}
  • @arguments ๋ฅผ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ์ „๋‹ฌํ•  ๋•Œ, ๋ฆฌํ„ฐ๋Ÿด ๋ณ€์ˆ˜๋‚˜ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋Š” ์ „์—ญ ์ฟผ๋ฆฌ ๋ณ€์ˆ˜๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ , @argumentDefinitions ๋กœ ์„ ์–ธํ•œ ๋‹ค๋ฅธ ์ง€์—ญ ๋ณ€์ˆ˜๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

  • PictureComponent_user ์„ ์ฟผ๋ฆฌ์˜ ์ผ๋ถ€๋กœ ์ง์ ‘ ํŒจ์น˜ํ•  ๊ฒฝ์šฐ, profile_picture ํ•„๋“œ์— ์ „๋‹ฌ๋˜๋Š” scale ๋ณ€์ˆ˜๋Š” ๋ถ€๋ชจ์ธ PictureComponent_user ๋กœ๋ถ€ํ„ฐ ์ œ๊ณต๋ฐ›๋Š” ์ธ์ž์— ์˜์กดํ•œ๋‹ค.

    • UserComponent_user ๋ฅผ ์œ„ํ•œ $scale : 2.0

    • OtherUserComponent_user ๋ฅผ ์œ„ํ•œ $scale : ์ฟผ๋ฆฌ๋ฅผ ํŒจ์น˜ํ•  ๋•Œ ์„œ๋ฒ„์— ๋ณด๋‚ด๋Š” $pictureScale ๋ณ€์ˆ˜ ๊ฐ’

์ธ์ž๊ฐ’์ด ํ•„์š”ํ•œ ํ”„๋ž˜๊ทธ๋จผํŠธ์—๋„ ๊ธฐ๋ณธ ๊ฐ’์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋กœ์จ ์ธ์ž๊ฐ’์„ optionalํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.:

/**
 * ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ arguments ์ดˆ๊ธฐํ™”
 */

function PictureComponent(props) {
  const data = useFragment(
    graphql`
      fragment PictureComponent_user on User
        @argumentDefinitions(scale: {type: "Float!", defaultValue: 2.0}) {

        # *`**$scale**`* is a local variable here, declared above
        # as an argument *`**scale**`*, of type *`**Float!` with a default value of *`2.0**`**
        profile_picture(scale: $scale) {
          uri
        }
      }
    `,
    props.user,
  );
}
function UserComponent(props) {
  const data = useFragment(
    graphql`
      fragment UserComponent_user on User {
        name

        # Do not pass an argument, value for scale will be **`2.0**`**
        ...PictureComponent_user
      }
    `,
    props.user,
  );
}
  • PictureComponent_user ์— ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ง€์—ญ๋ณ€์ˆ˜๋กœ ์ •์˜ํ•œ $scale ์—๋Š” ๊ธฐ๋ณธ๊ฐ’์ธ 2.0์ด ํ• ๋‹น๋œ๋‹ค.

Accessing GraphQL Variables At Runtime

๋ฃจํŠธ ์ฟผ๋ฆฌ์— ์„ธํŒ…๋œ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, props๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ์˜ ์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ๋‚˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๋งŒ์˜ ๊ณ ์œ ํ•œ context์— ๋ณ€์ˆ˜๋ฅผ ์ „ํŒŒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœํ•œ๋‹ค.

Relay๋Š” argument definitions๋ฅผ ์ ์šฉํ•œ ์ดํ›„, ํŠน์ • ํ”„๋ž˜๊ทธ๋จผํŠธ์— ํ• ๋‹น๋˜๋Š” resolved๋œ ๋ณ€์ˆ˜๋ฅผ ๋“œ๋Ÿฌ๋‚ด์ง€ ์•Š๋Š”๋ฐ, ์ด๋ฅผ ๋”ฐ๋กœ ์ •์˜ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋ณ„๋กœ ์—†๋‹ค.

Last updated