Apollo-link-rest: 깨진 React-Apollo 예제

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

μ•ˆλ…•ν•˜μ„Έμš”:

react-apollo codesandbox 의 κΈ°λ³Έ κ΅¬μ„±μœΌλ‘œ apollo-link-rest μ‹œμž‘ ν•˜κ² μŠ΅λ‹ˆλ‹€ .

TV ν”„λ‘œκ·Έλž¨ λͺ©λ‘μ„ ν‘œμ‹œν•˜λŠ” λŒ€μ‹  μΈν„°νŽ˜μ΄μŠ€μ— : Network error: Failed to execute 'fetch' on 'Window': Invalid credentials mode 였λ₯˜κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€.

λ‚΄ μžμ‹ μ˜ create-react-app ν™˜κ²½μ—μ„œ ν•΄λ‹Ή μ½”λ“œλ₯Ό 볡제 ν•  λ•Œ Apollo DevToolsμ—μ„œ μš”μ²­μ„ μ‹€ν–‰ν•  λ•Œ λ‹€μŒ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

{
  "errors": [
    {
      "message": "forward is not a function",
      "locations": [
        "TypeError: forward is not a function\n    at RestLink../node_modules/apollo-link-rest/bundle.umd.js.RestLink.request (http://localhost:3000/static/js/bundle.js:21028:20)\n    at http://localhost:3000/static/js/bundle.js:19809:33\n    at ApolloLink.request (http://localhost:3000/static/js/bundle.js:17407:12)\n    at ApolloLink.request 
    }
  ]
}

이 링크에 λŒ€ν•œ μž‘μ—…μ— κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€! REST APIλ₯Ό μ‚¬μš©ν•˜λŠ” μ‚¬λžŒλ“€μ„ μœ„ν•΄ GraphQLκ³Ό μœ μ‚¬ν•œ κΈ°λŠ₯을 μ œκ³΅ν•¨μœΌλ‘œμ¨ REST μž‘μ—…μ„ 훨씬 더 μ‰½κ²Œ 관리 ν•  수 β€‹β€‹μžˆμŠ΅λ‹ˆλ‹€.

help wanted πŸ›  question❔

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

v0.3.1μ—μ„œ μˆ˜μ •λ˜μ–΄μ•Όν•©λ‹ˆλ‹€-이제 μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€!

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

@sinisterra μ•±μ—μ„œ forward is not a function λ₯Ό λ³Ό λ•Œ 일반적으둜 λ…Έλ“œ 쀑 ν•˜λ‚˜μ—μ„œ @client λ˜λŠ” @rest(…) μ§€μ‹œλ¬Έμ„ μžŠμ—ˆ κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. λ‹Ήμ‹ μ„μœ„ν•œ κ²½μš°μž…λ‹ˆκΉŒ?

fetch 였λ₯˜μ™€ κ΄€λ ¨ν•˜μ—¬ μ–΄λ–€ λΈŒλΌμš°μ €μ—μ„œ ν…ŒμŠ€νŠΈν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ?

@fbartho μ•„λ‹ˆμš”, μ˜ˆμ œμ™€ λ˜‘κ°™μ€ 쿼리λ₯Ό μ‹€ν–‰ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. @rest μ§€μ‹œλ¬Έμ΄ μ œλŒ€λ‘œ 적용되고 μžˆμŠ΅λ‹ˆλ‹€. Google Chrome 67.0.3396.62λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. Firefox 60.0.1μ—μ„œ 예제λ₯Ό μ‹€ν–‰ν•˜λ €κ³ ν–ˆμ„ λ•Œ μ‹€νŒ¨ν–ˆμ§€λ§Œ λ‹€λ₯Έ 였λ₯˜ : Network error: 'credentials' member of RequestInit 'null' is not a valid value for enumeration RequestCredentials.

깨진 예 뿐만이 μ•„λ‹™λ‹ˆλ‹€! 이 λ¬Έμ œλŠ” 맀우 μ€‘μš”ν•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.
λ‚˜λŠ” λ˜ν•œ apollo-link-restλ₯Ό μ‚¬μš©ν•˜μ—¬ μ§€λ‚œ λͺ‡ 달 λ™μ•ˆ 잘 μž‘λ™ν–ˆλ˜ ν”„λ‘œλ•μ…˜ ν™˜κ²½μ—μ„œ λ™μΌν•œ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. 이것이 λΈŒλΌμš°μ € μ—…λ°μ΄νŠΈ λ•Œλ¬ΈμΈ 것 κ°™μŠ΅λ‹ˆλ‹€.
크둬 Version 67.0.3396.79 (Official Build) (64-bit)

같은 λ¬Έμ œκ°€ μ‚¬νŒŒλ¦¬μ™€ νŒŒμ΄μ–΄ ν­μŠ€μ—μ„œ μΌμ–΄λ‚˜κ³  μžˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” 그것을 μ™„μ „νžˆ 얻지 λͺ»ν•©λ‹ˆλ‹€. 5 μ›” 24 일 μ΄ν›„λ‘œ λ™μΌν•œ λ²„μ „μ˜ λ‚΄ μ‘μš© ν”„λ‘œκ·Έλž¨μ΄ 잘 μž‘λ™ν–ˆμŠ΅λ‹ˆλ‹€.

μž‘λ™ν•˜λŠ” customFetch fn을 κ΅¬μ„±ν–ˆμ§€λ§Œ apollo-link-rest둜 μˆ˜ν–‰ν•˜λŠ” μš”μ²­μ—μ„œλ§Œ λ°œμƒν•©λ‹ˆλ‹€. Β―_ (ツ) _ / Β―

customFetch ꡬ성을 제거 ν•  λ•Œλ„ λ™μΌν•œ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

μ‹€μ œλ‘œ 문제λ₯Ό λ°œκ²¬ν–ˆμŠ΅λ‹ˆλ‹€.
customFetch ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³  μžˆλŠ”λ°,이 ν•¨μˆ˜λŠ” apollo-link-rest μ—μ„œ μ˜΅μ…˜ 맀개 λ³€μˆ˜ credentials: null 와 ν•¨κ»˜ ν˜ΈμΆœλ©λ‹ˆλ‹€. 이둜 인해 μƒˆ λ²„μ „μ˜ 크둬을 ν¬ν•¨ν•œ λͺ¨λ“  λΈŒλΌμš°μ €μ—μ„œμ΄ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

자격 증λͺ…을 μ‚¬μš©ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ‚΄ μ‚¬μš©μž 지정 κ°€μ Έ 였기 κΈ°λŠ₯에 λŒ€ν•œ λΉ λ₯Έ μˆ˜μ •μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

export function fetchWithAuth(url, options = {}) {
  // this fixes apollo-link-rest providing credentials: null options
  const { credentials, ...opt } = options
  return fetch(url, getOptions(opt));
}

λ˜ν•œ "λ„€νŠΈμ›Œν¬ 였λ₯˜ : Failed to Fetch"κ°€ λ°œμƒν•˜μ—¬ 쿼리가 apollo-link-rest둜 μ‹€ν–‰λ˜λŠ” 것을 막아 벽에 머리λ₯Ό λΆ€λ”ͺ ν˜”μŠ΅λ‹ˆλ‹€. @JuHwon 이 λ§žμŠ΅λ‹ˆλ‹€. 이것은 RestLink μƒμ„±μžμ˜ 자격 증λͺ… μ˜΅μ…˜κ³Ό μ—°κ²°λœ κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€. μ΄μ „μ—λŠ” λ¬Έμ„œμ— 따라 선택 μ‚¬ν•­μœΌλ‘œ μž‘λ™ν–ˆμ§€λ§Œ μ΄μ œλŠ” λ‹€μŒκ³Ό 같은 μ˜΅μ…˜μ„ ν¬ν•¨ν•΄μ•Όλ§Œ μž‘λ™ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const restLink = new RestLink({  
  uri: ${process.env.REACT_APP_API_URL}/,  
  credentials: 'same-origin',  // eg. or credentials: 'omit', etc
});

v0.3.1μ—μ„œ μˆ˜μ •λ˜μ–΄μ•Όν•©λ‹ˆλ‹€-이제 μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€!

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