【发布时间】:2021-04-05 09:50:43
【问题描述】:
我想向我的 swr 添加变量,这些变量使用 graphql 请求获取。这是我的代码
import { request } from "graphql-request";
import useSWR from "swr";
const fetcher = (query, variables) => request(`https://graphql-pokemon.now.sh`, query, variables);
export default function Example() {
const variables = { code: 14 };
const { data, error } = useSWR(
`query GET_DATA($code: String!) {
getRegionsByCode(code: $code) {
code
name
}
}`,
fetcher
);
if (error) return <div>failed to load</div>;
if (!data) return <div>loading...</div>;
return <pre>{JSON.stringify(data, null, 2)}</pre>;
}
但我不知道如何将variables 添加到 swr fetcher 中,因为我知道 useSWR(String, fetcher) 字符串用于查询,而 fetcher 用于获取函数,我可以将变量放在哪里?
【问题讨论】:
标签: reactjs graphql react-hooks swr