|
11 | 11 | 'use strict';
|
12 | 12 |
|
13 | 13 | const {graphql} = require('../../query/GraphQLTag');
|
| 14 | +const RelayFeatureFlags = require('../../util/RelayFeatureFlags'); |
14 | 15 | const {
|
15 | 16 | createOperationDescriptor,
|
16 | 17 | } = require('../RelayModernOperationDescriptor');
|
@@ -635,4 +636,251 @@ describe('RelayReader error fields', () => {
|
635 | 636 | },
|
636 | 637 | ]);
|
637 | 638 | });
|
| 639 | + |
| 640 | + let wasNoncompliantErrorHandlingOnListsEnabled; |
| 641 | + |
| 642 | + beforeEach(() => { |
| 643 | + wasNoncompliantErrorHandlingOnListsEnabled = |
| 644 | + RelayFeatureFlags.ENABLE_NONCOMPLIANT_ERROR_HANDLING_ON_LISTS; |
| 645 | + }); |
| 646 | + |
| 647 | + afterEach(() => { |
| 648 | + RelayFeatureFlags.ENABLE_NONCOMPLIANT_ERROR_HANDLING_ON_LISTS = |
| 649 | + wasNoncompliantErrorHandlingOnListsEnabled; |
| 650 | + }); |
| 651 | + |
| 652 | + describe('when noncompliant error handling on lists is enabled', () => { |
| 653 | + beforeEach(() => { |
| 654 | + RelayFeatureFlags.ENABLE_NONCOMPLIANT_ERROR_HANDLING_ON_LISTS = true; |
| 655 | + }); |
| 656 | + |
| 657 | + describe('when query has @throwOnFieldError directive', () => { |
| 658 | + it('has errors that will throw when the linked field is an empty list', () => { |
| 659 | + const source = RelayRecordSource.create({ |
| 660 | + '1': { |
| 661 | + __id: '1', |
| 662 | + __typename: 'User', |
| 663 | + 'friends(first:3)': { |
| 664 | + __ref: 'client:1:friends(first:3)', |
| 665 | + }, |
| 666 | + id: '1', |
| 667 | + }, |
| 668 | + 'client:1:friends(first:3)': { |
| 669 | + __id: 'client:1:friends(first:3)', |
| 670 | + __typename: 'FriendsConnection', |
| 671 | + __errors: { |
| 672 | + edges: [ |
| 673 | + { |
| 674 | + message: 'There was an error!', |
| 675 | + }, |
| 676 | + ], |
| 677 | + }, |
| 678 | + edges: { |
| 679 | + __refs: [], |
| 680 | + }, |
| 681 | + }, |
| 682 | + 'client:root': { |
| 683 | + __id: 'client:root', |
| 684 | + __typename: '__Root', |
| 685 | + 'node(id:"1")': {__ref: '1'}, |
| 686 | + }, |
| 687 | + }); |
| 688 | + |
| 689 | + const FooQuery = graphql` |
| 690 | + query RelayReaderRelayErrorHandlingTestNoncompliantEmptyLinkedFieldWithThrowOnFieldErrorQuery |
| 691 | + @throwOnFieldError { |
| 692 | + node(id: "1") { |
| 693 | + id |
| 694 | + __typename |
| 695 | + ... on User { |
| 696 | + friends(first: 3) { |
| 697 | + edges { |
| 698 | + cursor |
| 699 | + } |
| 700 | + } |
| 701 | + } |
| 702 | + } |
| 703 | + } |
| 704 | + `; |
| 705 | + const operation = createOperationDescriptor(FooQuery, {}); |
| 706 | + const {errorResponseFields} = read(source, operation.fragment); |
| 707 | + |
| 708 | + expect(errorResponseFields).toEqual([ |
| 709 | + { |
| 710 | + fieldPath: '', |
| 711 | + handled: false, |
| 712 | + error: {message: 'There was an error!'}, |
| 713 | + kind: 'relay_field_payload.error', |
| 714 | + owner: |
| 715 | + 'RelayReaderRelayErrorHandlingTestNoncompliantEmptyLinkedFieldWithThrowOnFieldErrorQuery', |
| 716 | + shouldThrow: true, |
| 717 | + }, |
| 718 | + ]); |
| 719 | + }); |
| 720 | + |
| 721 | + it('has errors that will throw when the scalar field is an empty list', () => { |
| 722 | + const source = RelayRecordSource.create({ |
| 723 | + '1': { |
| 724 | + __id: '1', |
| 725 | + __typename: 'User', |
| 726 | + __errors: { |
| 727 | + emailAddresses: [ |
| 728 | + { |
| 729 | + message: 'There was an error!', |
| 730 | + }, |
| 731 | + ], |
| 732 | + }, |
| 733 | + id: '1', |
| 734 | + emailAddresses: [], |
| 735 | + }, |
| 736 | + 'client:root': { |
| 737 | + __id: 'client:root', |
| 738 | + __typename: '__Root', |
| 739 | + 'node(id:"1")': {__ref: '1'}, |
| 740 | + }, |
| 741 | + }); |
| 742 | + |
| 743 | + const FooQuery = graphql` |
| 744 | + query RelayReaderRelayErrorHandlingTestNoncompliantEmptyScalarFieldWithThrowOnFieldErrorQuery |
| 745 | + @throwOnFieldError { |
| 746 | + node(id: "1") { |
| 747 | + id |
| 748 | + __typename |
| 749 | + ... on User { |
| 750 | + emailAddresses |
| 751 | + } |
| 752 | + } |
| 753 | + } |
| 754 | + `; |
| 755 | + const operation = createOperationDescriptor(FooQuery, {}); |
| 756 | + const {errorResponseFields} = read(source, operation.fragment); |
| 757 | + |
| 758 | + expect(errorResponseFields).toEqual([ |
| 759 | + { |
| 760 | + fieldPath: '', |
| 761 | + handled: false, |
| 762 | + error: {message: 'There was an error!'}, |
| 763 | + kind: 'relay_field_payload.error', |
| 764 | + owner: |
| 765 | + 'RelayReaderRelayErrorHandlingTestNoncompliantEmptyScalarFieldWithThrowOnFieldErrorQuery', |
| 766 | + shouldThrow: true, |
| 767 | + }, |
| 768 | + ]); |
| 769 | + }); |
| 770 | + }); |
| 771 | + |
| 772 | + describe('when query does not have the @throwOnFieldError directive', () => { |
| 773 | + it('has errors that wont throw when the linked field is an empty list', () => { |
| 774 | + const source = RelayRecordSource.create({ |
| 775 | + '1': { |
| 776 | + __id: '1', |
| 777 | + __typename: 'User', |
| 778 | + 'friends(first:3)': { |
| 779 | + __ref: 'client:1:friends(first:3)', |
| 780 | + }, |
| 781 | + id: '1', |
| 782 | + }, |
| 783 | + 'client:1:friends(first:3)': { |
| 784 | + __id: 'client:1:friends(first:3)', |
| 785 | + __typename: 'FriendsConnection', |
| 786 | + __errors: { |
| 787 | + edges: [ |
| 788 | + { |
| 789 | + message: 'There was an error!', |
| 790 | + }, |
| 791 | + ], |
| 792 | + }, |
| 793 | + edges: { |
| 794 | + __refs: [], |
| 795 | + }, |
| 796 | + }, |
| 797 | + 'client:root': { |
| 798 | + __id: 'client:root', |
| 799 | + __typename: '__Root', |
| 800 | + 'node(id:"1")': {__ref: '1'}, |
| 801 | + }, |
| 802 | + }); |
| 803 | + |
| 804 | + const FooQuery = graphql` |
| 805 | + query RelayReaderRelayErrorHandlingTestNoncompliantEmptyLinkedFieldWithoutThrowOnFieldErrorQuery { |
| 806 | + node(id: "1") { |
| 807 | + id |
| 808 | + __typename |
| 809 | + ... on User { |
| 810 | + friends(first: 3) { |
| 811 | + edges { |
| 812 | + cursor |
| 813 | + } |
| 814 | + } |
| 815 | + } |
| 816 | + } |
| 817 | + } |
| 818 | + `; |
| 819 | + const operation = createOperationDescriptor(FooQuery, {}); |
| 820 | + const {data, errorResponseFields} = read(source, operation.fragment); |
| 821 | + |
| 822 | + expect(data.node.friends.edges).toEqual([]); |
| 823 | + expect(errorResponseFields).toEqual([ |
| 824 | + { |
| 825 | + fieldPath: '', |
| 826 | + handled: false, |
| 827 | + error: {message: 'There was an error!'}, |
| 828 | + kind: 'relay_field_payload.error', |
| 829 | + owner: |
| 830 | + 'RelayReaderRelayErrorHandlingTestNoncompliantEmptyLinkedFieldWithoutThrowOnFieldErrorQuery', |
| 831 | + shouldThrow: false, |
| 832 | + }, |
| 833 | + ]); |
| 834 | + }); |
| 835 | + |
| 836 | + it('has errors that wont throw when the scalar field is an empty list', () => { |
| 837 | + const source = RelayRecordSource.create({ |
| 838 | + '1': { |
| 839 | + __id: '1', |
| 840 | + __typename: 'User', |
| 841 | + __errors: { |
| 842 | + emailAddresses: [ |
| 843 | + { |
| 844 | + message: 'There was an error!', |
| 845 | + }, |
| 846 | + ], |
| 847 | + }, |
| 848 | + id: '1', |
| 849 | + emailAddresses: [], |
| 850 | + }, |
| 851 | + 'client:root': { |
| 852 | + __id: 'client:root', |
| 853 | + __typename: '__Root', |
| 854 | + 'node(id:"1")': {__ref: '1'}, |
| 855 | + }, |
| 856 | + }); |
| 857 | + |
| 858 | + const FooQuery = graphql` |
| 859 | + query RelayReaderRelayErrorHandlingTestNoncompliantEmptyScalarFieldWithoutThrowOnFieldErrorQuery { |
| 860 | + node(id: "1") { |
| 861 | + id |
| 862 | + __typename |
| 863 | + ... on User { |
| 864 | + emailAddresses |
| 865 | + } |
| 866 | + } |
| 867 | + } |
| 868 | + `; |
| 869 | + const operation = createOperationDescriptor(FooQuery, {}); |
| 870 | + const {data, errorResponseFields} = read(source, operation.fragment); |
| 871 | + expect(data.node.emailAddresses).toEqual([]); |
| 872 | + expect(errorResponseFields).toEqual([ |
| 873 | + { |
| 874 | + fieldPath: '', |
| 875 | + handled: false, |
| 876 | + error: {message: 'There was an error!'}, |
| 877 | + kind: 'relay_field_payload.error', |
| 878 | + owner: |
| 879 | + 'RelayReaderRelayErrorHandlingTestNoncompliantEmptyScalarFieldWithoutThrowOnFieldErrorQuery', |
| 880 | + shouldThrow: false, |
| 881 | + }, |
| 882 | + ]); |
| 883 | + }); |
| 884 | + }); |
| 885 | + }); |
638 | 886 | });
|
0 commit comments