๋ฌธ์ ์ ๋ฐ๋ฅด๋ฉด fetchOptions
์ต์
์ ์ฌ์ฉํ๋ฉด ์์ฒญ ๋ฐฉ๋ฒ์ GET
๋ก ์ค์ ํ ์ ์์ต๋๋ค. ๊ทธ๋ ๊ฒ ํ ํ์ ์ฟผ๋ฆฌ๊ฐ ์์ฒญ ๋ณธ๋ฌธ์ด ์๋ ์ฟผ๋ฆฌ ๋ฌธ์์ด ๋งค๊ฐ๋ณ์๋ก ํฌํจ๋ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค. ๋ณธ๋ฌธ์ผ๋ก GET ์์ฒญ์ ํ๋ ค๊ณ ํ๋ฉด Chrome์์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์๋ํ ๊ฒฐ๊ณผ:
์์ฒญ ๋ฐฉ๋ฒ์ด GET์ธ ๊ฒฝ์ฐ ์ฟผ๋ฆฌ ๋ฌธ์์ด์ ๊ตฌ์ฑํด์ผ ํฉ๋๋ค.
์ค์ ๊ฒฐ๊ณผ:
์์ฒญ ๋ณธ๋ฌธ์ด ์ฌ์ฉ๋ฉ๋๋ค.
๋ฌธ์ ๋ฅผ ์ฌํํ๋ ๋ฐฉ๋ฒ:
client.query({
context: {
fetchOptions: {
method: 'GET',
},
},
query: QUERY,
variables: { query },
})
@szdc ๋ค, ํ์คํ ์๋ํ์ง ์์ต๋๋ค! ๋น์ ์ ๊ทธ๊ฒ์ ์ถ๊ฐ์ ์ด๋ ค ์์ต๋๊น? https://github.com/apollographql/apollo-link/issues/257 ๊ณผ ๊ฐ์ ๊ฒ์ด ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ ์ ๋ณํํ๋ ๋ฐ๋ ์ข์ ์๋ฃจ์ ์ด ๋ ์ ์์ต๋๊น?
cc @arackaf @outlaw11a @puttyman ๋ชจ๋ ๊ด์ฌ์ ๊ฐ๊ณ ์์ต๋๋ค! ์ถ๊ฐํ๋ ค๋ฉด ๊ฝค ์์ ๋ณ๊ฒฝ์ด์ด์ผ ํฉ๋๋ค!
์๋ฌด๋ ์ํ์ง ์์ผ๋ฉด ์ถ๊ฐํ๊ฒ ์ต๋๋ค.
@szdc @arackaf ๊ทธ๋์ ์ด๊ฒ์ด ์ด๋ฏธ ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ด ๋ฐํ์ก์ต๋๋ค! ํ ์คํธ๋ฅผ ์งํํ๊ณ ๋ด๋ถ ๋ณ๊ฒฝ ์์ด ์ํํ ์ ์๋ ๋ฐฉ๋ฒ์ ๊ณง ๋ฌธ์ํํ๊ฒ ์ต๋๋ค.
@szdc @Outlaw11A @arackaf ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const customFetch = (uri, options) => {
const { body, ...newOptions } = options;
const queryString = objectToQuery(JSON.parse(body));
requestedString = uri + queryString;
return fetch(requestedString, newOptions);
};
const link = createHttpLink({
uri: "data",
fetchOptions: { method: "GET" },
fetch: customFetch
});
@jbaxleyiii ๋ฉ์ง๋ค์! objectToQuery๊ฐ ์ด๋์์ ์ค๋์ง ๊ถ๊ธํ์ ๊ฐ์? npm์ ์ ํธ๋ฆฌํฐ๊ฐ ์์ต๋๊น? ํ์ํ ๊ฒฝ์ฐ ์์ ์ ๊ฒ์ ๋กค๋งํ๋ ๊ฒ์ด ๊ฝค ์ฌ์ธ ๊ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก Object.keys๋ฅผ ๋งคํํ์ญ์์ค.
escapeUriComponnt(`${k}=${obj[k]}`).join("&");
๋๋ ๋น์ทํ ๊ฒ
https://www.apollographql.com/docs/link/links/http.html#Sending -GET-requests-custom-fetching
@arackaf ๋ ์ฃผ์์ ์ฐธ์กฐํ์ญ์์ค.
// turn the object into a query string, try object-to-querystring package
@jbaxleyiii ์๋ํฉ๋๋ค! ๋๋๋ค!
๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์์ฑ๋ ์ฟผ๋ฆฌ ๋ฌธ์์ด:
http://localhost/graphql?operationName=AllPlayersQuery&variables=%5Bobject%20Object%5D&query=query%20All...
๋ฌธ์ ๊ฐ ์๋ variables=%5Bobject%20Object%5D
ํฌํจํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ ๋๊น?
๊ทธ๊ฒ์ ํ์ค URI ์ง๋ ฌํ์ฒ๋ผ ๋ณด์ ๋๋ค. ํผํ ์ ์๋ค๊ณ ์๊ฐํ์ง ๋ง์ญ์์ค. ์ ๋ฌธ์ ๊ฐ ๋ ๊น์?
ํ์ค URI? ์ด๊ฒ์: %5Bobject%20Object%5D
?
encodeURIComponent('books(title:"Hello World"){๋์{title}}')
books(title%3A%22Hello%20World%22)%7BBook%7Btitle%7D%7D
Apollo์์ ๋ด ์ฟผ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
query AllPlayers {
players {
id
name
surname
}
๊ทธ๋์ @jbaxleyiii๊ฐ ๊ฒ์ํ ์ฝ๋์ ํจ๊ป [Object object]
๋ณ์๊ฐ ๋น์ด ์๋ค๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๋ค์์ GET์ ํตํด ๊ฐ์ ธ์ ์๋ํ๋ ๊ธฐ๋ณธ ๋ฐ์ ์ฑ(create-react-app์ ์ฌ์ฉํ์ฌ ์์ฑ)์
๋๋ค. index.js
์ฐธ์กฐ :
@johnunclesam ๋ถ๋ช
ํ variables
๋ ์ธ์ฝ๋ฉ ์ ์ ์ง๋ ฌํ๋์ง ์์ต๋๋ค. ์กฐ๊ฑด๋ถ JSON.stringify
๋ฅผ ์ถ๊ฐํ์ฌ ์ฌ์ฉ์ ์ง์ ๊ฐ์ ธ์ค๊ธฐ ๋ฐฉ๋ฒ(์์ ๋ฆฌํฌ์งํ ๋ฆฌ์์ ๊ฐ์ ธ์ด)์ ๊ฐ๋จํ ์กฐ์ ํ์ต๋๋ค.
const customFetch = (uri, options) => {
const {body, ...newOptions} = options;
const parsedBody = JSON.parse(body);
const command = omitBy(parsedBody, isEmpty);
if (command.variables) {
command.variables = JSON.stringify(command.variables);
}
const requestedString = uri + '?' + queryString.stringify(command);
return fetch(requestedString, newOptions);
};
๊ทธ๋ ๋ค๋ฉด useGETForQueries
๋ ๋ฌด์์ ์ฌ์ฉ๋ฉ๋๊น? useGETForQueries=true
๋ ๋ง์ถค ๊ฐ์ ธ์ค๊ธฐ๊ฐ ๋ด์ฅ๋์ง ์์ ์ด์ ๋ ๋ฌด์์
๋๊น?
์ฟผ๋ฆฌ์ ๋์ฐ๋ณ์ด๋ฅผ ์ด๋ป๊ฒ ๊ตฌ๋ณํฉ๋๊น? ์ฟผ๋ฆฌ(DNS ์บ์์ฉ)์๋ง GET์ ์ฌ์ฉํ๊ณ ์ถ์ต๋๋ค.
์ด๊ฒ์ https://github.com/apollographql/apollo-link/pull/510 ์์ ์์ ๋ ํ ์ฝ๊ฐ ์ถ๊ฐ๋ useGETForQueries ์ด์ ์ ์ค๋๋ ๋ฌธ์ ์ ๋๋ค.
์, useGETForQueries๊ฐ ์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
ํนํ ์ด ์ค์ ๋ฒ๊ทธ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
let {
uri = '/graphql',
// use default global fetch if nothing passed in
fetch: fetcher,
includeExtensions,
useGETForQueries,
...requestOptions
} = linkOptions;
๋ค์๊ณผ ๊ฐ์์ผ ํฉ๋๋ค.
let {
uri = '/graphql',
// use default global fetch if nothing passed in
fetch: fetcher,
includeExtensions,
fetchOptions,
...requestOptions
} = linkOptions;
let {
useGETForQueries
} = fetchOptions;
์ ๊ทธ๋ ๊ฒ ๋งํฉ๋๊น? createHttpLink
์ด๋ป๊ฒ ์ ํ๋ฅผ ๊ฑฐ์ญ๋๊น?
์์ ๋ด ๊ด์ฐฐ์ ๋๋ฒ๊ฑฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ๋จ๊ณ๋ณ๋ก ์คํํ๋ ๊ฒ์ด์ง๋ง ์ฌํํ๋ ค๋ฉด ๋ค์ ์ํํด์ผ ํฉ๋๋ค.
์ฐ๋ฆฌ๊ฐ ์ต์ข ์ ์ผ๋ก ๊ฒฐ์ ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import omitBy from 'lodash.omitby';
import isEmpty from 'lodash.isempty';
const graphQLUrl = 'http://the.server.com/graphql';
const customFetch = (uri, options) => {
const { body, credentials, headers, ...newOptions } = options;
let fetchUri = uri;
if (body) {
const parsedBody = JSON.parse(body);
const command = omitBy(parsedBody, isEmpty);
fetchUri = uri + "?" + queryString.stringify(command);
}
return fetch(fetchUri, newOptions);
};
const link = createHttpLink({
uri: graphQLUrl,
fetchOptions: { method: "GET" },
fetch: customFetch
});
๋น์ ์ด ํต๊ณผ๋๋ค๋ฉด ๋๋ ๋์ ์ง๋ฌธ์ ์๊ฐ useGETForQueries
๋ด๋ถ fetchOptions
๋์ ์ง์ ๋ด๋ถ์ createHttpLink
, ๊ทธ ๋น์ ์ด ๊ด์ฐฐ ํ ํ๋,ํ์ง๋ง ๊ฒ ๋ํํ์ง ์์ ๊ฒ์ด๋ค API ์๋ ๋ฐฉ์. ๋ค์ ํ ๋ฒ ํด๋ณด๋ ๊ฒ์ ์ถ์ฒํฉ๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@szdc @Outlaw11A @arackaf ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.