@khanacademy/wonder-blocks-data 8.0.2 → 8.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @khanacademy/wonder-blocks-data
2
2
 
3
+ ## 8.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 08238b89: Export the `getGqlDataFromResponse` method so we can use it in custom `gqlFetch` scenarios for a consistent experience
8
+
9
+ ## 8.0.4
10
+
11
+ ### Patch Changes
12
+
13
+ - dc2e00f4: Do not fetch if FetchPolicy is CacheBeforeNetwork and there is already a cached value, even if the requestId changes
14
+
15
+ ## 8.0.3
16
+
17
+ ### Patch Changes
18
+
19
+ - 5f4a4297: Make dependency on `flow-enums-runtime` explicit
20
+ - 2b96fd59: Change flow-enums-runtime to be peer dependencies
21
+ - Updated dependencies [5f4a4297]
22
+ - Updated dependencies [2b96fd59]
23
+ - @khanacademy/wonder-blocks-core@4.3.2
24
+
3
25
  ## 8.0.2
4
26
 
5
27
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -585,7 +585,6 @@ const useCachedEffect = (requestId, handler, options = {}) => {
585
585
 
586
586
  return fetchFn;
587
587
  }, [requestId, onResultChanged, forceUpdate, setMostRecentResult, fetchPolicy]);
