@merkur/plugin-graphql-client 0.37.9 → 0.39.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/README.md +1 -1
- package/lib/index.cjs +51 -78
- package/lib/index.es9.mjs +1 -1
- package/lib/index.js +51 -78
- package/lib/index.mjs +52 -79
- package/lib/index.umd.js +1 -1
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<a href="https://merkur.js.org/docs/getting-started" title="Getting started">
|
|
3
|
-
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-
|
|
3
|
+
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-logo.png" width="100px" height="100px" alt="Merkur illustration"/>
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
6
|
|
package/lib/index.cjs
CHANGED
|
@@ -12,139 +12,120 @@ const TYPENAME_FIELD = {
|
|
|
12
12
|
kind: 'Field',
|
|
13
13
|
name: {
|
|
14
14
|
kind: 'Name',
|
|
15
|
-
value: '__typename'
|
|
16
|
-
}
|
|
15
|
+
value: '__typename'
|
|
16
|
+
}
|
|
17
17
|
};
|
|
18
|
-
|
|
19
18
|
const DEV = 'development';
|
|
20
|
-
const ENV =
|
|
21
|
-
typeof process !== 'undefined' && process && process.env
|
|
22
|
-
? process.env.NODE_ENV
|
|
23
|
-
: DEV;
|
|
24
|
-
|
|
19
|
+
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
25
20
|
function setEndpointUrl(widget, url, name = 'graphql') {
|
|
26
21
|
widget.$in.graphqlClient[name].endpointUrl = url;
|
|
27
22
|
}
|
|
28
|
-
|
|
29
23
|
function setEntityClasses(widget, entityClasses, name = 'graphql') {
|
|
30
|
-
widget.$in.graphqlClient[name].entityClasses =
|
|
31
|
-
buildTypeToEntityMap(entityClasses);
|
|
24
|
+
widget.$in.graphqlClient[name].entityClasses = buildTypeToEntityMap(entityClasses);
|
|
32
25
|
}
|
|
33
|
-
|
|
34
26
|
function graphqlClientPlugin(name = 'graphql') {
|
|
35
27
|
return {
|
|
36
28
|
async setup(widget) {
|
|
37
29
|
core.assignMissingKeys(widget, graphqlClientAPI(name));
|
|
38
|
-
|
|
39
30
|
if (!widget.$in.graphqlClient) widget.$in.graphqlClient = {};
|
|
40
|
-
|
|
41
31
|
widget.$in.graphqlClient[name] = {
|
|
42
32
|
endpointUrl: '',
|
|
43
|
-
entityClasses: {}
|
|
33
|
+
entityClasses: {}
|
|
44
34
|
};
|
|
45
|
-
|
|
46
35
|
return widget;
|
|
47
36
|
},
|
|
48
37
|
async create(widget) {
|
|
49
38
|
if (ENV === DEV && !widget.$in.httpClient) {
|
|
50
|
-
throw new Error(
|
|
51
|
-
'You must install missing plugin: npm i @merkur/plugin-http-client',
|
|
52
|
-
);
|
|
39
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-http-client');
|
|
53
40
|
}
|
|
54
|
-
|
|
55
41
|
core.bindWidgetToFunctions(widget, widget[name]);
|
|
56
|
-
|
|
57
42
|
return widget;
|
|
58
|
-
}
|
|
43
|
+
}
|
|
59
44
|
};
|
|
60
45
|
}
|
|
61
|
-
|
|
62
46
|
function graphqlClientAPI(name = 'graphql') {
|
|
63
47
|
return {
|
|
64
48
|
[name]: {
|
|
65
49
|
async request(widget, operation, variables = {}, options = {}) {
|
|
66
|
-
const {
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
const {
|
|
51
|
+
endpointUrl,
|
|
52
|
+
entityClasses
|
|
53
|
+
} = widget.$in.graphqlClient[name];
|
|
54
|
+
const {
|
|
55
|
+
headers = {},
|
|
56
|
+
body = {},
|
|
57
|
+
...restOptions
|
|
58
|
+
} = options;
|
|
69
59
|
operation = addTypenameToSelections(operation);
|
|
70
|
-
|
|
71
|
-
|
|
60
|
+
const {
|
|
61
|
+
response
|
|
62
|
+
} = await widget.http.request({
|
|
72
63
|
url: endpointUrl,
|
|
73
64
|
method: 'POST',
|
|
74
65
|
headers: {
|
|
75
66
|
'Content-Type': 'application/json',
|
|
76
|
-
...headers
|
|
67
|
+
...headers
|
|
77
68
|
},
|
|
78
69
|
body: {
|
|
79
70
|
query: graphql.stripIgnoredCharacters(graphql.print(operation)),
|
|
80
71
|
variables,
|
|
81
|
-
...body
|
|
72
|
+
...body
|
|
82
73
|
},
|
|
83
|
-
...restOptions
|
|
74
|
+
...restOptions
|
|
84
75
|
});
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
const {
|
|
77
|
+
errors,
|
|
78
|
+
data = {}
|
|
79
|
+
} = response.body;
|
|
87
80
|
if (errors) {
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
new UnauthorizedError(`Unauthorized Error`, { errors, data }),
|
|
94
|
-
);
|
|
81
|
+
if (Array.isArray(errors) && errors.some(error => error.status === 'unauthorized')) {
|
|
82
|
+
return Promise.reject(new UnauthorizedError(`Unauthorized Error`, {
|
|
83
|
+
errors,
|
|
84
|
+
data
|
|
85
|
+
}));
|
|
95
86
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
);
|
|
87
|
+
return Promise.reject(new GraphQLError(`Api Error`, {
|
|
88
|
+
errors,
|
|
89
|
+
data
|
|
90
|
+
}));
|
|
100
91
|
}
|
|
101
|
-
|
|
102
92
|
return processResponseData(data, entityClasses);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
105
95
|
};
|
|
106
96
|
}
|
|
107
|
-
|
|
108
97
|
function buildTypeToEntityMap(entityClasses) {
|
|
109
98
|
const map = {};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
99
|
+
entityClasses.forEach(entityClass => {
|
|
100
|
+
let {
|
|
101
|
+
entityType
|
|
102
|
+
} = entityClass;
|
|
114
103
|
if (!Array.isArray(entityType)) {
|
|
115
104
|
entityType = [entityType];
|
|
116
105
|
}
|
|
117
|
-
|
|
118
|
-
entityType.forEach((type) => (map[type] = entityClass));
|
|
106
|
+
entityType.forEach(type => map[type] = entityClass);
|
|
119
107
|
});
|
|
120
|
-
|
|
121
108
|
return map;
|
|
122
109
|
}
|
|
123
|
-
|
|
124
110
|
function processResponseData(data, entityClasses) {
|
|
125
111
|
const processedData = {};
|
|
126
|
-
|
|
127
112
|
if (typeof data === 'string') {
|
|
128
113
|
return data;
|
|
129
114
|
}
|
|
130
115
|
Object.entries(data).forEach(([field, value]) => {
|
|
131
116
|
if (Array.isArray(value)) {
|
|
132
|
-
value = value.map(
|
|
117
|
+
value = value.map(node => processResponseData(node, entityClasses));
|
|
133
118
|
} else if (typeof value === 'object' && value !== null) {
|
|
134
119
|
value = processResponseData(value, entityClasses);
|
|
135
120
|
}
|
|
136
|
-
|
|
137
121
|
processedData[field] = value;
|
|
138
122
|
});
|
|
139
|
-
|
|
140
123
|
const type = processedData.__typename;
|
|
141
124
|
if (!type || !entityClasses[type]) {
|
|
142
125
|
return processedData;
|
|
143
126
|
}
|
|
144
|
-
|
|
145
127
|
return Reflect.construct(entityClasses[type], [processedData]);
|
|
146
128
|
}
|
|
147
|
-
|
|
148
129
|
function addTypenameToSelections(document) {
|
|
149
130
|
return graphql.visit(document, {
|
|
150
131
|
SelectionSet: {
|
|
@@ -155,40 +136,32 @@ function addTypenameToSelections(document) {
|
|
|
155
136
|
}
|
|
156
137
|
|
|
157
138
|
// No changes if no selections.
|
|
158
|
-
const {
|
|
139
|
+
const {
|
|
140
|
+
selections
|
|
141
|
+
} = node;
|
|
159
142
|
if (!selections) {
|
|
160
143
|
return;
|
|
161
144
|
}
|
|
162
145
|
|
|
163
146
|
// If selections already have a __typename, or are part of an
|
|
164
147
|
// introspection query, do nothing.
|
|
165
|
-
const skip = selections.some(
|
|
166
|
-
(selection) =>
|
|
167
|
-
selection.kind === 'Field' &&
|
|
168
|
-
(selection.name.value === '__typename' ||
|
|
169
|
-
selection.name.value.lastIndexOf('__', 0) === 0),
|
|
170
|
-
);
|
|
171
|
-
|
|
148
|
+
const skip = selections.some(selection => selection.kind === 'Field' && (selection.name.value === '__typename' || selection.name.value.lastIndexOf('__', 0) === 0));
|
|
172
149
|
if (skip) {
|
|
173
150
|
return;
|
|
174
151
|
}
|
|
175
152
|
|
|
176
153
|
// If this SelectionSet is @export-ed as an input variable, it should
|
|
177
154
|
// not have a __typename field (see issue #4691).
|
|
178
|
-
if (
|
|
179
|
-
parent.kind === 'Field' &&
|
|
180
|
-
parent.directives &&
|
|
181
|
-
parent.directives.some((d) => d.name.value === 'export')
|
|
182
|
-
) {
|
|
155
|
+
if (parent.kind === 'Field' && parent.directives && parent.directives.some(d => d.name.value === 'export')) {
|
|
183
156
|
return;
|
|
184
157
|
}
|
|
185
158
|
|
|
186
159
|
// Create and return a new SelectionSet with a __typename Field.
|
|
187
160
|
return Object.assign({}, node, {
|
|
188
|
-
selections: [...selections, TYPENAME_FIELD]
|
|
161
|
+
selections: [...selections, TYPENAME_FIELD]
|
|
189
162
|
});
|
|
190
|
-
}
|
|
191
|
-
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
192
165
|
});
|
|
193
166
|
}
|
|
194
167
|
|
package/lib/index.es9.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { bindWidgetToFunctions, assignMissingKeys } from '@merkur/core';
|
|
2
2
|
import { stripIgnoredCharacters, print, visit } from 'graphql';
|
|
3
3
|
import { GenericError } from '@merkur/plugin-error';
|
|
4
4
|
class GraphQLError extends GenericError {}
|
package/lib/index.js
CHANGED
|
@@ -12,139 +12,120 @@ const TYPENAME_FIELD = {
|
|
|
12
12
|
kind: 'Field',
|
|
13
13
|
name: {
|
|
14
14
|
kind: 'Name',
|
|
15
|
-
value: '__typename'
|
|
16
|
-
}
|
|
15
|
+
value: '__typename'
|
|
16
|
+
}
|
|
17
17
|
};
|
|
18
|
-
|
|
19
18
|
const DEV = 'development';
|
|
20
|
-
const ENV =
|
|
21
|
-
typeof process !== 'undefined' && process && process.env
|
|
22
|
-
? process.env.NODE_ENV
|
|
23
|
-
: DEV;
|
|
24
|
-
|
|
19
|
+
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
25
20
|
function setEndpointUrl(widget, url, name = 'graphql') {
|
|
26
21
|
widget.$in.graphqlClient[name].endpointUrl = url;
|
|
27
22
|
}
|
|
28
|
-
|
|
29
23
|
function setEntityClasses(widget, entityClasses, name = 'graphql') {
|
|
30
|
-
widget.$in.graphqlClient[name].entityClasses =
|
|
31
|
-
buildTypeToEntityMap(entityClasses);
|
|
24
|
+
widget.$in.graphqlClient[name].entityClasses = buildTypeToEntityMap(entityClasses);
|
|
32
25
|
}
|
|
33
|
-
|
|
34
26
|
function graphqlClientPlugin(name = 'graphql') {
|
|
35
27
|
return {
|
|
36
28
|
async setup(widget) {
|
|
37
29
|
core.assignMissingKeys(widget, graphqlClientAPI(name));
|
|
38
|
-
|
|
39
30
|
if (!widget.$in.graphqlClient) widget.$in.graphqlClient = {};
|
|
40
|
-
|
|
41
31
|
widget.$in.graphqlClient[name] = {
|
|
42
32
|
endpointUrl: '',
|
|
43
|
-
entityClasses: {}
|
|
33
|
+
entityClasses: {}
|
|
44
34
|
};
|
|
45
|
-
|
|
46
35
|
return widget;
|
|
47
36
|
},
|
|
48
37
|
async create(widget) {
|
|
49
38
|
if (ENV === DEV && !widget.$in.httpClient) {
|
|
50
|
-
throw new Error(
|
|
51
|
-
'You must install missing plugin: npm i @merkur/plugin-http-client',
|
|
52
|
-
);
|
|
39
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-http-client');
|
|
53
40
|
}
|
|
54
|
-
|
|
55
41
|
core.bindWidgetToFunctions(widget, widget[name]);
|
|
56
|
-
|
|
57
42
|
return widget;
|
|
58
|
-
}
|
|
43
|
+
}
|
|
59
44
|
};
|
|
60
45
|
}
|
|
61
|
-
|
|
62
46
|
function graphqlClientAPI(name = 'graphql') {
|
|
63
47
|
return {
|
|
64
48
|
[name]: {
|
|
65
49
|
async request(widget, operation, variables = {}, options = {}) {
|
|
66
|
-
const {
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
const {
|
|
51
|
+
endpointUrl,
|
|
52
|
+
entityClasses
|
|
53
|
+
} = widget.$in.graphqlClient[name];
|
|
54
|
+
const {
|
|
55
|
+
headers = {},
|
|
56
|
+
body = {},
|
|
57
|
+
...restOptions
|
|
58
|
+
} = options;
|
|
69
59
|
operation = addTypenameToSelections(operation);
|
|
70
|
-
|
|
71
|
-
|
|
60
|
+
const {
|
|
61
|
+
response
|
|
62
|
+
} = await widget.http.request({
|
|
72
63
|
url: endpointUrl,
|
|
73
64
|
method: 'POST',
|
|
74
65
|
headers: {
|
|
75
66
|
'Content-Type': 'application/json',
|
|
76
|
-
...headers
|
|
67
|
+
...headers
|
|
77
68
|
},
|
|
78
69
|
body: {
|
|
79
70
|
query: graphql.stripIgnoredCharacters(graphql.print(operation)),
|
|
80
71
|
variables,
|
|
81
|
-
...body
|
|
72
|
+
...body
|
|
82
73
|
},
|
|
83
|
-
...restOptions
|
|
74
|
+
...restOptions
|
|
84
75
|
});
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
const {
|
|
77
|
+
errors,
|
|
78
|
+
data = {}
|
|
79
|
+
} = response.body;
|
|
87
80
|
if (errors) {
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
new UnauthorizedError(`Unauthorized Error`, { errors, data }),
|
|
94
|
-
);
|
|
81
|
+
if (Array.isArray(errors) && errors.some(error => error.status === 'unauthorized')) {
|
|
82
|
+
return Promise.reject(new UnauthorizedError(`Unauthorized Error`, {
|
|
83
|
+
errors,
|
|
84
|
+
data
|
|
85
|
+
}));
|
|
95
86
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
);
|
|
87
|
+
return Promise.reject(new GraphQLError(`Api Error`, {
|
|
88
|
+
errors,
|
|
89
|
+
data
|
|
90
|
+
}));
|
|
100
91
|
}
|
|
101
|
-
|
|
102
92
|
return processResponseData(data, entityClasses);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
105
95
|
};
|
|
106
96
|
}
|
|
107
|
-
|
|
108
97
|
function buildTypeToEntityMap(entityClasses) {
|
|
109
98
|
const map = {};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
99
|
+
entityClasses.forEach(entityClass => {
|
|
100
|
+
let {
|
|
101
|
+
entityType
|
|
102
|
+
} = entityClass;
|
|
114
103
|
if (!Array.isArray(entityType)) {
|
|
115
104
|
entityType = [entityType];
|
|
116
105
|
}
|
|
117
|
-
|
|
118
|
-
entityType.forEach((type) => (map[type] = entityClass));
|
|
106
|
+
entityType.forEach(type => map[type] = entityClass);
|
|
119
107
|
});
|
|
120
|
-
|
|
121
108
|
return map;
|
|
122
109
|
}
|
|
123
|
-
|
|
124
110
|
function processResponseData(data, entityClasses) {
|
|
125
111
|
const processedData = {};
|
|
126
|
-
|
|
127
112
|
if (typeof data === 'string') {
|
|
128
113
|
return data;
|
|
129
114
|
}
|
|
130
115
|
Object.entries(data).forEach(([field, value]) => {
|
|
131
116
|
if (Array.isArray(value)) {
|
|
132
|
-
value = value.map(
|
|
117
|
+
value = value.map(node => processResponseData(node, entityClasses));
|
|
133
118
|
} else if (typeof value === 'object' && value !== null) {
|
|
134
119
|
value = processResponseData(value, entityClasses);
|
|
135
120
|
}
|
|
136
|
-
|
|
137
121
|
processedData[field] = value;
|
|
138
122
|
});
|
|
139
|
-
|
|
140
123
|
const type = processedData.__typename;
|
|
141
124
|
if (!type || !entityClasses[type]) {
|
|
142
125
|
return processedData;
|
|
143
126
|
}
|
|
144
|
-
|
|
145
127
|
return Reflect.construct(entityClasses[type], [processedData]);
|
|
146
128
|
}
|
|
147
|
-
|
|
148
129
|
function addTypenameToSelections(document) {
|
|
149
130
|
return graphql.visit(document, {
|
|
150
131
|
SelectionSet: {
|
|
@@ -155,40 +136,32 @@ function addTypenameToSelections(document) {
|
|
|
155
136
|
}
|
|
156
137
|
|
|
157
138
|
// No changes if no selections.
|
|
158
|
-
const {
|
|
139
|
+
const {
|
|
140
|
+
selections
|
|
141
|
+
} = node;
|
|
159
142
|
if (!selections) {
|
|
160
143
|
return;
|
|
161
144
|
}
|
|
162
145
|
|
|
163
146
|
// If selections already have a __typename, or are part of an
|
|
164
147
|
// introspection query, do nothing.
|
|
165
|
-
const skip = selections.some(
|
|
166
|
-
(selection) =>
|
|
167
|
-
selection.kind === 'Field' &&
|
|
168
|
-
(selection.name.value === '__typename' ||
|
|
169
|
-
selection.name.value.lastIndexOf('__', 0) === 0),
|
|
170
|
-
);
|
|
171
|
-
|
|
148
|
+
const skip = selections.some(selection => selection.kind === 'Field' && (selection.name.value === '__typename' || selection.name.value.lastIndexOf('__', 0) === 0));
|
|
172
149
|
if (skip) {
|
|
173
150
|
return;
|
|
174
151
|
}
|
|
175
152
|
|
|
176
153
|
// If this SelectionSet is @export-ed as an input variable, it should
|
|
177
154
|
// not have a __typename field (see issue #4691).
|
|
178
|
-
if (
|
|
179
|
-
parent.kind === 'Field' &&
|
|
180
|
-
parent.directives &&
|
|
181
|
-
parent.directives.some((d) => d.name.value === 'export')
|
|
182
|
-
) {
|
|
155
|
+
if (parent.kind === 'Field' && parent.directives && parent.directives.some(d => d.name.value === 'export')) {
|
|
183
156
|
return;
|
|
184
157
|
}
|
|
185
158
|
|
|
186
159
|
// Create and return a new SelectionSet with a __typename Field.
|
|
187
160
|
return Object.assign({}, node, {
|
|
188
|
-
selections: [...selections, TYPENAME_FIELD]
|
|
161
|
+
selections: [...selections, TYPENAME_FIELD]
|
|
189
162
|
});
|
|
190
|
-
}
|
|
191
|
-
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
192
165
|
});
|
|
193
166
|
}
|
|
194
167
|
|
package/lib/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { bindWidgetToFunctions, assignMissingKeys } from '@merkur/core';
|
|
2
2
|
import { stripIgnoredCharacters, print, visit } from 'graphql';
|
|
3
3
|
import { GenericError } from '@merkur/plugin-error';
|
|
4
4
|
|
|
@@ -10,139 +10,120 @@ const TYPENAME_FIELD = {
|
|
|
10
10
|
kind: 'Field',
|
|
11
11
|
name: {
|
|
12
12
|
kind: 'Name',
|
|
13
|
-
value: '__typename'
|
|
14
|
-
}
|
|
13
|
+
value: '__typename'
|
|
14
|
+
}
|
|
15
15
|
};
|
|
16
|
-
|
|
17
16
|
const DEV = 'development';
|
|
18
|
-
const ENV =
|
|
19
|
-
typeof process !== 'undefined' && process && process.env
|
|
20
|
-
? process.env.NODE_ENV
|
|
21
|
-
: DEV;
|
|
22
|
-
|
|
17
|
+
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
23
18
|
function setEndpointUrl(widget, url, name = 'graphql') {
|
|
24
19
|
widget.$in.graphqlClient[name].endpointUrl = url;
|
|
25
20
|
}
|
|
26
|
-
|
|
27
21
|
function setEntityClasses(widget, entityClasses, name = 'graphql') {
|
|
28
|
-
widget.$in.graphqlClient[name].entityClasses =
|
|
29
|
-
buildTypeToEntityMap(entityClasses);
|
|
22
|
+
widget.$in.graphqlClient[name].entityClasses = buildTypeToEntityMap(entityClasses);
|
|
30
23
|
}
|
|
31
|
-
|
|
32
24
|
function graphqlClientPlugin(name = 'graphql') {
|
|
33
25
|
return {
|
|
34
26
|
async setup(widget) {
|
|
35
27
|
assignMissingKeys(widget, graphqlClientAPI(name));
|
|
36
|
-
|
|
37
28
|
if (!widget.$in.graphqlClient) widget.$in.graphqlClient = {};
|
|
38
|
-
|
|
39
29
|
widget.$in.graphqlClient[name] = {
|
|
40
30
|
endpointUrl: '',
|
|
41
|
-
entityClasses: {}
|
|
31
|
+
entityClasses: {}
|
|
42
32
|
};
|
|
43
|
-
|
|
44
33
|
return widget;
|
|
45
34
|
},
|
|
46
35
|
async create(widget) {
|
|
47
36
|
if (ENV === DEV && !widget.$in.httpClient) {
|
|
48
|
-
throw new Error(
|
|
49
|
-
'You must install missing plugin: npm i @merkur/plugin-http-client',
|
|
50
|
-
);
|
|
37
|
+
throw new Error('You must install missing plugin: npm i @merkur/plugin-http-client');
|
|
51
38
|
}
|
|
52
|
-
|
|
53
39
|
bindWidgetToFunctions(widget, widget[name]);
|
|
54
|
-
|
|
55
40
|
return widget;
|
|
56
|
-
}
|
|
41
|
+
}
|
|
57
42
|
};
|
|
58
43
|
}
|
|
59
|
-
|
|
60
44
|
function graphqlClientAPI(name = 'graphql') {
|
|
61
45
|
return {
|
|
62
46
|
[name]: {
|
|
63
47
|
async request(widget, operation, variables = {}, options = {}) {
|
|
64
|
-
const {
|
|
65
|
-
|
|
66
|
-
|
|
48
|
+
const {
|
|
49
|
+
endpointUrl,
|
|
50
|
+
entityClasses
|
|
51
|
+
} = widget.$in.graphqlClient[name];
|
|
52
|
+
const {
|
|
53
|
+
headers = {},
|
|
54
|
+
body = {},
|
|
55
|
+
...restOptions
|
|
56
|
+
} = options;
|
|
67
57
|
operation = addTypenameToSelections(operation);
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
const {
|
|
59
|
+
response
|
|
60
|
+
} = await widget.http.request({
|
|
70
61
|
url: endpointUrl,
|
|
71
62
|
method: 'POST',
|
|
72
63
|
headers: {
|
|
73
64
|
'Content-Type': 'application/json',
|
|
74
|
-
...headers
|
|
65
|
+
...headers
|
|
75
66
|
},
|
|
76
67
|
body: {
|
|
77
68
|
query: stripIgnoredCharacters(print(operation)),
|
|
78
69
|
variables,
|
|
79
|
-
...body
|
|
70
|
+
...body
|
|
80
71
|
},
|
|
81
|
-
...restOptions
|
|
72
|
+
...restOptions
|
|
82
73
|
});
|
|
83
|
-
|
|
84
|
-
|
|
74
|
+
const {
|
|
75
|
+
errors,
|
|
76
|
+
data = {}
|
|
77
|
+
} = response.body;
|
|
85
78
|
if (errors) {
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
new UnauthorizedError(`Unauthorized Error`, { errors, data }),
|
|
92
|
-
);
|
|
79
|
+
if (Array.isArray(errors) && errors.some(error => error.status === 'unauthorized')) {
|
|
80
|
+
return Promise.reject(new UnauthorizedError(`Unauthorized Error`, {
|
|
81
|
+
errors,
|
|
82
|
+
data
|
|
83
|
+
}));
|
|
93
84
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
);
|
|
85
|
+
return Promise.reject(new GraphQLError(`Api Error`, {
|
|
86
|
+
errors,
|
|
87
|
+
data
|
|
88
|
+
}));
|
|
98
89
|
}
|
|
99
|
-
|
|
100
90
|
return processResponseData(data, entityClasses);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
103
93
|
};
|
|
104
94
|
}
|
|
105
|
-
|
|
106
95
|
function buildTypeToEntityMap(entityClasses) {
|
|
107
96
|
const map = {};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
97
|
+
entityClasses.forEach(entityClass => {
|
|
98
|
+
let {
|
|
99
|
+
entityType
|
|
100
|
+
} = entityClass;
|
|
112
101
|
if (!Array.isArray(entityType)) {
|
|
113
102
|
entityType = [entityType];
|
|
114
103
|
}
|
|
115
|
-
|
|
116
|
-
entityType.forEach((type) => (map[type] = entityClass));
|
|
104
|
+
entityType.forEach(type => map[type] = entityClass);
|
|
117
105
|
});
|
|
118
|
-
|
|
119
106
|
return map;
|
|
120
107
|
}
|
|
121
|
-
|
|
122
108
|
function processResponseData(data, entityClasses) {
|
|
123
109
|
const processedData = {};
|
|
124
|
-
|
|
125
110
|
if (typeof data === 'string') {
|
|
126
111
|
return data;
|
|
127
112
|
}
|
|
128
113
|
Object.entries(data).forEach(([field, value]) => {
|
|
129
114
|
if (Array.isArray(value)) {
|
|
130
|
-
value = value.map(
|
|
115
|
+
value = value.map(node => processResponseData(node, entityClasses));
|
|
131
116
|
} else if (typeof value === 'object' && value !== null) {
|
|
132
117
|
value = processResponseData(value, entityClasses);
|
|
133
118
|
}
|
|
134
|
-
|
|
135
119
|
processedData[field] = value;
|
|
136
120
|
});
|
|
137
|
-
|
|
138
121
|
const type = processedData.__typename;
|
|
139
122
|
if (!type || !entityClasses[type]) {
|
|
140
123
|
return processedData;
|
|
141
124
|
}
|
|
142
|
-
|
|
143
125
|
return Reflect.construct(entityClasses[type], [processedData]);
|
|
144
126
|
}
|
|
145
|
-
|
|
146
127
|
function addTypenameToSelections(document) {
|
|
147
128
|
return visit(document, {
|
|
148
129
|
SelectionSet: {
|
|
@@ -153,40 +134,32 @@ function addTypenameToSelections(document) {
|
|
|
153
134
|
}
|
|
154
135
|
|
|
155
136
|
// No changes if no selections.
|
|
156
|
-
const {
|
|
137
|
+
const {
|
|
138
|
+
selections
|
|
139
|
+
} = node;
|
|
157
140
|
if (!selections) {
|
|
158
141
|
return;
|
|
159
142
|
}
|
|
160
143
|
|
|
161
144
|
// If selections already have a __typename, or are part of an
|
|
162
145
|
// introspection query, do nothing.
|
|
163
|
-
const skip = selections.some(
|
|
164
|
-
(selection) =>
|
|
165
|
-
selection.kind === 'Field' &&
|
|
166
|
-
(selection.name.value === '__typename' ||
|
|
167
|
-
selection.name.value.lastIndexOf('__', 0) === 0),
|
|
168
|
-
);
|
|
169
|
-
|
|
146
|
+
const skip = selections.some(selection => selection.kind === 'Field' && (selection.name.value === '__typename' || selection.name.value.lastIndexOf('__', 0) === 0));
|
|
170
147
|
if (skip) {
|
|
171
148
|
return;
|
|
172
149
|
}
|
|
173
150
|
|
|
174
151
|
// If this SelectionSet is @export-ed as an input variable, it should
|
|
175
152
|
// not have a __typename field (see issue #4691).
|
|
176
|
-
if (
|
|
177
|
-
parent.kind === 'Field' &&
|
|
178
|
-
parent.directives &&
|
|
179
|
-
parent.directives.some((d) => d.name.value === 'export')
|
|
180
|
-
) {
|
|
153
|
+
if (parent.kind === 'Field' && parent.directives && parent.directives.some(d => d.name.value === 'export')) {
|
|
181
154
|
return;
|
|
182
155
|
}
|
|
183
156
|
|
|
184
157
|
// Create and return a new SelectionSet with a __typename Field.
|
|
185
158
|
return Object.assign({}, node, {
|
|
186
|
-
selections: [...selections, TYPENAME_FIELD]
|
|
159
|
+
selections: [...selections, TYPENAME_FIELD]
|
|
187
160
|
});
|
|
188
|
-
}
|
|
189
|
-
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
190
163
|
});
|
|
191
164
|
}
|
|
192
165
|
|
package/lib/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(r,e){if("function"==typeof define&&define.amd)define("@merkur/plugin-graphql-client",["exports","@merkur/core","graphql","@merkur/plugin-error"],e);else if("undefined"!=typeof exports)e(exports,require("@merkur/core"),require("graphql"),require("@merkur/plugin-error"));else{var t={exports:{}};e(t.exports,r.Merkur.Core,r.graphql,r.Merkur.Plugin.Error),r.merkurPluginGraphqlClient=t.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(r,e,t,n){Object.defineProperty(r,"__esModule",{value:!0}),r.UnauthorizedError=r.GraphQLError=void 0,r.graphqlClientPlugin=function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"graphql";return{setup:function(n){return d((function*(){return(0,e.assignMissingKeys)(n,function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"graphql";return p({},r,{request:function(e,n){var u=arguments;return d((function*(){var a,l=u.length>2&&void 0!==u[2]?u[2]:{},c=u.length>3&&void 0!==u[3]?u[3]:{},f=e.$in.graphqlClient[r],p=f.endpointUrl,y=f.entityClasses,d=c.headers,b=void 0===d?{}:d,v=c.body,h=void 0===v?{}:v,m=function(r,e){if(null==r)return{};var t,n,o=function(r,e){if(null==r)return{};var t={};for(var n in r)if({}.hasOwnProperty.call(r,n)){if(e.
|
|
1
|
+
!function(r,e){if("function"==typeof define&&define.amd)define("@merkur/plugin-graphql-client",["exports","@merkur/core","graphql","@merkur/plugin-error"],e);else if("undefined"!=typeof exports)e(exports,require("@merkur/core"),require("graphql"),require("@merkur/plugin-error"));else{var t={exports:{}};e(t.exports,r.Merkur.Core,r.graphql,r.Merkur.Plugin.Error),r.merkurPluginGraphqlClient=t.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(r,e,t,n){Object.defineProperty(r,"__esModule",{value:!0}),r.UnauthorizedError=r.GraphQLError=void 0,r.graphqlClientPlugin=function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"graphql";return{setup:function(n){return d((function*(){return(0,e.assignMissingKeys)(n,function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"graphql";return p({},r,{request:function(e,n){var u=arguments;return d((function*(){var a,l=u.length>2&&void 0!==u[2]?u[2]:{},c=u.length>3&&void 0!==u[3]?u[3]:{},f=e.$in.graphqlClient[r],p=f.endpointUrl,y=f.entityClasses,d=c.headers,b=void 0===d?{}:d,v=c.body,h=void 0===v?{}:v,m=function(r,e){if(null==r)return{};var t,n,o=function(r,e){if(null==r)return{};var t={};for(var n in r)if({}.hasOwnProperty.call(r,n)){if(-1!==e.indexOf(n))continue;t[n]=r[n]}return t}(r,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(r);for(n=0;n<i.length;n++)t=i[n],-1===e.indexOf(t)&&{}.propertyIsEnumerable.call(r,t)&&(o[t]=r[t])}return o}(c,o);a=n,n=(0,t.visit)(a,{SelectionSet:{enter:function(r,e,t){if(!t||"OperationDefinition"!==t.kind){var n=r.selections;if(n&&!(n.some((function(r){return"Field"===r.kind&&("__typename"===r.name.value||0===r.name.value.lastIndexOf("__",0))}))||"Field"===t.kind&&t.directives&&t.directives.some((function(r){return"export"===r.name.value}))))return Object.assign({},r,{selections:[].concat(i(n),[q])})}}}});var g=(yield e.http.request(s({url:p,method:"POST",headers:s({"Content-Type":"application/json"},b),body:s({query:(0,t.stripIgnoredCharacters)((0,t.print)(n)),variables:l},h)},m))).response.body,O=g.errors,j=g.data,w=void 0===j?{}:j;return O?Array.isArray(O)&&O.some((function(r){return"unauthorized"===r.status}))?Promise.reject(new S("Unauthorized Error",{errors:O,data:w})):Promise.reject(new E("Api Error",{errors:O,data:w})):k(w,y)}))()}})}(r)),n.$in.graphqlClient||(n.$in.graphqlClient={}),n.$in.graphqlClient[r]={endpointUrl:"",entityClasses:{}},n}))()},create:function(t){return d((function*(){if(C===_&&!t.$in.httpClient)throw new Error("You must install missing plugin: npm i @merkur/plugin-http-client");return(0,e.bindWidgetToFunctions)(t,t[r]),t}))()}}},r.setEndpointUrl=function(r,e){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"graphql";r.$in.graphqlClient[t].endpointUrl=e},r.setEntityClasses=function(r,e){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"graphql";r.$in.graphqlClient[t].entityClasses=function(r){var e={};return r.forEach((function(r){var t=r.entityType;Array.isArray(t)||(t=[t]),t.forEach((function(t){return e[t]=r}))})),e}(e)};var o=["headers","body"];function i(r){return function(r){if(Array.isArray(r))return c(r)}(r)||function(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}(r)||l(r)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(r){return u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},u(r)}function a(r,e){return function(r){if(Array.isArray(r))return r}(r)||function(r,e){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=t){var n,o,i,u,a=[],l=!0,c=!1;try{if(i=(t=t.call(r)).next,0===e){if(Object(t)!==t)return;l=!1}else for(;!(l=(n=i.call(t)).done)&&(a.push(n.value),a.length!==e);l=!0);}catch(r){c=!0,o=r}finally{try{if(!l&&null!=t.return&&(u=t.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}(r,e)||l(r,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(r,e){if(r){if("string"==typeof r)return c(r,e);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?c(r,e):void 0}}function c(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=Array(e);t<e;t++)n[t]=r[t];return n}function f(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,n)}return t}function s(r){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{};e%2?f(Object(t),!0).forEach((function(e){p(r,e,t[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(t)):f(Object(t)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(t,e))}))}return r}function p(r,e,t){return(e=h(e))in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function y(r,e,t,n,o,i,u){try{var a=r[i](u),l=a.value}catch(r){return void t(r)}a.done?e(l):Promise.resolve(l).then(n,o)}function d(r){return function(){var e=this,t=arguments;return new Promise((function(n,o){var i=r.apply(e,t);function u(r){y(i,n,o,u,a,"next",r)}function a(r){y(i,n,o,u,a,"throw",r)}u(void 0)}))}}function b(r,e){for(var t=0;t<e.length;t++){var n=e[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,h(n.key),n)}}function v(r,e,t){return e&&b(r.prototype,e),t&&b(r,t),Object.defineProperty(r,"prototype",{writable:!1}),r}function h(r){var e=function(r,e){if("object"!=u(r)||!r)return r;var t=r[Symbol.toPrimitive];if(void 0!==t){var n=t.call(r,e||"default");if("object"!=u(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(r)}(r,"string");return"symbol"==u(e)?e:e+""}function m(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")}function g(r,e,t){return e=j(e),function(r,e){if(e&&("object"==u(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(r){if(void 0===r)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return r}(r)}(r,O()?Reflect.construct(e,t||[],j(r).constructor):e.apply(r,t))}function O(){try{var r=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(r){}return(O=function(){return!!r})()}function j(r){return j=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(r){return r.__proto__||Object.getPrototypeOf(r)},j(r)}function w(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");r.prototype=Object.create(e&&e.prototype,{constructor:{value:r,writable:!0,configurable:!0}}),Object.defineProperty(r,"prototype",{writable:!1}),e&&P(r,e)}function P(r,e){return P=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(r,e){return r.__proto__=e,r},P(r,e)}var E=r.GraphQLError=function(r){function e(){return m(this,e),g(this,e,arguments)}return w(e,r),v(e)}(n.GenericError),S=r.UnauthorizedError=function(r){function e(){return m(this,e),g(this,e,arguments)}return w(e,r),v(e)}(E),q={kind:"Field",name:{kind:"Name",value:"__typename"}},_="development",C="undefined"!=typeof process&&process&&process.env?process.env.NODE_ENV:_;function k(r,e){var t={};if("string"==typeof r)return r;Object.entries(r).forEach((function(r){var n=a(r,2),o=n[0],i=n[1];Array.isArray(i)?i=i.map((function(r){return k(r,e)})):"object"===u(i)&&null!==i&&(i=k(i,e)),t[o]=i}));var n=t.__typename;return n&&e[n]?Reflect.construct(e[n],[t]):t}}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkur/plugin-graphql-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.0",
|
|
4
4
|
"description": "Merkur event emitter plugin.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -27,8 +27,7 @@
|
|
|
27
27
|
"preversion": "npm test",
|
|
28
28
|
"test": "jest --no-watchman -c ./jest.config.js",
|
|
29
29
|
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
|
|
30
|
-
"build": "rollup -c rollup.config.mjs"
|
|
31
|
-
"prepare": "npm run build"
|
|
30
|
+
"build": "rollup -c rollup.config.mjs"
|
|
32
31
|
},
|
|
33
32
|
"repository": {
|
|
34
33
|
"type": "git",
|
|
@@ -51,10 +50,10 @@
|
|
|
51
50
|
},
|
|
52
51
|
"homepage": "https://merkur.js.org/",
|
|
53
52
|
"devDependencies": {
|
|
54
|
-
"@merkur/core": "^0.
|
|
55
|
-
"@merkur/plugin-component": "^0.
|
|
56
|
-
"@merkur/plugin-error": "^0.
|
|
57
|
-
"@merkur/plugin-http-client": "^0.
|
|
53
|
+
"@merkur/core": "^0.39.0",
|
|
54
|
+
"@merkur/plugin-component": "^0.39.0",
|
|
55
|
+
"@merkur/plugin-error": "^0.39.0",
|
|
56
|
+
"@merkur/plugin-http-client": "^0.39.0"
|
|
58
57
|
},
|
|
59
58
|
"peerDependencies": {
|
|
60
59
|
"@merkur/core": "*",
|
|
@@ -65,5 +64,5 @@
|
|
|
65
64
|
"graphql": "^16.6.0",
|
|
66
65
|
"graphql-tag": "^2.12.6"
|
|
67
66
|
},
|
|
68
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "8ad2c8b26246850ce6289502a8b05e882f80ce31"
|
|
69
68
|
}
|