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