588
- const requestIdRef = React.useRef(requestId);
589
588
  const shouldFetch = React.useMemo(() => {
590
589
  if (hardSkip) {
591
590
  return false;
@@ -596,14 +595,13 @@ const useCachedEffect = (requestId, handler, options = {}) => {
596
595
  return false;
597
596
 
598
597
  case FetchPolicy.CacheBeforeNetwork:
599
- return mostRecentResult == null || requestId !== requestIdRef.current;
598
+ return mostRecentResult == null;
600
599
 
601
600
  case FetchPolicy.CacheAndNetwork:
602
601
  case FetchPolicy.NetworkOnly:
603
602
  return networkResultRef.current == null;
604
603
  }
605
- }, [requestId, mostRecentResult, fetchPolicy, hardSkip]);
606
- requestIdRef.current = requestId;
604
+ }, [mostRecentResult, fetchPolicy, hardSkip]);
607
605
  React.useEffect(() => {
608
606
  if (!shouldFetch) {
609
607
  return;
@@ -721,6 +719,71 @@ const getGqlRequestId = (operation, variables, context) => {
721
719
  return parts.join("|");
722
720
  };
723
721
 
722
+ const GqlErrors = Object.freeze({
723
+ Internal: "Internal",
724
+ BadResponse: "BadResponse",
725
+ ErrorResult: "ErrorResult"
726
+ });
727
+ class GqlError extends KindError {
728
+ constructor(message, kind, {
729
+ metadata,
730
+ cause
731
+ } = {}) {
732
+ super(message, kind, {
733
+ metadata,
734
+ cause,
735
+ name: "Gql"
736
+ });
737
+ }
738
+
739
+ }
740
+
741
+ const getGqlDataFromResponse = async response => {
742
+ const bodyText = await response.text();
743
+ let result;
744
+
745
+ try {
746
+ result = JSON.parse(bodyText);
747
+ } catch (e) {
748
+ throw new DataError("Failed to parse response", DataErrors.Parse, {
749
+ metadata: {
750
+ statusCode: response.status,
751
+ bodyText
752
+ },
753
+ cause: e
754
+ });
755
+ }
756
+
757
+ if (response.status >= 300) {
758
+ throw new DataError("Response unsuccessful", DataErrors.Network, {
759
+ metadata: {
760
+ statusCode: response.status,
761
+ result
762
+ }
763
+ });
764
+ }
765
+
766
+ if (!Object.prototype.hasOwnProperty.call(result, "data") && !Object.prototype.hasOwnProperty.call(result, "errors")) {
767
+ throw new GqlError("Server response missing", GqlErrors.BadResponse, {
768
+ metadata: {
769
+ statusCode: response.status,
770
+ result
771
+ }
772
+ });
773
+ }
774
+
775
+ if (result.errors != null && Array.isArray(result.errors) && result.errors.length > 0) {
776
+ throw new GqlError("GraphQL errors", GqlErrors.ErrorResult, {
777
+ metadata: {
778
+ statusCode: response.status,
779
+ result
780
+ }
781
+ });
782
+ }
783
+
784
+ return result.data;
785
+ };
786
+
724
787
  const DocumentTypes = Object.freeze({
725
788
  query: "query",
726
789
  mutation: "mutation"
@@ -825,25 +888,6 @@ const mergeGqlContext = (defaultContext, overrides) => {
825
888
  }, _extends({}, defaultContext));
826
889
  };
827
890
 
828
- const GqlErrors = Object.freeze({
829
- Internal: "Internal",
830
- BadResponse: "BadResponse",
831
- ErrorResult: "ErrorResult"
832
- });
833
- class GqlError extends KindError {
834
- constructor(message, kind, {
835
- metadata,
836
- cause
837
- } = {}) {
838
- super(message, kind, {
839
- metadata,
840
- cause,
841
- name: "Gql"
842
- });
843
- }
844
-
845
- }
846
-
847
891
  const useGqlRouterContext = (contextOverrides = {}) => {
848
892
  const gqlRouterContext = useContext(GqlRouterContext);
849
893
 
@@ -873,52 +917,6 @@ const useGqlRouterContext = (contextOverrides = {}) => {
873
917
  return finalRouterContext;
874
918
  };
875
919
 
876
- const getGqlDataFromResponse = async response => {
877
- const bodyText = await response.text();
878
- let result;
879
-
880
- try {
881
- result = JSON.parse(bodyText);
882
- } catch (e) {
883
- throw new DataError("Failed to parse response", DataErrors.Parse, {
884
- metadata: {
885
- statusCode: response.status,
886
- bodyText
887
- },
888
- cause: e
889
- });
890
- }
891
-
892
- if (response.status >= 300) {
893
- throw new DataError("Response unsuccessful", DataErrors.Network, {
894
- metadata: {
895
- statusCode: response.status,
896
- result
897
- }
898
- });
899
- }
900
-
901
- if (!Object.prototype.hasOwnProperty.call(result, "data") && !Object.prototype.hasOwnProperty.call(result, "errors")) {
902
- throw new GqlError("Server response missing", GqlErrors.BadResponse, {
903
- metadata: {
904
- statusCode: response.status,
905
- result
906
- }
907
- });
908
- }
909
-
910
- if (result.errors != null && Array.isArray(result.errors) && result.errors.length > 0) {
911
- throw new GqlError("GraphQL errors", GqlErrors.ErrorResult, {
912
- metadata: {
913
- statusCode: response.status,
914
- result
915
- }
916
- });
917
- }
918
-
919
- return result.data;
920
- };
921
-
922
920
  const useGql = (context = {}) => {
923
921
  const gqlRouterContext = useGqlRouterContext(context);
924
922
  const gqlFetch = useCallback((operation, options = Object.freeze({})) => {
@@ -936,4 +934,4 @@ const useGql = (context = {}) => {
936
934
  return gqlFetch;
937
935
  };
938
936
 
939
- export { Data, DataError, DataErrors, FetchPolicy, GqlError, GqlErrors, GqlRouter, InterceptRequests, ScopedInMemoryCache, SerializableInMemoryCache, Status, TrackData, WhenClientSide, abortInflightRequests, fetchTrackedRequests, getGqlRequestId, graphQLDocumentNodeParser, hasTrackedRequestsToBeFetched, initializeHydrationCache, purgeCaches, purgeHydrationCache, purgeSharedCache, toGqlOperation, useCachedEffect, useGql, useHydratableEffect, useServerEffect, useSharedCache };
937
+ export { Data, DataError, DataErrors, FetchPolicy, GqlError, GqlErrors, GqlRouter, InterceptRequests, ScopedInMemoryCache, SerializableInMemoryCache, Status, TrackData, WhenClientSide, abortInflightRequests, fetchTrackedRequests, getGqlDataFromResponse, getGqlRequestId, graphQLDocumentNodeParser, hasTrackedRequestsToBeFetched, initializeHydrationCache, purgeCaches, purgeHydrationCache, purgeSharedCache, toGqlOperation, useCachedEffect, useGql, useHydratableEffect, useServerEffect, useSharedCache };