Apollo-link-rest: POST μš”μ²­μ— 데이터λ₯Ό μ „λ‹¬ν•˜λŠ” κΈ°λŠ₯

에 λ§Œλ“  2018λ…„ 06μ›” 16일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: apollographql/apollo-link-rest

데이터λ₯Ό JSON으둜 κ²Œμ‹œν•΄μ•Ό ν•˜λŠ” κΈ°μ‘΄ REST APIκ°€ μžˆμŠ΅λ‹ˆλ‹€. url λ§€κ°œλ³€μˆ˜λ₯Ό ν—ˆμš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” λ¬Έμ„œλ₯Ό μ‘°μ‚¬ν–ˆμ§€λ§Œ 이것에 λŒ€ν•΄ μ΄μ•ΌκΈ°ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ§€μ›λ˜λ‚˜μš”?

λ‹€μŒμ€ λ‚΄κ°€ λ‹¬μ„±ν•˜λ €λŠ” κ²ƒμ˜ μ˜ˆμž…λ‹ˆλ‹€.

curl -X POST http://localhost:8000/rest-auth/login/ -d '{}'

미리 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€!

question❔

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

당신이 μ˜³μŠ΅λ‹ˆλ‹€. bodyKey/input은 당신이 ν•„μš”λ‘œ ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

첫째: μ§€μ‹œλ¬Έμ— μž…λ ₯을 μ „λ‹¬ν•˜λ―€λ‘œ μ•„λ§ˆλ„ λ‹€μŒμ„ μ›ν–ˆμ„ κ²ƒμž…λ‹ˆλ‹€.

mutation login ($username: String!, $password: String!) {
  login(input: {username: $username, password: $password}) @rest(
    method: "POST",
    endpoint: "default",
    path: "rest-auth/login/", 
  ) {
    results
  }
}

ν•˜μ§€λ§Œ μž‘λ™ν•˜μ§€ μ•Šμ„ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 제 μƒκ°μ—λŠ” "μž…λ ₯"이 μ—¬λŸ¬λΆ„μ²˜λŸΌ λ™μ μœΌλ‘œ ꡬ성될 수 μ—†λŠ” 버그가 μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. (κ·Έ 버그λ₯Ό μˆ˜μ •ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€)

λ…Έλ ₯ν•˜λ‹€:

input LoginInput { # you can omit this whole declaration with ApolloLink Rest
  username: String!
  password: String!
}

mutation login(input: LoginInput!) {
  login(input: $input) @rest(
    method: "POST",
    endpoint: "default", # if you only have 1 endpoint, you can omit this too if you create your rest link the right way
    path: "rest-auth/login/", 
  ) {
    results
  }
}

이 λ§ˆμ§€λ§‰ μ˜ˆλŠ” 기본적으둜 λ‚΄ 앱에 μžˆλŠ” κ²ƒμž…λ‹ˆλ‹€.

λͺ¨λ“  3 λŒ“κΈ€

bodyKey/bodyBuilder λ‚΄κ°€ 찾던 것 κ°™μ•„μš”.

λ‚˜λŠ” λ‹€μŒκ³Ό 같은 일을 μ‹œλ„ν–ˆμŠ΅λ‹ˆλ‹€.

mutation login ($username: String!, $password: String!) {
  login @rest(
    method: "POST",
    endpoint: "default",
    path: "rest-auth/login/", 
    input: {username: $username, password: $password}
  ) {
    results
  }
}

κ·ΈλŸ¬λ‚˜μ΄ 였λ₯˜κ°€ 계속 λ°œμƒν•©λ‹ˆλ‹€.

[GraphQL mutation using a REST call without a body]. No `input` was detected. Pass bodyKey, or bodyBuilder to the @rest() directive to resolve this.

λ‚΄ νœ΄μ‹ λ§ν¬λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

export const restLink = new RestLink({
  endpoints: {
    default: process.env.REACT_APP_HOST_URL,
  },
  headers: {
    "Content-Type": "application/json"
  },
  credentials: "same-origin",
})

λ‚΄ ν΄λΌμ΄μ–ΈνŠΈλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

const client = new ApolloClient({
  link: ApolloLink.from([ stateLink, restLink ]),
  cache: cache,
  defaultOptions,
})

당신이 μ˜³μŠ΅λ‹ˆλ‹€. bodyKey/input은 당신이 ν•„μš”λ‘œ ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

첫째: μ§€μ‹œλ¬Έμ— μž…λ ₯을 μ „λ‹¬ν•˜λ―€λ‘œ μ•„λ§ˆλ„ λ‹€μŒμ„ μ›ν–ˆμ„ κ²ƒμž…λ‹ˆλ‹€.

mutation login ($username: String!, $password: String!) {
  login(input: {username: $username, password: $password}) @rest(
    method: "POST",
    endpoint: "default",
    path: "rest-auth/login/", 
  ) {
    results
  }
}

ν•˜μ§€λ§Œ μž‘λ™ν•˜μ§€ μ•Šμ„ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 제 μƒκ°μ—λŠ” "μž…λ ₯"이 μ—¬λŸ¬λΆ„μ²˜λŸΌ λ™μ μœΌλ‘œ ꡬ성될 수 μ—†λŠ” 버그가 μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. (κ·Έ 버그λ₯Ό μˆ˜μ •ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€)

λ…Έλ ₯ν•˜λ‹€:

input LoginInput { # you can omit this whole declaration with ApolloLink Rest
  username: String!
  password: String!
}

mutation login(input: LoginInput!) {
  login(input: $input) @rest(
    method: "POST",
    endpoint: "default", # if you only have 1 endpoint, you can omit this too if you create your rest link the right way
    path: "rest-auth/login/", 
  ) {
    results
  }
}

이 λ§ˆμ§€λ§‰ μ˜ˆλŠ” 기본적으둜 λ‚΄ 앱에 μžˆλŠ” κ²ƒμž…λ‹ˆλ‹€.

@fbartho - κ°μ‚¬ν•©λ‹ˆλ‹€! 이것은 λ§Žμ€ 것을 λͺ…ν™•νžˆ ν–ˆμŠ΅λ‹ˆλ‹€. λ§žμŠ΅λ‹ˆλ‹€. μ‹€μˆ˜λ‘œ μ§€μ‹œλ¬Έμ— μž…λ ₯을 μž…λ ₯ν–ˆμŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