Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaultOptions errorPolicy: 'all' does not take effect in useQuery #6561

Closed
sushruth opened this issue Jul 8, 2020 · 7 comments
Closed

defaultOptions errorPolicy: 'all' does not take effect in useQuery #6561

sushruth opened this issue Jul 8, 2020 · 7 comments

Comments

@sushruth
Copy link

sushruth commented Jul 8, 2020

errorPolicy when specified in defaultOptions does not seem to take effect for useQuery when the query has multiple queries within.

Intended outcome:
useQuery must populate data and error together if one of the multiple queries fails to resolve, when default error policy is 'all'

Actual outcome:
only error is populated.

How to reproduce the issue:
A repo that can repro this issue - https://github.com/sushruth/apollo-client-erorrpolicy-issue

Versions

  System:
    OS: Windows 10 10.0.19608
  Binaries:
    Node: 14.0.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
    Yarn: 1.22.4 - ~\scoop\apps\yarn\current\Yarn\bin\yarn.CMD
    npm: 6.14.4 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
  Browsers:
    Edge: 44.19608.1000.0
  npmPackages:
    @apollo/client: 3.0.0-rc.10 => 3.0.0-rc.10
    @apollo/link-schema: 2.0.0-beta.3 => 2.0.0-beta.3
@abdullahtariq1171
Copy link

hey @sushruth the repo link you shared is not accessible.

@sushruth
Copy link
Author

sushruth commented Jul 9, 2020

@abdullahtariq1171 sorry about that! it is public now. Thank you.

edit: here is the code snippet anyway -

// client.ts
import { SchemaLink } from "@apollo/link-schema";
import { gql } from "@apollo/client";
import { makeExecutableSchema } from "@graphql-tools/schema";

import { InMemoryCache, ApolloClient } from "@apollo/client/core";

const typeDefs = gql`
  type Query {
    dogs: String
    cats: String
  }
`;

const schema = makeExecutableSchema({
  typeDefs,
  resolvers: {
    Query: {
      dogs: () => {
        return "bark";
      },
      cats: () => {
        throw Error('Hail Satan')
      }
    }
  }
});

const apolloCache = new InMemoryCache();

export const graphqlClient = new ApolloClient({
  cache: apolloCache,
  link: new SchemaLink({ schema })
});
// App.tsx
import { ApolloProvider, gql, useQuery } from "@apollo/client";
import React from "react";
import { graphqlClient } from "./client";
import "./styles.css";

const GET_DOGS = gql`
  query GetDogs {
    dogs
    cats
  }
`;

const Compo1: React.FC = () => {
  const { data, error } = useQuery(GET_DOGS);
  console.log("[Compo1] This is the data - ", data, error);
  return null;
};

const Compo2: React.FC = () => {
  const { data, error } = useQuery(GET_DOGS, {
    errorPolicy: "all"
  });
  console.log("[Compo2] This is the data - ", data, error);
  return null;
};

export default function App() {
  return (
    <ApolloProvider client={graphqlClient}>
      <div className="App">
        <Compo1 />
        <Compo2 />
        <h1>Check console</h1>
      </div>
    </ApolloProvider>
  );
}

@abdullahtariq1171
Copy link

hey, I ran the attached repo locally, looks like theerrorPolicy: 'all', is being respected as expected.

[Compo2] This is the data -   Object { dogs: "bark", cats: null } Object { error: Error }
[Compo1] This is the data -   Object { dogs: "bark", cats: null } Object { error: undefined }

Further dug into it and it has test which is passing

itAsync('returns errors with data if errorPolicy is all', (resolve, reject) => {

If you think I'm misunderstanding the issue, please guide me as I haven't used errorPolicy personally and new contributor here. Thanks

@sushruth
Copy link
Author

sushruth commented Jul 13, 2020

@abdullahtariq1171 - The current issue is about using errorPolicy in defaultOptions in new ApolloClient(), which is not being honored. The test you have mentioned looks like it us using the watchQuery hook with explicit errorPolicy mentioned in the props. Can you share your lock file and npx envinfo@latest --preset apollo --clipboard output so that I can repro it in the same setup?

On the same note, it looks like the issue is not present as described in the original description when using 3.0.0-rc.13

image

Although, the component Comp1 does seem to get error and data at separate times and not together, it does not look right. It also does not signify partial error or partial success looking at individual render data.

I updated the repo with a little more cleanup. Please use it to repro once again - https://github.com/sushruth/apollo-client-erorrpolicy-issue

Updated component descriptions -

const Compo1: React.FC = () => {
  const { data, error, loading, called } = useQuery(GET_DOGS);
  
  if(!loading && called) {
    console.log("[Compo1] Without explicit errorPolicy - ", data, error?.message);
  }
  
  return null;
};

const Compo2: React.FC = () => {
  const { data, error, loading, called } = useQuery(GET_DOGS, {
    errorPolicy: "all"
  });

  if(!loading && called) {
    console.log("[Compo2] With explicit errorPolicy - ", data, error?.message);
  }
  
  return null;
};

@Titozzz
Copy link

Titozzz commented Jul 24, 2020

Related to #6677

@Titozzz
Copy link

Titozzz commented Jul 24, 2020

Looks like default Options are not working properly 😢

@hwillson
Copy link
Member

hwillson commented May 3, 2021

This was fixed in #6715 - thanks!

@hwillson hwillson closed this as completed May 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants