我正在將我的應用程式遷移到來自 apolloclient 的 react-query。使用 apolloclient,我正在模擬這里顯示的模式。看起來是這樣的:
const users = [
{
name: "user1name",
image: "https://user1image.jpg",
},
{
name: "user2name",
image: "https://user2image.jpg",
},
{
name: "user3name",
image: "https://user3image.jpg",
},
{
name: "user4name",
image: "https://user4image.jpg",
},
];
const cache = new InMemoryCache({
typePolicies: {
Post: {
fields: {
user: {
read() {
return user[Math.floor(Math.random() * people.length)];
},
},
},
},
},
});
然后,在帖子查詢中,我可以@client在用戶欄位之后添加,它將運行read上面指定的函式并回傳該欄位的值,而其他欄位將具有來自后端的資料。
我無法找到任何有關如何使用 react-query 執行類似操作的資訊。甚至可能嗎?
uj5u.com熱心網友回復:
react-query 不是 graphql 特定的庫,因此它無法處理任何與模式相關的事情。react-query 基本上想要從queryFn- 回傳的已解決或已拒絕 Promise 。
因此,也許這個問題與您實際使用發出 graphql 請求的庫更相關,例如graphql-request或urql-core?
您始終可以做的是在queryFn獲取資料之后,但在從useQuery. 如果您查看docs 中的 graphql 基本示例,您可以執行以下操作:
useQuery("posts", async () => {
const {
posts: { data },
} = await request(
endpoint,
gql`
query {
posts {
data {
id
title
}
}
}
`
);
return {
...data,
someOtherField: myMockedOtherFieldData };
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482156.html
