@merkur/plugin-graphql-client 0.29.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/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2021 Vojtěch Šimko SimkoVojta@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ <p align="center">
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-illustration.png" width="100px" height="100px" alt="Merkur illustration"/>
4
+ </a>
5
+ </p>
6
+
7
+ # Merkur
8
+
9
+ [![Build Status](https://github.com/mjancarik/merkur/workflows/CI/badge.svg)](https://github.com/mjancarik/merkur/actions/workflows/ci.yml)
10
+ [![NPM package version](https://img.shields.io/npm/v/@merkur/core/latest.svg)](https://www.npmjs.com/package/@merkur/core)
11
+ ![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@merkur/core/latest)
12
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
13
+
14
+ The [Merkur](https://merkur.js.org/) is tiny extensible javascript library for front-end microservices(micro frontends). It allows by default server side rendering for loading performance boost. You can connect it with other frameworks or languages because merkur defines easy API. You can use one of six predefined template's library [React](https://reactjs.org/), [Preact](https://preactjs.com/), [hyperHTML](https://viperhtml.js.org/hyper.html), [µhtml](https://github.com/WebReflection/uhtml#readme), [Svelte](https://svelte.dev/) and [vanilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) but you can easily extend for others.
15
+
16
+ ## Features
17
+ - Flexible templating engine
18
+ - Usable with all tech stacks
19
+ - SSR-ready by default
20
+ - Easy extensible with plugins
21
+ - Tiny - 1 KB minified + gzipped
22
+
23
+ ## Getting started
24
+
25
+ ```bash
26
+ npx @merkur/create-widget <name>
27
+
28
+ cd name
29
+
30
+ npm run dev // Point your browser at http://localhost:4444/
31
+ ```
32
+ ![alt text](https://raw.githubusercontent.com/mjancarik/merkur/master/images/hello-widget.png "Merkur example, hello widget")
33
+ ## Documentation
34
+
35
+ To check out [live demo](https://merkur.js.org/demo) and [docs](https://merkur.js.org/docs), visit [https://merkur.js.org](https://merkur.js.org).
36
+
37
+ ## Contribution
38
+
39
+ Contribute to this project via [Pull-Requests](https://github.com/mjancarik/merkur/pulls).
40
+
41
+ We are following [Conventional Commits Specification](https://www.conventionalcommits.org/en/v1.0.0/#summary). To simplify the commit process, you can use `npm run commit` command. It opens an interactive interface, which should help you with commit message composition.
42
+
43
+ Thank you to all the people who already contributed to Merkur!
44
+
45
+ <a href="https://github.com/mjancarik/merkur/graphs/contributors">
46
+ <img src="https://contrib.rocks/image?repo=mjancarik/merkur" />
47
+ </a>
package/jest.config.js ADDED
@@ -0,0 +1,3 @@
1
+ const defaultConfig = require('../../jest.config.js');
2
+
3
+ module.exports = { ...defaultConfig };
package/lib/index.cjs ADDED
@@ -0,0 +1,201 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var core = require('@merkur/core');
6
+ var graphql = require('graphql');
7
+ var pluginError = require('@merkur/plugin-error');
8
+
9
+ class GraphQLError extends pluginError.GenericError {}
10
+
11
+ class UnauthorizedError extends GraphQLError {}
12
+
13
+ const TYPENAME_FIELD = {
14
+ kind: 'Field',
15
+ name: {
16
+ kind: 'Name',
17
+ value: '__typename',
18
+ },
19
+ };
20
+
21
+ const DEV = 'development';
22
+ const ENV =
23
+ typeof process !== 'undefined' && process && process.env
24
+ ? process.env.NODE_ENV
25
+ : DEV;
26
+
27
+ function setEndpointUrl(widget, url) {
28
+ widget.$in.graphqlClient.endpointUrl = url;
29
+ }
30
+
31
+ function setEntityClasses(widget, entityClasses) {
32
+ widget.$in.graphqlClient.entityClasses = buildTypeToEntityMap(entityClasses);
33
+ }
34
+
35
+ function graphqlClientPlugin() {
36
+ return {
37
+ async setup(widget) {
38
+ widget = {
39
+ ...graphqlClientAPI(),
40
+ ...widget,
41
+ };
42
+
43
+ widget.$in.graphqlClient = {
44
+ endpointUrl: '',
45
+ entityClasses: {},
46
+ };
47
+
48
+ return widget;
49
+ },
50
+ async create(widget) {
51
+ if (ENV === DEV && !widget.$in.httpClient) {
52
+ throw new Error(
53
+ 'You must install missing plugin: npm i @merkur/plugin-http-client'
54
+ );
55
+ }
56
+
57
+ core.bindWidgetToFunctions(widget, widget.graphql);
58
+
59
+ return widget;
60
+ },
61
+ };
62
+ }
63
+
64
+ function graphqlClientAPI() {
65
+ return {
66
+ graphql: {
67
+ async request(widget, operation, variables = {}, options = {}) {
68
+ const { endpointUrl, entityClasses } = widget.$in.graphqlClient;
69
+ const { headers = {}, body = {}, ...restOptions } = options;
70
+
71
+ operation = addTypenameToSelections(operation);
72
+
73
+ const { response } = await widget.http.request({
74
+ url: endpointUrl,
75
+ method: 'POST',
76
+ headers: {
77
+ 'Content-Type': 'application/json',
78
+ ...headers,
79
+ },
80
+ body: {
81
+ query: graphql.stripIgnoredCharacters(graphql.print(operation)),
82
+ variables,
83
+ ...body,
84
+ },
85
+ ...restOptions,
86
+ });
87
+
88
+ const { errors, data = {} } = response.body;
89
+ if (errors) {
90
+ if (
91
+ Array.isArray(errors) &&
92
+ errors.some((error) => error.status === 'unauthorized')
93
+ ) {
94
+ return Promise.reject(
95
+ new UnauthorizedError(`Unauthorized Error`, { errors, data })
96
+ );
97
+ }
98
+
99
+ return Promise.reject(
100
+ new GraphQLError(`Api Error`, { errors, data })
101
+ );
102
+ }
103
+
104
+ return processResponseData(data, entityClasses);
105
+ },
106
+ },
107
+ };
108
+ }
109
+
110
+ function buildTypeToEntityMap(entityClasses) {
111
+ const map = {};
112
+
113
+ entityClasses.forEach((entityClass) => {
114
+ let { entityType } = entityClass;
115
+
116
+ if (!Array.isArray(entityType)) {
117
+ entityType = [entityType];
118
+ }
119
+
120
+ entityType.forEach((type) => (map[type] = entityClass));
121
+ });
122
+
123
+ return map;
124
+ }
125
+
126
+ function processResponseData(data, entityClasses) {
127
+ const processedData = {};
128
+
129
+ if (typeof data === 'string') {
130
+ return data;
131
+ }
132
+ Object.entries(data).forEach(([field, value]) => {
133
+ if (Array.isArray(value)) {
134
+ value = value.map((node) => processResponseData(node, entityClasses));
135
+ } else if (typeof value === 'object' && value !== null) {
136
+ value = processResponseData(value, entityClasses);
137
+ }
138
+
139
+ processedData[field] = value;
140
+ });
141
+
142
+ const type = data.__typename;
143
+ if (!type || !entityClasses[type]) {
144
+ return data;
145
+ }
146
+
147
+ return Reflect.construct(entityClasses[type], [data]);
148
+ }
149
+
150
+ function addTypenameToSelections(document) {
151
+ return graphql.visit(document, {
152
+ SelectionSet: {
153
+ enter(node, _key, parent) {
154
+ // Don't add __typename to OperationDefinitions.
155
+ if (parent && parent.kind === 'OperationDefinition') {
156
+ return;
157
+ }
158
+
159
+ // No changes if no selections.
160
+ const { selections } = node;
161
+ if (!selections) {
162
+ return;
163
+ }
164
+
165
+ // If selections already have a __typename, or are part of an
166
+ // introspection query, do nothing.
167
+ const skip = selections.some(
168
+ (selection) =>
169
+ selection.kind === 'Field' &&
170
+ (selection.name.value === '__typename' ||
171
+ selection.name.value.lastIndexOf('__', 0) === 0)
172
+ );
173
+
174
+ if (skip) {
175
+ return;
176
+ }
177
+
178
+ // If this SelectionSet is @export-ed as an input variable, it should
179
+ // not have a __typename field (see issue #4691).
180
+ if (
181
+ parent.kind === 'Field' &&
182
+ parent.directives &&
183
+ parent.directives.some((d) => d.name.value === 'export')
184
+ ) {
185
+ return;
186
+ }
187
+
188
+ // Create and return a new SelectionSet with a __typename Field.
189
+ return Object.assign({}, node, {
190
+ selections: [...selections, TYPENAME_FIELD],
191
+ });
192
+ },
193
+ },
194
+ });
195
+ }
196
+
197
+ exports.GraphQLError = GraphQLError;
198
+ exports.UnauthorizedError = UnauthorizedError;
199
+ exports.graphqlClientPlugin = graphqlClientPlugin;
200
+ exports.setEndpointUrl = setEndpointUrl;
201
+ exports.setEntityClasses = setEntityClasses;
@@ -0,0 +1 @@
1
+ "use strict";require("core-js/modules/es.object.set-prototype-of.js"),require("core-js/modules/es.object.get-prototype-of.js"),require("core-js/modules/es.object.create.js"),require("core-js/modules/es.object.keys.js"),require("core-js/modules/es.symbol.js"),require("core-js/modules/es.array.filter.js"),require("core-js/modules/es.object.get-own-property-descriptor.js"),require("core-js/modules/es.object.get-own-property-descriptors.js"),require("core-js/modules/es.object.define-properties.js"),require("core-js/modules/es.array.index-of.js"),require("core-js/modules/es.symbol.description.js"),require("core-js/modules/es.symbol.iterator.js"),require("core-js/modules/es.array.iterator.js"),require("core-js/modules/es.string.iterator.js"),require("core-js/modules/web.dom-collections.iterator.js"),require("core-js/modules/es.array.slice.js"),require("core-js/modules/es.array.from.js"),require("core-js/modules/es.regexp.exec.js");var e=["headers","body"];function r(e){return function(e){if(Array.isArray(e))return i(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||o(e)||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 t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t(e)}function n(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==t)return;var n,o,i=[],u=!0,s=!1;try{for(t=t.call(e);!(u=(n=t.next()).done)&&(i.push(n.value),!r||i.length!==r);u=!0);}catch(e){s=!0,o=e}finally{try{u||null==t.return||t.return()}finally{if(s)throw o}}return i}(e,r)||o(e,r)||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 o(e,r){if(e){if("string"==typeof e)return i(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?i(e,r):void 0}}function i(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function u(e,r){if(null==e)return{};var t,n,o=function(e,r){if(null==e)return{};var t,n,o={},i=Object.keys(e);for(n=0;n<i.length;n++)t=i[n],r.indexOf(t)>=0||(o[t]=e[t]);return o}(e,r);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)t=i[n],r.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(o[t]=e[t])}return o}function s(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function c(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?s(Object(t),!0).forEach((function(r){a(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):s(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function a(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function l(e,r,t,n,o,i,u){try{var s=e[i](u),c=s.value}catch(e){return void t(e)}s.done?r(c):Promise.resolve(c).then(n,o)}function f(e){return function(){var r=this,t=arguments;return new Promise((function(n,o){var i=e.apply(r,t);function u(e){l(i,n,o,u,s,"next",e)}function s(e){l(i,n,o,u,s,"throw",e)}u(void 0)}))}}function p(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function y(e,r,t){return r&&p(e.prototype,r),t&&p(e,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function d(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}function j(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(r&&r.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),r&&m(e,r)}function m(e,r){return m=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e},m(e,r)}function b(e){var r=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var t,n=v(e);if(r){var o=v(this).constructor;t=Reflect.construct(n,arguments,o)}else t=n.apply(this,arguments);return h(this,t)}}function h(e,r){if(r&&("object"===t(r)||"function"==typeof r))return r;if(void 0!==r)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function v(e){return v=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},v(e)}require("regenerator-runtime/runtime.js"),require("core-js/modules/es.object.define-property.js"),require("core-js/modules/es.array.is-array.js"),require("core-js/modules/es.array.some.js"),require("core-js/modules/es.object.to-string.js"),require("core-js/modules/es.promise.js"),require("core-js/modules/es.array.for-each.js"),require("core-js/modules/web.dom-collections.for-each.js"),require("core-js/modules/es.object.entries.js"),require("core-js/modules/es.array.map.js"),require("core-js/modules/es.reflect.construct.js"),require("core-js/modules/es.function.name.js"),require("core-js/modules/es.array.last-index-of.js"),require("core-js/modules/es.object.assign.js"),require("core-js/modules/es.array.concat.js"),Object.defineProperty(exports,"__esModule",{value:!0});var g=require("@merkur/core"),O=require("graphql"),q=require("@merkur/plugin-error"),w=function(e){j(t,q.GenericError);var r=b(t);function t(){return d(this,t),r.apply(this,arguments)}return y(t)}(),P=function(e){j(t,w);var r=b(t);function t(){return d(this,t),r.apply(this,arguments)}return y(t)}(),x={kind:"Field",name:{kind:"Name",value:"__typename"}},E="development",S="undefined"!=typeof process&&process&&process.env?process.env.NODE_ENV:E;function _(e,r){var o={};if("string"==typeof e)return e;Object.entries(e).forEach((function(e){var i=n(e,2),u=i[0],s=i[1];Array.isArray(s)?s=s.map((function(e){return _(e,r)})):"object"===t(s)&&null!==s&&(s=_(s,r)),o[u]=s}));var i=e.__typename;return i&&r[i]?Reflect.construct(r[i],[e]):e}function k(e){return O.visit(e,{SelectionSet:{enter:function(e,t,n){if(!n||"OperationDefinition"!==n.kind){var o=e.selections;if(o)if(!(o.some((function(e){return"Field"===e.kind&&("__typename"===e.name.value||0===e.name.value.lastIndexOf("__",0))}))||"Field"===n.kind&&n.directives&&n.directives.some((function(e){return"export"===e.name.value}))))return Object.assign({},e,{selections:[].concat(r(o),[x])})}}}})}exports.GraphQLError=w,exports.UnauthorizedError=P,exports.graphqlClientPlugin=function(){return{setup:function(r){return f(regeneratorRuntime.mark((function t(){return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=c(c({},{graphql:{request:function(r,t){var n=arguments;return f(regeneratorRuntime.mark((function o(){var i,s,a,l,f,p,y,d,j,m,b,h,v,g,q,x;return regeneratorRuntime.wrap((function(o){for(;;)switch(o.prev=o.next){case 0:return i=n.length>2&&void 0!==n[2]?n[2]:{},s=n.length>3&&void 0!==n[3]?n[3]:{},a=r.$in.graphqlClient,l=a.endpointUrl,f=a.entityClasses,p=s.headers,y=void 0===p?{}:p,d=s.body,j=void 0===d?{}:d,m=u(s,e),t=k(t),o.next=7,r.http.request(c({url:l,method:"POST",headers:c({"Content-Type":"application/json"},y),body:c({query:O.stripIgnoredCharacters(O.print(t)),variables:i},j)},m));case 7:if(b=o.sent,h=b.response,v=h.body,g=v.errors,q=v.data,x=void 0===q?{}:q,!g){o.next=14;break}if(!Array.isArray(g)||!g.some((function(e){return"unauthorized"===e.status}))){o.next=13;break}return o.abrupt("return",Promise.reject(new P("Unauthorized Error",{errors:g,data:x})));case 13:return o.abrupt("return",Promise.reject(new w("Api Error",{errors:g,data:x})));case 14:return o.abrupt("return",_(x,f));case 15:case"end":return o.stop()}}),o)})))()}}}),r),r.$in.graphqlClient={endpointUrl:"",entityClasses:{}},t.abrupt("return",r);case 3:case"end":return t.stop()}}),t)})))()},create:function(e){return f(regeneratorRuntime.mark((function r(){return regeneratorRuntime.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(S!==E||e.$in.httpClient){r.next=2;break}throw new Error("You must install missing plugin: npm i @merkur/plugin-http-client");case 2:return g.bindWidgetToFunctions(e,e.graphql),r.abrupt("return",e);case 4:case"end":return r.stop()}}),r)})))()}}},exports.setEndpointUrl=function(e,r){e.$in.graphqlClient.endpointUrl=r},exports.setEntityClasses=function(e,r){e.$in.graphqlClient.entityClasses=function(e){var r={};return e.forEach((function(e){var t=e.entityType;Array.isArray(t)||(t=[t]),t.forEach((function(t){return r[t]=e}))})),r}(r)};
@@ -0,0 +1,203 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+
7
+ var core = require('@merkur/core');
8
+
9
+ var graphql = require('graphql');
10
+
11
+ var pluginError = require('@merkur/plugin-error');
12
+
13
+ class GraphQLError extends pluginError.GenericError {}
14
+
15
+ class UnauthorizedError extends GraphQLError {}
16
+
17
+ const TYPENAME_FIELD = {
18
+ kind: 'Field',
19
+ name: {
20
+ kind: 'Name',
21
+ value: '__typename'
22
+ }
23
+ };
24
+ const DEV = 'development';
25
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
26
+
27
+ function setEndpointUrl(widget, url) {
28
+ widget.$in.graphqlClient.endpointUrl = url;
29
+ }
30
+
31
+ function setEntityClasses(widget, entityClasses) {
32
+ widget.$in.graphqlClient.entityClasses = buildTypeToEntityMap(entityClasses);
33
+ }
34
+
35
+ function graphqlClientPlugin() {
36
+ return {
37
+ async setup(widget) {
38
+ widget = { ...graphqlClientAPI(),
39
+ ...widget
40
+ };
41
+ widget.$in.graphqlClient = {
42
+ endpointUrl: '',
43
+ entityClasses: {}
44
+ };
45
+ return widget;
46
+ },
47
+
48
+ async create(widget) {
49
+ if (ENV === DEV && !widget.$in.httpClient) {
50
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-http-client');
51
+ }
52
+
53
+ core.bindWidgetToFunctions(widget, widget.graphql);
54
+ return widget;
55
+ }
56
+
57
+ };
58
+ }
59
+
60
+ function graphqlClientAPI() {
61
+ return {
62
+ graphql: {
63
+ async request(widget, operation, variables = {}, options = {}) {
64
+ const {
65
+ endpointUrl,
66
+ entityClasses
67
+ } = widget.$in.graphqlClient;
68
+ const {
69
+ headers = {},
70
+ body = {},
71
+ ...restOptions
72
+ } = options;
73
+ operation = addTypenameToSelections(operation);
74
+ const {
75
+ response
76
+ } = await widget.http.request({
77
+ url: endpointUrl,
78
+ method: 'POST',
79
+ headers: {
80
+ 'Content-Type': 'application/json',
81
+ ...headers
82
+ },
83
+ body: {
84
+ query: graphql.stripIgnoredCharacters(graphql.print(operation)),
85
+ variables,
86
+ ...body
87
+ },
88
+ ...restOptions
89
+ });
90
+ const {
91
+ errors,
92
+ data = {}
93
+ } = response.body;
94
+
95
+ if (errors) {
96
+ if (Array.isArray(errors) && errors.some(error => error.status === 'unauthorized')) {
97
+ return Promise.reject(new UnauthorizedError(`Unauthorized Error`, {
98
+ errors,
99
+ data
100
+ }));
101
+ }
102
+
103
+ return Promise.reject(new GraphQLError(`Api Error`, {
104
+ errors,
105
+ data
106
+ }));
107
+ }
108
+
109
+ return processResponseData(data, entityClasses);
110
+ }
111
+
112
+ }
113
+ };
114
+ }
115
+
116
+ function buildTypeToEntityMap(entityClasses) {
117
+ const map = {};
118
+ entityClasses.forEach(entityClass => {
119
+ let {
120
+ entityType
121
+ } = entityClass;
122
+
123
+ if (!Array.isArray(entityType)) {
124
+ entityType = [entityType];
125
+ }
126
+
127
+ entityType.forEach(type => map[type] = entityClass);
128
+ });
129
+ return map;
130
+ }
131
+
132
+ function processResponseData(data, entityClasses) {
133
+ const processedData = {};
134
+
135
+ if (typeof data === 'string') {
136
+ return data;
137
+ }
138
+
139
+ Object.entries(data).forEach(([field, value]) => {
140
+ if (Array.isArray(value)) {
141
+ value = value.map(node => processResponseData(node, entityClasses));
142
+ } else if (typeof value === 'object' && value !== null) {
143
+ value = processResponseData(value, entityClasses);
144
+ }
145
+
146
+ processedData[field] = value;
147
+ });
148
+ const type = data.__typename;
149
+
150
+ if (!type || !entityClasses[type]) {
151
+ return data;
152
+ }
153
+
154
+ return Reflect.construct(entityClasses[type], [data]);
155
+ }
156
+
157
+ function addTypenameToSelections(document) {
158
+ return graphql.visit(document, {
159
+ SelectionSet: {
160
+ enter(node, _key, parent) {
161
+ // Don't add __typename to OperationDefinitions.
162
+ if (parent && parent.kind === 'OperationDefinition') {
163
+ return;
164
+ } // No changes if no selections.
165
+
166
+
167
+ const {
168
+ selections
169
+ } = node;
170
+
171
+ if (!selections) {
172
+ return;
173
+ } // If selections already have a __typename, or are part of an
174
+ // introspection query, do nothing.
175
+
176
+
177
+ const skip = selections.some(selection => selection.kind === 'Field' && (selection.name.value === '__typename' || selection.name.value.lastIndexOf('__', 0) === 0));
178
+
179
+ if (skip) {
180
+ return;
181
+ } // If this SelectionSet is @export-ed as an input variable, it should
182
+ // not have a __typename field (see issue #4691).
183
+
184
+
185
+ if (parent.kind === 'Field' && parent.directives && parent.directives.some(d => d.name.value === 'export')) {
186
+ return;
187
+ } // Create and return a new SelectionSet with a __typename Field.
188
+
189
+
190
+ return Object.assign({}, node, {
191
+ selections: [...selections, TYPENAME_FIELD]
192
+ });
193
+ }
194
+
195
+ }
196
+ });
197
+ }
198
+
199
+ exports.GraphQLError = GraphQLError;
200
+ exports.UnauthorizedError = UnauthorizedError;
201
+ exports.graphqlClientPlugin = graphqlClientPlugin;
202
+ exports.setEndpointUrl = setEndpointUrl;
203
+ exports.setEntityClasses = setEntityClasses;
@@ -0,0 +1,191 @@
1
+ import { bindWidgetToFunctions } from '@merkur/core';
2
+ import { visit, stripIgnoredCharacters, print } from 'graphql';
3
+ import { GenericError } from '@merkur/plugin-error';
4
+
5
+ class GraphQLError extends GenericError {}
6
+
7
+ class UnauthorizedError extends GraphQLError {}
8
+
9
+ const TYPENAME_FIELD = {
10
+ kind: 'Field',
11
+ name: {
12
+ kind: 'Name',
13
+ value: '__typename'
14
+ }
15
+ };
16
+ const DEV = 'development';
17
+ const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
18
+
19
+ function setEndpointUrl(widget, url) {
20
+ widget.$in.graphqlClient.endpointUrl = url;
21
+ }
22
+
23
+ function setEntityClasses(widget, entityClasses) {
24
+ widget.$in.graphqlClient.entityClasses = buildTypeToEntityMap(entityClasses);
25
+ }
26
+
27
+ function graphqlClientPlugin() {
28
+ return {
29
+ async setup(widget) {
30
+ widget = { ...graphqlClientAPI(),
31
+ ...widget
32
+ };
33
+ widget.$in.graphqlClient = {
34
+ endpointUrl: '',
35
+ entityClasses: {}
36
+ };
37
+ return widget;
38
+ },
39
+
40
+ async create(widget) {
41
+ if (ENV === DEV && !widget.$in.httpClient) {
42
+ throw new Error('You must install missing plugin: npm i @merkur/plugin-http-client');
43
+ }
44
+
45
+ bindWidgetToFunctions(widget, widget.graphql);
46
+ return widget;
47
+ }
48
+
49
+ };
50
+ }
51
+
52
+ function graphqlClientAPI() {
53
+ return {
54
+ graphql: {
55
+ async request(widget, operation, variables = {}, options = {}) {
56
+ const {
57
+ endpointUrl,
58
+ entityClasses
59
+ } = widget.$in.graphqlClient;
60
+ const {
61
+ headers = {},
62
+ body = {},
63
+ ...restOptions
64
+ } = options;
65
+ operation = addTypenameToSelections(operation);
66
+ const {
67
+ response
68
+ } = await widget.http.request({
69
+ url: endpointUrl,
70
+ method: 'POST',
71
+ headers: {
72
+ 'Content-Type': 'application/json',
73
+ ...headers
74
+ },
75
+ body: {
76
+ query: stripIgnoredCharacters(print(operation)),
77
+ variables,
78
+ ...body
79
+ },
80
+ ...restOptions
81
+ });
82
+ const {
83
+ errors,
84
+ data = {}
85
+ } = response.body;
86
+
87
+ if (errors) {
88
+ if (Array.isArray(errors) && errors.some(error => error.status === 'unauthorized')) {
89
+ return Promise.reject(new UnauthorizedError(`Unauthorized Error`, {
90
+ errors,
91
+ data
92
+ }));
93
+ }
94
+
95
+ return Promise.reject(new GraphQLError(`Api Error`, {
96
+ errors,
97
+ data
98
+ }));
99
+ }
100
+
101
+ return processResponseData(data, entityClasses);
102
+ }
103
+
104
+ }
105
+ };
106
+ }
107
+
108
+ function buildTypeToEntityMap(entityClasses) {
109
+ const map = {};
110
+ entityClasses.forEach(entityClass => {
111
+ let {
112
+ entityType
113
+ } = entityClass;
114
+
115
+ if (!Array.isArray(entityType)) {
116
+ entityType = [entityType];
117
+ }
118
+
119
+ entityType.forEach(type => map[type] = entityClass);
120
+ });
121
+ return map;
122
+ }
123
+
124
+ function processResponseData(data, entityClasses) {
125
+ const processedData = {};
126
+
127
+ if (typeof data === 'string') {
128
+ return data;
129
+ }
130
+
131
+ Object.entries(data).forEach(([field, value]) => {
132
+ if (Array.isArray(value)) {
133
+ value = value.map(node => processResponseData(node, entityClasses));
134
+ } else if (typeof value === 'object' && value !== null) {
135
+ value = processResponseData(value, entityClasses);
136
+ }
137
+
138
+ processedData[field] = value;
139
+ });
140
+ const type = data.__typename;
141
+
142
+ if (!type || !entityClasses[type]) {
143
+ return data;
144
+ }
145
+
146
+ return Reflect.construct(entityClasses[type], [data]);
147
+ }
148
+
149
+ function addTypenameToSelections(document) {
150
+ return visit(document, {
151
+ SelectionSet: {
152
+ enter(node, _key, parent) {
153
+ // Don't add __typename to OperationDefinitions.
154
+ if (parent && parent.kind === 'OperationDefinition') {
155
+ return;
156
+ } // No changes if no selections.
157
+
158
+
159
+ const {
160
+ selections
161
+ } = node;
162
+
163
+ if (!selections) {
164
+ return;
165
+ } // If selections already have a __typename, or are part of an
166
+ // introspection query, do nothing.
167
+
168
+
169
+ const skip = selections.some(selection => selection.kind === 'Field' && (selection.name.value === '__typename' || selection.name.value.lastIndexOf('__', 0) === 0));
170
+
171
+ if (skip) {
172
+ return;
173
+ } // If this SelectionSet is @export-ed as an input variable, it should
174
+ // not have a __typename field (see issue #4691).
175
+
176
+
177
+ if (parent.kind === 'Field' && parent.directives && parent.directives.some(d => d.name.value === 'export')) {
178
+ return;
179
+ } // Create and return a new SelectionSet with a __typename Field.
180
+
181
+
182
+ return Object.assign({}, node, {
183
+ selections: [...selections, TYPENAME_FIELD]
184
+ });
185
+ }
186
+
187
+ }
188
+ });
189
+ }
190
+
191
+ export { GraphQLError, UnauthorizedError, graphqlClientPlugin, setEndpointUrl, setEntityClasses };
package/lib/index.js ADDED
@@ -0,0 +1,201 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var core = require('@merkur/core');
6
+ var graphql = require('graphql');
7
+ var pluginError = require('@merkur/plugin-error');
8
+
9
+ class GraphQLError extends pluginError.GenericError {}
10
+
11
+ class UnauthorizedError extends GraphQLError {}
12
+
13
+ const TYPENAME_FIELD = {
14
+ kind: 'Field',
15
+ name: {
16
+ kind: 'Name',
17
+ value: '__typename',
18
+ },
19
+ };
20
+
21
+ const DEV = 'development';
22
+ const ENV =
23
+ typeof process !== 'undefined' && process && process.env
24
+ ? process.env.NODE_ENV
25
+ : DEV;
26
+
27
+ function setEndpointUrl(widget, url) {
28
+ widget.$in.graphqlClient.endpointUrl = url;
29
+ }
30
+
31
+ function setEntityClasses(widget, entityClasses) {
32
+ widget.$in.graphqlClient.entityClasses = buildTypeToEntityMap(entityClasses);
33
+ }
34
+
35
+ function graphqlClientPlugin() {
36
+ return {
37
+ async setup(widget) {
38
+ widget = {
39
+ ...graphqlClientAPI(),
40
+ ...widget,
41
+ };
42
+
43
+ widget.$in.graphqlClient = {
44
+ endpointUrl: '',
45
+ entityClasses: {},
46
+ };
47
+
48
+ return widget;
49
+ },
50
+ async create(widget) {
51
+ if (ENV === DEV && !widget.$in.httpClient) {
52
+ throw new Error(
53
+ 'You must install missing plugin: npm i @merkur/plugin-http-client'
54
+ );
55
+ }
56
+
57
+ core.bindWidgetToFunctions(widget, widget.graphql);
58
+
59
+ return widget;
60
+ },
61
+ };
62
+ }
63
+
64
+ function graphqlClientAPI() {
65
+ return {
66
+ graphql: {
67
+ async request(widget, operation, variables = {}, options = {}) {
68
+ const { endpointUrl, entityClasses } = widget.$in.graphqlClient;
69
+ const { headers = {}, body = {}, ...restOptions } = options;
70
+
71
+ operation = addTypenameToSelections(operation);
72
+
73
+ const { response } = await widget.http.request({
74
+ url: endpointUrl,
75
+ method: 'POST',
76
+ headers: {
77
+ 'Content-Type': 'application/json',
78
+ ...headers,
79
+ },
80
+ body: {
81
+ query: graphql.stripIgnoredCharacters(graphql.print(operation)),
82
+ variables,
83
+ ...body,
84
+ },
85
+ ...restOptions,
86
+ });
87
+
88
+ const { errors, data = {} } = response.body;
89
+ if (errors) {
90
+ if (
91
+ Array.isArray(errors) &&
92
+ errors.some((error) => error.status === 'unauthorized')
93
+ ) {
94
+ return Promise.reject(
95
+ new UnauthorizedError(`Unauthorized Error`, { errors, data })
96
+ );
97
+ }
98
+
99
+ return Promise.reject(
100
+ new GraphQLError(`Api Error`, { errors, data })
101
+ );
102
+ }
103
+
104
+ return processResponseData(data, entityClasses);
105
+ },
106
+ },
107
+ };
108
+ }
109
+
110
+ function buildTypeToEntityMap(entityClasses) {
111
+ const map = {};
112
+
113
+ entityClasses.forEach((entityClass) => {
114
+ let { entityType } = entityClass;
115
+
116
+ if (!Array.isArray(entityType)) {
117
+ entityType = [entityType];
118
+ }
119
+
120
+ entityType.forEach((type) => (map[type] = entityClass));
121
+ });
122
+
123
+ return map;
124
+ }
125
+
126
+ function processResponseData(data, entityClasses) {
127
+ const processedData = {};
128
+
129
+ if (typeof data === 'string') {
130
+ return data;
131
+ }
132
+ Object.entries(data).forEach(([field, value]) => {
133
+ if (Array.isArray(value)) {
134
+ value = value.map((node) => processResponseData(node, entityClasses));
135
+ } else if (typeof value === 'object' && value !== null) {
136
+ value = processResponseData(value, entityClasses);
137
+ }
138
+
139
+ processedData[field] = value;
140
+ });
141
+
142
+ const type = data.__typename;
143
+ if (!type || !entityClasses[type]) {
144
+ return data;
145
+ }
146
+
147
+ return Reflect.construct(entityClasses[type], [data]);
148
+ }
149
+
150
+ function addTypenameToSelections(document) {
151
+ return graphql.visit(document, {
152
+ SelectionSet: {
153
+ enter(node, _key, parent) {
154
+ // Don't add __typename to OperationDefinitions.
155
+ if (parent && parent.kind === 'OperationDefinition') {
156
+ return;
157
+ }
158
+
159
+ // No changes if no selections.
160
+ const { selections } = node;
161
+ if (!selections) {
162
+ return;
163
+ }
164
+
165
+ // If selections already have a __typename, or are part of an
166
+ // introspection query, do nothing.
167
+ const skip = selections.some(
168
+ (selection) =>
169
+ selection.kind === 'Field' &&
170
+ (selection.name.value === '__typename' ||
171
+ selection.name.value.lastIndexOf('__', 0) === 0)
172
+ );
173
+
174
+ if (skip) {
175
+ return;
176
+ }
177
+
178
+ // If this SelectionSet is @export-ed as an input variable, it should
179
+ // not have a __typename field (see issue #4691).
180
+ if (
181
+ parent.kind === 'Field' &&
182
+ parent.directives &&
183
+ parent.directives.some((d) => d.name.value === 'export')
184
+ ) {
185
+ return;
186
+ }
187
+
188
+ // Create and return a new SelectionSet with a __typename Field.
189
+ return Object.assign({}, node, {
190
+ selections: [...selections, TYPENAME_FIELD],
191
+ });
192
+ },
193
+ },
194
+ });
195
+ }
196
+
197
+ exports.GraphQLError = GraphQLError;
198
+ exports.UnauthorizedError = UnauthorizedError;
199
+ exports.graphqlClientPlugin = graphqlClientPlugin;
200
+ exports.setEndpointUrl = setEndpointUrl;
201
+ exports.setEntityClasses = setEntityClasses;
package/lib/index.mjs ADDED
@@ -0,0 +1,193 @@
1
+ import { bindWidgetToFunctions } from '@merkur/core';
2
+ import { visit, stripIgnoredCharacters, print } from 'graphql';
3
+ import { GenericError } from '@merkur/plugin-error';
4
+
5
+ class GraphQLError extends GenericError {}
6
+
7
+ class UnauthorizedError extends GraphQLError {}
8
+
9
+ const TYPENAME_FIELD = {
10
+ kind: 'Field',
11
+ name: {
12
+ kind: 'Name',
13
+ value: '__typename',
14
+ },
15
+ };
16
+
17
+ const DEV = 'development';
18
+ const ENV =
19
+ typeof process !== 'undefined' && process && process.env
20
+ ? process.env.NODE_ENV
21
+ : DEV;
22
+
23
+ function setEndpointUrl(widget, url) {
24
+ widget.$in.graphqlClient.endpointUrl = url;
25
+ }
26
+
27
+ function setEntityClasses(widget, entityClasses) {
28
+ widget.$in.graphqlClient.entityClasses = buildTypeToEntityMap(entityClasses);
29
+ }
30
+
31
+ function graphqlClientPlugin() {
32
+ return {
33
+ async setup(widget) {
34
+ widget = {
35
+ ...graphqlClientAPI(),
36
+ ...widget,
37
+ };
38
+
39
+ widget.$in.graphqlClient = {
40
+ endpointUrl: '',
41
+ entityClasses: {},
42
+ };
43
+
44
+ return widget;
45
+ },
46
+ async create(widget) {
47
+ if (ENV === DEV && !widget.$in.httpClient) {
48
+ throw new Error(
49
+ 'You must install missing plugin: npm i @merkur/plugin-http-client'
50
+ );
51
+ }
52
+
53
+ bindWidgetToFunctions(widget, widget.graphql);
54
+
55
+ return widget;
56
+ },
57
+ };
58
+ }
59
+
60
+ function graphqlClientAPI() {
61
+ return {
62
+ graphql: {
63
+ async request(widget, operation, variables = {}, options = {}) {
64
+ const { endpointUrl, entityClasses } = widget.$in.graphqlClient;
65
+ const { headers = {}, body = {}, ...restOptions } = options;
66
+
67
+ operation = addTypenameToSelections(operation);
68
+
69
+ const { response } = await widget.http.request({
70
+ url: endpointUrl,
71
+ method: 'POST',
72
+ headers: {
73
+ 'Content-Type': 'application/json',
74
+ ...headers,
75
+ },
76
+ body: {
77
+ query: stripIgnoredCharacters(print(operation)),
78
+ variables,
79
+ ...body,
80
+ },
81
+ ...restOptions,
82
+ });
83
+
84
+ const { errors, data = {} } = response.body;
85
+ if (errors) {
86
+ if (
87
+ Array.isArray(errors) &&
88
+ errors.some((error) => error.status === 'unauthorized')
89
+ ) {
90
+ return Promise.reject(
91
+ new UnauthorizedError(`Unauthorized Error`, { errors, data })
92
+ );
93
+ }
94
+
95
+ return Promise.reject(
96
+ new GraphQLError(`Api Error`, { errors, data })
97
+ );
98
+ }
99
+
100
+ return processResponseData(data, entityClasses);
101
+ },
102
+ },
103
+ };
104
+ }
105
+
106
+ function buildTypeToEntityMap(entityClasses) {
107
+ const map = {};
108
+
109
+ entityClasses.forEach((entityClass) => {
110
+ let { entityType } = entityClass;
111
+
112
+ if (!Array.isArray(entityType)) {
113
+ entityType = [entityType];
114
+ }
115
+
116
+ entityType.forEach((type) => (map[type] = entityClass));
117
+ });
118
+
119
+ return map;
120
+ }
121
+
122
+ function processResponseData(data, entityClasses) {
123
+ const processedData = {};
124
+
125
+ if (typeof data === 'string') {
126
+ return data;
127
+ }
128
+ Object.entries(data).forEach(([field, value]) => {
129
+ if (Array.isArray(value)) {
130
+ value = value.map((node) => processResponseData(node, entityClasses));
131
+ } else if (typeof value === 'object' && value !== null) {
132
+ value = processResponseData(value, entityClasses);
133
+ }
134
+
135
+ processedData[field] = value;
136
+ });
137
+
138
+ const type = data.__typename;
139
+ if (!type || !entityClasses[type]) {
140
+ return data;
141
+ }
142
+
143
+ return Reflect.construct(entityClasses[type], [data]);
144
+ }
145
+
146
+ function addTypenameToSelections(document) {
147
+ return visit(document, {
148
+ SelectionSet: {
149
+ enter(node, _key, parent) {
150
+ // Don't add __typename to OperationDefinitions.
151
+ if (parent && parent.kind === 'OperationDefinition') {
152
+ return;
153
+ }
154
+
155
+ // No changes if no selections.
156
+ const { selections } = node;
157
+ if (!selections) {
158
+ return;
159
+ }
160
+
161
+ // If selections already have a __typename, or are part of an
162
+ // 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
+
170
+ if (skip) {
171
+ return;
172
+ }
173
+
174
+ // If this SelectionSet is @export-ed as an input variable, it should
175
+ // 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
+ ) {
181
+ return;
182
+ }
183
+
184
+ // Create and return a new SelectionSet with a __typename Field.
185
+ return Object.assign({}, node, {
186
+ selections: [...selections, TYPENAME_FIELD],
187
+ });
188
+ },
189
+ },
190
+ });
191
+ }
192
+
193
+ export { GraphQLError, UnauthorizedError, graphqlClientPlugin, setEndpointUrl, setEntityClasses };
@@ -0,0 +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(){return{setup:function(r){return b((function*(){return r=p(p({},{graphql:{request:function(r,e){var n=arguments;return b((function*(){var i=n.length>2&&void 0!==n[2]?n[2]:{},u=n.length>3&&void 0!==n[3]?n[3]:{},a=r.$in.graphqlClient,c=a.endpointUrl,l=a.entityClasses,s=u.headers,y=void 0===s?{}:s,d=u.body,b=void 0===d?{}:d,h=f(u,o);e=C(e);var v=(yield r.http.request(p({url:c,method:"POST",headers:p({"Content-Type":"application/json"},y),body:p({query:(0,t.stripIgnoredCharacters)((0,t.print)(e)),variables:i},b)},h))).response.body,m=v.errors,g=v.data,O=void 0===g?{}:g;return m?Array.isArray(m)&&m.some((function(r){return"unauthorized"===r.status}))?Promise.reject(new S("Unauthorized Error",{errors:m,data:O})):Promise.reject(new E("Api Error",{errors:m,data:O})):A(O,l)}))()}}}),r),r.$in.graphqlClient={endpointUrl:"",entityClasses:{}},r}))()},create:function(r){return b((function*(){if(k===q&&!r.$in.httpClient)throw new Error("You must install missing plugin: npm i @merkur/plugin-http-client");return(0,e.bindWidgetToFunctions)(r,r.graphql),r}))()}}},r.setEndpointUrl=function(r,e){r.$in.graphqlClient.endpointUrl=e},r.setEntityClasses=function(r,e){r.$in.graphqlClient.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 l(r)}(r)||function(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}(r)||c(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)return;var n,o,i=[],u=!0,a=!1;try{for(t=t.call(r);!(u=(n=t.next()).done)&&(i.push(n.value),!e||i.length!==e);u=!0);}catch(r){a=!0,o=r}finally{try{u||null==t.return||t.return()}finally{if(a)throw o}}return i}(r,e)||c(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 c(r,e){if(r){if("string"==typeof r)return l(r,e);var t=Object.prototype.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)?l(r,e):void 0}}function l(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function f(r,e){if(null==r)return{};var t,n,o=function(r,e){if(null==r)return{};var t,n,o={},i=Object.keys(r);for(n=0;n<i.length;n++)t=i[n],e.indexOf(t)>=0||(o[t]=r[t]);return o}(r,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(r);for(n=0;n<i.length;n++)t=i[n],e.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(r,t)&&(o[t]=r[t])}return o}function s(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 p(r){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{};e%2?s(Object(t),!0).forEach((function(e){y(r,e,t[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(t)):s(Object(t)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(t,e))}))}return r}function y(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function d(r,e,t,n,o,i,u){try{var a=r[i](u),c=a.value}catch(r){return void t(r)}a.done?e(c):Promise.resolve(c).then(n,o)}function b(r){return function(){var e=this,t=arguments;return new Promise((function(n,o){var i=r.apply(e,t);function u(r){d(i,n,o,u,a,"next",r)}function a(r){d(i,n,o,u,a,"throw",r)}u(void 0)}))}}function h(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,n.key,n)}}function v(r,e,t){return e&&h(r.prototype,e),t&&h(r,t),Object.defineProperty(r,"prototype",{writable:!1}),r}function m(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")}function g(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&&O(r,e)}function O(r,e){return O=Object.setPrototypeOf||function(r,e){return r.__proto__=e,r},O(r,e)}function j(r){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(r){return!1}}();return function(){var t,n=P(r);if(e){var o=P(this).constructor;t=Reflect.construct(n,arguments,o)}else t=n.apply(this,arguments);return w(this,t)}}function w(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)}function P(r){return P=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)},P(r)}var E=function(r){g(t,r);var e=j(t);function t(){return m(this,t),e.apply(this,arguments)}return v(t)}(n.GenericError);r.GraphQLError=E;var S=function(r){g(t,r);var e=j(t);function t(){return m(this,t),e.apply(this,arguments)}return v(t)}(E);r.UnauthorizedError=S;var _={kind:"Field",name:{kind:"Name",value:"__typename"}},q="development",k="undefined"!=typeof process&&process&&process.env?process.env.NODE_ENV:q;function A(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 A(r,e)})):"object"===u(i)&&null!==i&&(i=A(i,e)),t[o]=i}));var n=r.__typename;return n&&e[n]?Reflect.construct(e[n],[r]):r}function C(r){return(0,t.visit)(r,{SelectionSet:{enter:function(r,e,t){if(!t||"OperationDefinition"!==t.kind){var n=r.selections;if(n)if(!(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),[_])})}}}})}}));
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@merkur/plugin-graphql-client",
3
+ "version": "0.29.0",
4
+ "description": "Merkur event emitter plugin.",
5
+ "main": "lib/index",
6
+ "module": "lib/index",
7
+ "unpkg": "lib/index.umd.js",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "import": "./lib/index.mjs",
12
+ "require": "./lib/index.cjs"
13
+ },
14
+ "./lib/index.es9.mjs": "./lib/index.es9.mjs",
15
+ "./lib/index.es9.cjs": "./lib/index.es9.cjs",
16
+ "./webpack": "./webpack.js",
17
+ "./webpack/": "./webpack.js",
18
+ "./webpack.js": "./webpack.js"
19
+ },
20
+ "browser": {
21
+ "./lib/index.js": "./lib/index.es5.js",
22
+ "./lib/index.cjs": "./lib/index.es5.js",
23
+ "./lib/index.mjs": "./lib/index.mjs",
24
+ "./lib/index.es9.mjs": "./lib/index.es9.mjs"
25
+ },
26
+ "scripts": {
27
+ "preversion": "npm test",
28
+ "test": "../../node_modules/.bin/jest --no-watchman -c ./jest.config.js",
29
+ "test:es:version": "../../node_modules/.bin/es-check es5 ./lib/index.es5.js && ../../node_modules/.bin/es-check es11 ./lib/index.mjs --module && ../../node_modules/.bin/es-check es9 ./lib/index.es9.mjs --module && ../../node_modules/.bin/es-check es9 ./lib/index.es9.cjs --module",
30
+ "build": "node_modules/.bin/rollup -c",
31
+ "prepare": "npm run build"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/mjancarik/merkur.git"
36
+ },
37
+ "keywords": [
38
+ "merkur",
39
+ "plugin",
40
+ "microservices",
41
+ "microfrontends"
42
+ ],
43
+ "author": "Miroslav Jancarik",
44
+ "license": "ISC",
45
+ "bugs": {
46
+ "url": "https://github.com/mjancarik/merkur/issues"
47
+ },
48
+ "publishConfig": {
49
+ "registry": "https://registry.npmjs.org/",
50
+ "access": "public"
51
+ },
52
+ "homepage": "https://merkur.js.org/",
53
+ "devDependencies": {
54
+ "@merkur/core": "^0.29.0",
55
+ "@merkur/plugin-component": "^0.29.0",
56
+ "@merkur/plugin-error": "^0.29.0",
57
+ "@merkur/plugin-http-client": "^0.29.0",
58
+ "rollup": "^2.70.2"
59
+ },
60
+ "peerDependencies": {
61
+ "@merkur/core": "*",
62
+ "@merkur/plugin-error": "^0.28.0",
63
+ "@merkur/plugin-http-client": "^0.28.1"
64
+ },
65
+ "dependencies": {
66
+ "graphql": "^15.7.0",
67
+ "graphql-tag": "^2.12.5"
68
+ },
69
+ "gitHead": "ddc13f0ef4d1b18a02461011dae5df49d55f03a6"
70
+ }
package/webpack.js ADDED
@@ -0,0 +1,13 @@
1
+ function applyGraphqlLoader(config) {
2
+ config.module.rules.push({
3
+ test: /\.(graphql|gql)$/,
4
+ exclude: /node_modules/,
5
+ loader: 'graphql-tag/loader',
6
+ });
7
+
8
+ return config;
9
+ }
10
+
11
+ module.exports = {
12
+ applyGraphqlLoader,
13
+ };