Skip to content

Commit

Permalink
prioritize result.error calling onError over result.data calling onCo…
Browse files Browse the repository at this point in the history
…mpleted
  • Loading branch information
brainkim committed Aug 16, 2021
1 parent 7712bfa commit f1a9770
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ export function useQuery<

let [result, setResult] = useState(() => {
const result = obsQuery.getCurrentResult();
if (!result.loading) {
if (result.data) {
options?.onCompleted?.(result.data);
} else if (result.error) {
options?.onError?.(result.error);
if (!result.loading && options) {
if (result.error) {
options.onError?.(result.error);
} else if (result.data) {
options.onCompleted?.(result.data);
}
}

Expand All @@ -116,10 +116,7 @@ export function useQuery<
useEffect(() => {
const watchQueryOptions = createWatchQueryOptions(query, options);
let nextResult: ApolloQueryResult<TData> | undefined;
if (
ref.current.client !== client ||
!equal(ref.current.query, query)
) {
if (ref.current.client !== client || !equal(ref.current.query, query)) {
const obsQuery = client.watchQuery(watchQueryOptions);
setObsQuery(obsQuery);
nextResult = obsQuery.getCurrentResult();
Expand All @@ -136,11 +133,13 @@ export function useQuery<
}

setResult(ref.current.result = nextResult);
if (!nextResult.loading) {
if (nextResult.data) {
options?.onCompleted?.(nextResult.data);
} else if (nextResult.error) {
options?.onError?.(nextResult.error);
if (!nextResult.loading && options) {
if (!result.loading) {
if (result.error) {
options.onError?.(result.error);
} else if (result.data) {
options.onCompleted?.(result.data);
}
}
}
}
Expand Down

0 comments on commit f1a9770

Please sign in to comment.