jen*_*gel 5 apollo reactjs react-apollo apollo-client react-apollo-hooks
你好,所以我在我的 ReactJS 应用程序上使用 apollo-client 已经有一段时间了。我刚刚注意到,有时当我使用useQuery钩子时,执行完全忽略skip参数,并继续onCompleted(尽管没有任何数据)。有趣的是,它也没有向我的端点发出 API 请求。但是,如果我只是设置skip为 false,那么一切正常(如预期)。
此外,每次我使用useQuerywith时都不会发生这种情况skip,它似乎适用于某些而不是其他。
为什么 apollo 忽略skip参数并onCompleted立即使用空数据执行?有没有修复(除了useLazyQuery)?
const userQuery = useQuery(GET_USER, {
variables: {
value: userId,
},
skip: true,
onCompleted: props => {
console.log(`props => `, props)
const {
user: { type, role, firstName, lastName, permissions },
} = props;
setValue('firstName', firstName);
setValue('lastName', lastName);
setValue(
'type',
userTypes.find(({ value }) => value === type),
);
setValue(
'role',
userRoles.find(({ value }) => value === role),
);
},
});
Run Code Online (Sandbox Code Playgroud)
补充说明:
• 从 onCompleted 函数记录道具的输出是props => undefined。
• 我添加skip: true只是为了证明它实际上不起作用。
• 如果我记录userQuery自己,那么在第一个日志上,立即userQuery.called等于 true(但就像我之前所说的,实际上没有执行 API 调用)
"@apollo/react-hooks": "^3.1.5",
"apollo-cache-inmemory": "^1.6.3",
"apollo-client": "^2.6.10",
"apollo-datasource-rest": "^0.6.6",
"apollo-link": "^1.2.13",
"apollo-link-context": "^1.0.19",
"apollo-link-error": "^1.1.12",
"apollo-link-rest": "^0.7.3",
"apollo-link-retry": "^2.2.15",
"apollo-link-token-refresh": "^0.2.7",
"apollo-upload-client": "^11.0.0",
"react": "16.10.2",
"react-apollo": "^3.1.3",
Run Code Online (Sandbox Code Playgroud)
useLazyQuery似乎工作正常,因此作为一种解决方法,您可以同时使用它useEffect来实现类似的结果。
Apollo 客户端有问题。跳过中的错误是一个烦人的错误。您可以放弃 GraphQL,或解决它们。我建议放弃它,但如果你不能,这里有一个解决方法。该模块的工作方式与 useQuery 类似,并且可以跳过工作。它对于完整规范来说并不完整,但应该可以帮助您入门。
import { useEffect, useState } from 'react';
import { QueryHookOptions, useApolloClient } from '@apollo/react-hooks';
import { ApolloQueryResult, NetworkStatus, QueryOptions, ApolloError } from 'apollo-client';
interface DocumentNode {
loc: {
source: {
body: string;
};
};
}
interface QueryResult<T> extends ApolloQueryResult<T> {
error?: Error | ApolloError;
refetch?: () => void;
}
/**
* Method: useQuery
* Description: enable skip on useQuery. There is a bug that currently ignores it. The bug is confirmed in the 3.2 release as well.
* Note: https://github.com/apollographql/react-apollo/issues/3492
*/
export function useQuery<T>(
query: DocumentNode,
options?: QueryHookOptions
): QueryResult<T | null> {
// Note: using useApolloClient because useLazyQuery fires on initialization. If we are skipping, we don't even want the first fire.
const apolloClient = useApolloClient();
const [result, setQueryResult] = useState<QueryResult<T | null>>({
// Note: initial state
data: null,
loading: false,
stale: false,
networkStatus: NetworkStatus.ready
});
const setResult = (res: QueryResult<T | null>) => {
// Note: GraphQL and Apollo can't decide if they want to return errors, or error in the type contract. I want to be sure the contract is stable. If there are errors, there will be error.
if (res.errors?.length && !res.error) {
[res.error] = res.errors;
}
// Note: Functions should always exist even if the query is not complete.
if (!res.refetch) {
res.refetch = () => {
if (!result.loading) {
execQuery();
}
};
}
setQueryResult(res);
};
const execQuery = async () => {
// Note: loading state. Not spreading "...result" to allow errors to be reset.
setResult({
data: options?.fetchPolicy === 'no-cache' ? null : result.data,
loading: true,
networkStatus: NetworkStatus.loading,
stale: true
});
try {
const queryOptions = ({
...options,
query
} as unknown) as QueryOptions;
await apolloClient
.query(queryOptions)
.then(result => {
setResult(result);
if(options?.onCompleted) options.onCompleted(result.data);
})
.catch(err => {
setResult(err);
if(options?.onError) options.onError(err);
});
} catch (err) {
// Note: fail state
setResult({
...result,
loading: false,
errors: [err],
error: err,
stale: true,
networkStatus: NetworkStatus.error
});
}
};
useEffect(() => {
// Note: Skip Functionality
if (!result.loading && options?.skip !== true) {
execQuery();
}
// Note only listen too explicit variables. If you listen to whole objects they are always different and will cause endless loops.
}, [options?.skip, query.loc.source.body]);
return result;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3102 次 |
| 最近记录: |