|
7 | 7 | print,
|
8 | 8 | visit,
|
9 | 9 | } from "graphql";
|
| 10 | +import { GraphQLFormattedError } from "graphql"; |
10 | 11 | import { gql } from "graphql-tag";
|
11 | 12 | import { assign, cloneDeep } from "lodash";
|
12 | 13 | import { EMPTY, EmptyError, Observable, of, Subscription } from "rxjs";
|
@@ -2636,6 +2637,152 @@ describe("client", () => {
|
2636 | 2637 | );
|
2637 | 2638 | });
|
2638 | 2639 |
|
| 2640 | + it("rejects network errors", async () => { |
| 2641 | + const query = gql` |
| 2642 | + query { |
| 2643 | + posts { |
| 2644 | + foo |
| 2645 | + __typename |
| 2646 | + } |
| 2647 | + } |
| 2648 | + `; |
| 2649 | + const error = new Error("Oops"); |
| 2650 | + const link = mockSingleLink({ |
| 2651 | + request: { query }, |
| 2652 | + error, |
| 2653 | + }); |
| 2654 | + const client = new ApolloClient({ |
| 2655 | + link, |
| 2656 | + cache: new InMemoryCache(), |
| 2657 | + }); |
| 2658 | + |
| 2659 | + await expect(client.query({ query })).rejects.toThrow(error); |
| 2660 | + }); |
| 2661 | + |
| 2662 | + it("resolves partial data and GraphQL errors when errorPolicy is 'all'", async () => { |
| 2663 | + const query = gql` |
| 2664 | + query { |
| 2665 | + posts { |
| 2666 | + foo |
| 2667 | + __typename |
| 2668 | + } |
| 2669 | + } |
| 2670 | + `; |
| 2671 | + const errors: GraphQLFormattedError[] = [ |
| 2672 | + { message: 'Cannot query field "foo" on type "Post".' }, |
| 2673 | + ]; |
| 2674 | + const link = mockSingleLink({ |
| 2675 | + request: { query }, |
| 2676 | + result: { data: { posts: null }, errors }, |
| 2677 | + }); |
| 2678 | + const client = new ApolloClient({ |
| 2679 | + link, |
| 2680 | + cache: new InMemoryCache(), |
| 2681 | + }); |
| 2682 | + |
| 2683 | + await expect( |
| 2684 | + client.query({ query, errorPolicy: "all" }) |
| 2685 | + ).resolves.toEqualApolloQueryResult({ |
| 2686 | + data: { posts: null }, |
| 2687 | + error: new CombinedGraphQLErrors([ |
| 2688 | + { message: 'Cannot query field "foo" on type "Post".' }, |
| 2689 | + ]), |
| 2690 | + loading: false, |
| 2691 | + networkStatus: NetworkStatus.error, |
| 2692 | + partial: false, |
| 2693 | + }); |
| 2694 | + }); |
| 2695 | + |
| 2696 | + it("resolves with network error when errorPolicy is 'all'", async () => { |
| 2697 | + const query = gql` |
| 2698 | + query { |
| 2699 | + posts { |
| 2700 | + foo |
| 2701 | + __typename |
| 2702 | + } |
| 2703 | + } |
| 2704 | + `; |
| 2705 | + const error = new Error("Oops"); |
| 2706 | + const link = mockSingleLink({ |
| 2707 | + request: { query }, |
| 2708 | + error, |
| 2709 | + }); |
| 2710 | + const client = new ApolloClient({ |
| 2711 | + link, |
| 2712 | + cache: new InMemoryCache(), |
| 2713 | + }); |
| 2714 | + |
| 2715 | + await expect( |
| 2716 | + client.query({ query, errorPolicy: "all" }) |
| 2717 | + ).resolves.toEqualApolloQueryResult({ |
| 2718 | + data: undefined, |
| 2719 | + error, |
| 2720 | + loading: false, |
| 2721 | + networkStatus: NetworkStatus.error, |
| 2722 | + partial: true, |
| 2723 | + }); |
| 2724 | + }); |
| 2725 | + |
| 2726 | + it("resolves partial data and strips errors when errorPolicy is 'ignore'", async () => { |
| 2727 | + const query = gql` |
| 2728 | + query { |
| 2729 | + posts { |
| 2730 | + foo |
| 2731 | + __typename |
| 2732 | + } |
| 2733 | + } |
| 2734 | + `; |
| 2735 | + const errors: GraphQLFormattedError[] = [ |
| 2736 | + { message: 'Cannot query field "foo" on type "Post".' }, |
| 2737 | + ]; |
| 2738 | + const link = mockSingleLink({ |
| 2739 | + request: { query }, |
| 2740 | + result: { data: { posts: null }, errors }, |
| 2741 | + }); |
| 2742 | + const client = new ApolloClient({ |
| 2743 | + link, |
| 2744 | + cache: new InMemoryCache(), |
| 2745 | + }); |
| 2746 | + |
| 2747 | + await expect( |
| 2748 | + client.query({ query, errorPolicy: "ignore" }) |
| 2749 | + ).resolves.toEqualApolloQueryResult({ |
| 2750 | + data: { posts: null }, |
| 2751 | + loading: false, |
| 2752 | + networkStatus: NetworkStatus.ready, |
| 2753 | + partial: false, |
| 2754 | + }); |
| 2755 | + }); |
| 2756 | + |
| 2757 | + it("resolves with no data or errors for network error when errorPolicy is 'ignore'", async () => { |
| 2758 | + const query = gql` |
| 2759 | + query { |
| 2760 | + posts { |
| 2761 | + foo |
| 2762 | + __typename |
| 2763 | + } |
| 2764 | + } |
| 2765 | + `; |
| 2766 | + const error = new Error("Oops"); |
| 2767 | + const link = mockSingleLink({ |
| 2768 | + request: { query }, |
| 2769 | + error, |
| 2770 | + }); |
| 2771 | + const client = new ApolloClient({ |
| 2772 | + link, |
| 2773 | + cache: new InMemoryCache(), |
| 2774 | + }); |
| 2775 | + |
| 2776 | + await expect( |
| 2777 | + client.query({ query, errorPolicy: "ignore" }) |
| 2778 | + ).resolves.toEqualApolloQueryResult({ |
| 2779 | + data: undefined, |
| 2780 | + loading: false, |
| 2781 | + networkStatus: NetworkStatus.ready, |
| 2782 | + partial: true, |
| 2783 | + }); |
| 2784 | + }); |
| 2785 | + |
2639 | 2786 | it("should warn if server returns wrong data", async () => {
|
2640 | 2787 | using _consoleSpies = spyOnConsole.takeSnapshots("error");
|
2641 | 2788 | const query = gql`
|
|
0 commit comments