@khanacademy/wonder-blocks-data 3.1.3 → 3.2.0
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 +6 -0
- package/dist/es/index.js +19 -3
- package/dist/index.js +20 -5
- package/package.json +1 -1
- package/src/hooks/__tests__/use-gql.test.js +1 -0
- package/src/hooks/use-gql.js +36 -23
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @khanacademy/wonder-blocks-data
|
|
2
2
|
|
|
3
|
+
## 3.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 6973afa2: useGql method now merges defaultContext and partial context by ignoring values explicitly set to undefined in the partial context. This ensures that that existing default context values are not overridden unless explicitly given a value other than undefined.
|
|
8
|
+
|
|
3
9
|
## 3.1.3
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/es/index.js
CHANGED
|
@@ -865,6 +865,10 @@ const getGqlDataFromResponse = async response => {
|
|
|
865
865
|
*
|
|
866
866
|
* The fetch function will resolve null if the request was aborted, otherwise
|
|
867
867
|
* it will resolve the data returned by the GraphQL server.
|
|
868
|
+
*
|
|
869
|
+
* Context is merged with the default context provided to the GqlRouter.
|
|
870
|
+
* Values in the partial context given to the returned fetch function will
|
|
871
|
+
* only be included if they have a value other than undefined.
|
|
868
872
|
*/
|
|
869
873
|
const useGql = () => {
|
|
870
874
|
// This hook only works if the `GqlRouter` has been used to setup context.
|
|
@@ -886,10 +890,22 @@ const useGql = () => {
|
|
|
886
890
|
const gqlFetch = useMemo(() => (operation, options = Object.freeze({})) => {
|
|
887
891
|
const {
|
|
888
892
|
variables,
|
|
889
|
-
context
|
|
890
|
-
} = options; //
|
|
893
|
+
context = {}
|
|
894
|
+
} = options; // Let's merge the partial context of the fetch with the
|
|
895
|
+
// default context. We deliberately don't spread because
|
|
896
|
+
// spreading would overwrite default context values with
|
|
897
|
+
// undefined if the partial context includes a value explicitly
|
|
898
|
+
// set to undefined. Instead, we use a map/reduce of keys.
|
|
899
|
+
|
|
900
|
+
const mergedContext = Object.keys(context).reduce((acc, key) => {
|
|
901
|
+
if (context[key] !== undefined) {
|
|
902
|
+
acc[key] = context[key];
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
return acc;
|
|
906
|
+
}, _extends({}, defaultContext)); // Invoke the fetch and extract the data.
|
|
891
907
|
|
|
892
|
-
return fetch(operation, variables,
|
|
908
|
+
return fetch(operation, variables, mergedContext).then(getGqlDataFromResponse, error => {
|
|
893
909
|
// Return null if the request was aborted.
|
|
894
910
|
// The only way to detect this reliably, it seems, is to
|
|
895
911
|
// check the error name and see if it's "AbortError" (this
|
package/dist/index.js
CHANGED
|
@@ -1032,6 +1032,10 @@ const GqlRouter = ({
|
|
|
1032
1032
|
*
|
|
1033
1033
|
* The fetch function will resolve null if the request was aborted, otherwise
|
|
1034
1034
|
* it will resolve the data returned by the GraphQL server.
|
|
1035
|
+
*
|
|
1036
|
+
* Context is merged with the default context provided to the GqlRouter.
|
|
1037
|
+
* Values in the partial context given to the returned fetch function will
|
|
1038
|
+
* only be included if they have a value other than undefined.
|
|
1035
1039
|
*/
|
|
1036
1040
|
const useGql = () => {
|
|
1037
1041
|
// This hook only works if the `GqlRouter` has been used to setup context.
|
|
@@ -1053,12 +1057,23 @@ const useGql = () => {
|
|
|
1053
1057
|
const gqlFetch = Object(react__WEBPACK_IMPORTED_MODULE_0__["useMemo"])(() => (operation, options = Object.freeze({})) => {
|
|
1054
1058
|
const {
|
|
1055
1059
|
variables,
|
|
1056
|
-
context
|
|
1057
|
-
} = options; //
|
|
1060
|
+
context = {}
|
|
1061
|
+
} = options; // Let's merge the partial context of the fetch with the
|
|
1062
|
+
// default context. We deliberately don't spread because
|
|
1063
|
+
// spreading would overwrite default context values with
|
|
1064
|
+
// undefined if the partial context includes a value explicitly
|
|
1065
|
+
// set to undefined. Instead, we use a map/reduce of keys.
|
|
1066
|
+
|
|
1067
|
+
const mergedContext = Object.keys(context).reduce((acc, key) => {
|
|
1068
|
+
if (context[key] !== undefined) {
|
|
1069
|
+
acc[key] = context[key];
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
return acc;
|
|
1073
|
+
}, { ...defaultContext
|
|
1074
|
+
}); // Invoke the fetch and extract the data.
|
|
1058
1075
|
|
|
1059
|
-
return fetch(operation, variables,
|
|
1060
|
-
...context
|
|
1061
|
-
}).then(_util_get_gql_data_from_response_js__WEBPACK_IMPORTED_MODULE_2__[/* getGqlDataFromResponse */ "a"], error => {
|
|
1076
|
+
return fetch(operation, variables, mergedContext).then(_util_get_gql_data_from_response_js__WEBPACK_IMPORTED_MODULE_2__[/* getGqlDataFromResponse */ "a"], error => {
|
|
1062
1077
|
// Return null if the request was aborted.
|
|
1063
1078
|
// The only way to detect this reliably, it seems, is to
|
|
1064
1079
|
// check the error name and see if it's "AbortError" (this
|
package/package.json
CHANGED
package/src/hooks/use-gql.js
CHANGED
|
@@ -9,7 +9,6 @@ import type {
|
|
|
9
9
|
GqlContext,
|
|
10
10
|
GqlOperation,
|
|
11
11
|
GqlFetchOptions,
|
|
12
|
-
GqlOperationType,
|
|
13
12
|
} from "../util/gql-types.js";
|
|
14
13
|
|
|
15
14
|
/**
|
|
@@ -17,13 +16,12 @@ import type {
|
|
|
17
16
|
*
|
|
18
17
|
* The fetch function will resolve null if the request was aborted, otherwise
|
|
19
18
|
* it will resolve the data returned by the GraphQL server.
|
|
19
|
+
*
|
|
20
|
+
* Context is merged with the default context provided to the GqlRouter.
|
|
21
|
+
* Values in the partial context given to the returned fetch function will
|
|
22
|
+
* only be included if they have a value other than undefined.
|
|
20
23
|
*/
|
|
21
|
-
export const useGql = (): (<
|
|
22
|
-
TType: GqlOperationType,
|
|
23
|
-
TData,
|
|
24
|
-
TVariables: {...},
|
|
25
|
-
TContext: GqlContext,
|
|
26
|
-
>(
|
|
24
|
+
export const useGql = (): (<TData, TVariables: {...}, TContext: GqlContext>(
|
|
27
25
|
operation: GqlOperation<TData, TVariables>,
|
|
28
26
|
options?: GqlFetchOptions<TVariables, TContext>,
|
|
29
27
|
) => Promise<?TData>) => {
|
|
@@ -47,24 +45,39 @@ export const useGql = (): (<
|
|
|
47
45
|
{},
|
|
48
46
|
),
|
|
49
47
|
) => {
|
|
50
|
-
const {variables, context} = options;
|
|
48
|
+
const {variables, context = {}} = options;
|
|
49
|
+
|
|
50
|
+
// Let's merge the partial context of the fetch with the
|
|
51
|
+
// default context. We deliberately don't spread because
|
|
52
|
+
// spreading would overwrite default context values with
|
|
53
|
+
// undefined if the partial context includes a value explicitly
|
|
54
|
+
// set to undefined. Instead, we use a map/reduce of keys.
|
|
55
|
+
const mergedContext = Object.keys(context).reduce(
|
|
56
|
+
(acc, key) => {
|
|
57
|
+
if (context[key] !== undefined) {
|
|
58
|
+
acc[key] = context[key];
|
|
59
|
+
}
|
|
60
|
+
return acc;
|
|
61
|
+
},
|
|
62
|
+
{...defaultContext},
|
|
63
|
+
);
|
|
51
64
|
|
|
52
65
|
// Invoke the fetch and extract the data.
|
|
53
|
-
return fetch(operation, variables,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
return fetch(operation, variables, mergedContext).then(
|
|
67
|
+
getGqlDataFromResponse,
|
|
68
|
+
(error) => {
|
|
69
|
+
// Return null if the request was aborted.
|
|
70
|
+
// The only way to detect this reliably, it seems, is to
|
|
71
|
+
// check the error name and see if it's "AbortError" (this
|
|
72
|
+
// is also what Apollo does).
|
|
73
|
+
// Even then, it's reliant on the fetch supporting aborts.
|
|
74
|
+
if (error.name === "AbortError") {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
// Need to make sure we pass other errors along.
|
|
78
|
+
throw error;
|
|
79
|
+
},
|
|
80
|
+
);
|
|
68
81
|
},
|
|
69
82
|
[fetch, defaultContext],
|
|
70
83
|
);
|