@operato/scene-graphql 0.1.3

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +59 -0
  4. package/dist/graphql-client.d.ts +29 -0
  5. package/dist/graphql-client.js +143 -0
  6. package/dist/graphql-client.js.map +1 -0
  7. package/dist/graphql-query-and-mutation.d.ts +68 -0
  8. package/dist/graphql-query-and-mutation.js +197 -0
  9. package/dist/graphql-query-and-mutation.js.map +1 -0
  10. package/dist/graphql-subscription.d.ts +28 -0
  11. package/dist/graphql-subscription.js +90 -0
  12. package/dist/graphql-subscription.js.map +1 -0
  13. package/dist/index.d.ts +5 -0
  14. package/dist/index.js +5 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/templates/graphql-client.d.ts +17 -0
  17. package/dist/templates/graphql-client.js +19 -0
  18. package/dist/templates/graphql-client.js.map +1 -0
  19. package/dist/templates/graphql-mutation.d.ts +17 -0
  20. package/dist/templates/graphql-mutation.js +23 -0
  21. package/dist/templates/graphql-mutation.js.map +1 -0
  22. package/dist/templates/graphql-query.d.ts +17 -0
  23. package/dist/templates/graphql-query.js +31 -0
  24. package/dist/templates/graphql-query.js.map +1 -0
  25. package/dist/templates/graphql-subscription.d.ts +18 -0
  26. package/dist/templates/graphql-subscription.js +26 -0
  27. package/dist/templates/graphql-subscription.js.map +1 -0
  28. package/dist/templates/index.d.ts +48 -0
  29. package/dist/templates/index.js +6 -0
  30. package/dist/templates/index.js.map +1 -0
  31. package/helps/scene/component/graphql-subscription.ko.md +19 -0
  32. package/helps/scene/component/graphql-subscription.md +19 -0
  33. package/helps/scene/component/graphql-subscription.zh.md +19 -0
  34. package/helps/scene/component/graphql.ko.md +35 -0
  35. package/helps/scene/component/graphql.md +35 -0
  36. package/helps/scene/component/graphql.zh.md +35 -0
  37. package/icons/icon-graphql-client.png +0 -0
  38. package/icons/icon-graphql-mutation.png +0 -0
  39. package/icons/icon-graphql-query-input.png +0 -0
  40. package/icons/icon-graphql-query.png +0 -0
  41. package/icons/icon-graphql-subscription.png +0 -0
  42. package/icons/no-image.png +0 -0
  43. package/icons/symbol-graphql-client.png +0 -0
  44. package/icons/symbol-graphql-mutation.png +0 -0
  45. package/icons/symbol-graphql-query.png +0 -0
  46. package/icons/symbol-graphql-subscription.png +0 -0
  47. package/package.json +65 -0
  48. package/src/graphql-client.ts +185 -0
  49. package/src/graphql-query-and-mutation.ts +233 -0
  50. package/src/graphql-subscription.ts +110 -0
  51. package/src/index.ts +5 -0
  52. package/src/templates/graphql-client.ts +19 -0
  53. package/src/templates/graphql-mutation.ts +23 -0
  54. package/src/templates/graphql-query.ts +31 -0
  55. package/src/templates/graphql-subscription.ts +26 -0
  56. package/src/templates/index.ts +6 -0
  57. package/things-scene.config.js +5 -0
  58. package/translations/en.json +7 -0
  59. package/translations/ko.json +7 -0
  60. package/translations/zh.json +7 -0
  61. package/tsconfig.json +22 -0
  62. package/tsconfig.tsbuildinfo +1 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ### [0.1.3](https://github.com/things-scene/operato-scene/compare/v0.1.2...v0.1.3) (2022-01-28)
7
+
8
+
9
+ ### :rocket: New Features
10
+
11
+ * add graphql, mqtt scene component ([e223063](https://github.com/things-scene/operato-scene/commit/e223063b562f6a6a7b6ff12f3e32df30aa920da8))
12
+
13
+
14
+ ### :bug: Bug Fix
15
+
16
+ * applied [@operato](https://github.com/operato)@^0.3.20 ([9a1dcbd](https://github.com/things-scene/operato-scene/commit/9a1dcbd2d1ca7f95b498503c87242b6d57ee3482))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Hearty, Oh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # graphql
2
+
3
+ ## graphql을 활용하여, 데이터를 불러오거나 수정하는 컴포넌트가 있다.
4
+
5
+ ## graphql-client
6
+
7
+ **properties: endpoint**
8
+
9
+ 입력받은 endpoint에 데이터를 request하는 client 컴포넌트입니다. 다른 컴포넌트에서 주로 참조하여 사용하기 때문에 ID값을 지정해주어야 합니다.
10
+
11
+ ## graphql-query
12
+
13
+ **properties: client, period, query**
14
+
15
+ graphql-client의 id를 참조하여, 입력한 주기대로 해당 클라이언트에 graphql query를 통해 데이터를 요청하여 받아오는 컴포넌트입니다.
16
+
17
+ ## graphql-quick-bind-query
18
+
19
+ **properties: client, period, query**
20
+
21
+ 위의 graphql-query와 동일한 역할을 하지만, query에 quick-bind기능을 활용하여, 변화하는 값을 쿼리에 넣어 조회할 수 있도록 돕는 컴포넌트입니다. quick-bind 사용방법은 query문 내의 변화하는 참조값을 입력하는 부분에 \${참조할컴포넌트ID.프로퍼티명} 형식으로 입력하면 됩니다. period에 관계없이 데이터가 특정 컴포넌트 값이 변경될 때마다 가져오고 싶은 경우, graphql-quick-bind-query컴포넌트의 value 값으로 데이터를 넘겨주면, 그때마다 데이터를 새로 조회할 수 있습니다.
22
+
23
+ ## graphql-quick-bind-mutation
24
+
25
+ **properties: client, updateGql**
26
+
27
+ updateGql에 따라 해당 클라이언트를 통해 데이터를 업데이트해줍니다. graphql-quick-bind-query와 마찬가지로 quick-bind 방식을 사용할 수 있습니다.
28
+
29
+ ## node package를 설치한다.
30
+
31
+ `$ yarn`
32
+
33
+ ## 실행
34
+
35
+ `$ yarn serve`
36
+ `$ yarn serve:dev`
37
+
38
+ ## 포트를 바꾸려면, -p 3001 식으로 추가해준다.
39
+
40
+ `$ yarn serve`
41
+ `$ yarn serve -p 3001`
42
+
43
+ ## test in browser
44
+
45
+ http://localhost:3000
46
+
47
+ ## build
48
+
49
+ `$ yarn build`
50
+
51
+ | type | filename | for | tested |
52
+ | ---- | -------------------------- | -------------- | ------ |
53
+ | UMD | things-scene-graphql.js | modern browser | O |
54
+ | UMD | things-scene-graphql-ie.js | ie 11 | O |
55
+ | ESM | things-scene-graphql.mjs | modern browser | O |
56
+
57
+ ## publish
58
+
59
+ `$ yarn publish`
@@ -0,0 +1,29 @@
1
+ import { ApolloClient, NormalizedCacheObject } from '@apollo/client/core';
2
+ import { Shape } from '@hatiolab/things-scene';
3
+ declare const GraphqlClient_base: (new (...args: any[]) => {
4
+ isDataSource(): boolean;
5
+ _convertDataFormat(data: any, format: string): any;
6
+ }) & typeof Shape;
7
+ export default class GraphqlClient extends GraphqlClient_base {
8
+ private static _image;
9
+ private _client?;
10
+ static get image(): HTMLImageElement;
11
+ ready(): void;
12
+ init(): void;
13
+ get client(): ApolloClient<NormalizedCacheObject> | undefined;
14
+ dispose(): void;
15
+ render(context: CanvasRenderingContext2D): void;
16
+ onchangeData(data: any, before: any): void;
17
+ get nature(): {
18
+ mutable: boolean;
19
+ resizable: boolean;
20
+ rotatable: boolean;
21
+ properties: {
22
+ type: string;
23
+ label: string;
24
+ name: string;
25
+ }[];
26
+ help: string;
27
+ };
28
+ }
29
+ export {};
@@ -0,0 +1,143 @@
1
+ /*
2
+ * Copyright © HatioLab Inc. All rights reserved.
3
+ */
4
+ import { ApolloClient, from, HttpLink, InMemoryCache } from '@apollo/client/core';
5
+ import { onError } from '@apollo/client/link/error';
6
+ import { Component, DataSource, RectPath, Shape } from '@hatiolab/things-scene';
7
+ const defaultOptions = {
8
+ watchQuery: {
9
+ fetchPolicy: 'no-cache',
10
+ errorPolicy: 'ignore'
11
+ },
12
+ query: {
13
+ fetchPolicy: 'no-cache',
14
+ errorPolicy: 'all'
15
+ },
16
+ mutate: {
17
+ errorPolicy: 'all'
18
+ }
19
+ };
20
+ const ERROR_HANDLER = ({ operation, graphQLErrors, networkError }) => {
21
+ if (graphQLErrors) {
22
+ document.dispatchEvent(new CustomEvent('notify', {
23
+ detail: {
24
+ level: 'error',
25
+ message: graphQLErrors[0].message,
26
+ ex: graphQLErrors
27
+ }
28
+ }));
29
+ }
30
+ if (networkError) {
31
+ /* networkError가 ServerParseError 이거나 ServerError 인 경우에만 statusCode를 갖는다. */
32
+ switch (networkError.statusCode) {
33
+ case undefined /* in case this error is not a server side error */:
34
+ document.dispatchEvent(new CustomEvent('notify', {
35
+ detail: {
36
+ level: 'error',
37
+ message: networkError.message,
38
+ ex: networkError
39
+ }
40
+ }));
41
+ break;
42
+ case 401:
43
+ /* 401 에러가 리턴되면, 인증이 필요하다는 메시지를 dispatch 한다. 이 auth 모듈 등에서 이 메시지를 받아서 signin 프로세스를 진행할 수 있다. */
44
+ document.dispatchEvent(new CustomEvent('auth-required'));
45
+ break;
46
+ case 403:
47
+ /* 403 에러가 리턴되면, 도메인 정보가 필요하다는 메시지를 dispatch 한다. 이 auth 모듈 등에서 이 메시지를 받아서 domain-register 프로세스 등을 진행할 수 있다. */
48
+ document.dispatchEvent(new CustomEvent('domain-required'));
49
+ break;
50
+ default:
51
+ var { name, response, statusCode, bodyText, message } = networkError;
52
+ if (name == 'ServerParseError') {
53
+ message = `[ ${statusCode || ''} : ${response.statusText} ] ${bodyText}`;
54
+ }
55
+ else {
56
+ /* in case this error is instanceof ServerError */
57
+ message = `[ ${statusCode || ''} : ${response.statusText} ] ${message}`;
58
+ }
59
+ document.dispatchEvent(new CustomEvent('notify', {
60
+ detail: {
61
+ level: 'error',
62
+ message,
63
+ ex: networkError
64
+ }
65
+ }));
66
+ }
67
+ }
68
+ };
69
+ const NATURE = {
70
+ mutable: false,
71
+ resizable: true,
72
+ rotatable: true,
73
+ properties: [
74
+ {
75
+ type: 'string',
76
+ label: 'endpoint',
77
+ name: 'endpoint'
78
+ }
79
+ ],
80
+ help: 'scene/component/graphql'
81
+ };
82
+ export default class GraphqlClient extends DataSource(RectPath(Shape)) {
83
+ static get image() {
84
+ if (!GraphqlClient._image) {
85
+ GraphqlClient._image = new Image();
86
+ GraphqlClient._image.src = new URL('../icons/symbol-graphql-client.png', import.meta.url).href;
87
+ }
88
+ return GraphqlClient._image;
89
+ }
90
+ ready() {
91
+ super.ready();
92
+ this.init();
93
+ }
94
+ init() {
95
+ var { endpoint } = this.state;
96
+ if (!endpoint) {
97
+ console.warn('endpoint not defined');
98
+ return;
99
+ }
100
+ var cache = new InMemoryCache();
101
+ const httpOptions = {
102
+ uri: endpoint,
103
+ credentials: 'include'
104
+ };
105
+ const client = new ApolloClient({
106
+ defaultOptions,
107
+ cache,
108
+ link: from([onError(ERROR_HANDLER), new HttpLink(httpOptions)])
109
+ });
110
+ this._client = client;
111
+ }
112
+ get client() {
113
+ return this._client;
114
+ }
115
+ dispose() {
116
+ super.dispose();
117
+ try {
118
+ if (this._client) {
119
+ this._client.stop();
120
+ }
121
+ }
122
+ catch (e) {
123
+ console.error(e);
124
+ }
125
+ delete this._client;
126
+ }
127
+ render(context) {
128
+ /*
129
+ * TODO role이 publisher 인지 subscriber 인지에 따라서 구분할 수 있는 표시를 추가할 것.
130
+ */
131
+ var { left, top, width, height } = this.bounds;
132
+ context.beginPath();
133
+ this.drawImage(context, GraphqlClient.image, left, top, width, height);
134
+ }
135
+ onchangeData(data, before) {
136
+ super.onchangeData(data, before);
137
+ }
138
+ get nature() {
139
+ return NATURE;
140
+ }
141
+ }
142
+ Component.register('graphql-client', GraphqlClient);
143
+ //# sourceMappingURL=graphql-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-client.js","sourceRoot":"","sources":["../src/graphql-client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EACL,YAAY,EAEZ,IAAI,EACJ,QAAQ,EAER,aAAa,EAEd,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAa,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAE/E,MAAM,cAAc,GAAmB;IACrC,UAAU,EAAE;QACV,WAAW,EAAE,UAAU;QACvB,WAAW,EAAE,QAAQ;KACtB;IACD,KAAK,EAAE;QACL,WAAW,EAAE,UAAU;QACvB,WAAW,EAAE,KAAK;KACnB;IACD,MAAM,EAAE;QACN,WAAW,EAAE,KAAK;KACnB;CACF,CAAA;AAED,MAAM,aAAa,GAA2B,CAAC,EAAE,SAAS,EAAE,aAAa,EAAE,YAAY,EAAE,EAAE,EAAE;IAC3F,IAAI,aAAa,EAAE;QACjB,QAAQ,CAAC,aAAa,CACpB,IAAI,WAAW,CAAC,QAAQ,EAAE;YACxB,MAAM,EAAE;gBACN,KAAK,EAAE,OAAO;gBACd,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,OAAO;gBACjC,EAAE,EAAE,aAAa;aAClB;SACF,CAAC,CACH,CAAA;KACF;IAED,IAAI,YAAY,EAAE;QAChB,4EAA4E;QAC5E,QAAS,YAAiC,CAAC,UAAU,EAAE;YACrD,KAAK,SAAS,CAAC,mDAAmD;g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C,OAAO,CAAC,IAAI,EAAE,CAAA;aACpB;SACF;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;SACjB;QACD,OAAO,IAAI,CAAC,OAAO,CAAA;IACrB,CAAC;IAED,MAAM,CAAC,OAAiC;QACtC;;WAEG;QAEH,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAA;QAE9C,OAAO,CAAC,SAAS,EAAE,CAAA;QACnB,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;IACxE,CAAC;IAED,YAAY,CAAC,IAAS,EAAE,MAAW;QACjC,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IAClC,CAAC;IAED,IAAI,MAAM;QACR,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAED,SAAS,CAAC,QAAQ,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAA","sourcesContent":["/*\n * Copyright © HatioLab Inc. All rights reserved.\n */\n\nimport { ServerParseError } from '@apollo/client'\nimport {\n ApolloClient,\n DefaultOptions,\n from,\n HttpLink,\n HttpOptions,\n InMemoryCache,\n NormalizedCacheObject\n} from '@apollo/client/core'\nimport { ErrorLink, onError } from '@apollo/client/link/error'\nimport { Component, DataSource, RectPath, Shape } from '@hatiolab/things-scene'\n\nconst defaultOptions: DefaultOptions = {\n watchQuery: {\n fetchPolicy: 'no-cache',\n errorPolicy: 'ignore'\n },\n query: {\n fetchPolicy: 'no-cache', //'network-only'\n errorPolicy: 'all'\n },\n mutate: {\n errorPolicy: 'all'\n }\n}\n\nconst ERROR_HANDLER: ErrorLink.ErrorHandler = ({ operation, graphQLErrors, networkError }) => {\n if (graphQLErrors) {\n document.dispatchEvent(\n new CustomEvent('notify', {\n detail: {\n level: 'error',\n message: graphQLErrors[0].message,\n ex: graphQLErrors\n }\n })\n )\n }\n\n if (networkError) {\n /* networkError가 ServerParseError 이거나 ServerError 인 경우에만 statusCode를 갖는다. */\n switch ((networkError as ServerParseError).statusCode) {\n case undefined /* in case this error is not a server side error */:\n document.dispatchEvent(\n new CustomEvent('notify', {\n detail: {\n level: 'error',\n message: networkError.message,\n ex: networkError\n }\n })\n )\n break\n\n case 401:\n /* 401 에러가 리턴되면, 인증이 필요하다는 메시지를 dispatch 한다. 이 auth 모듈 등에서 이 메시지를 받아서 signin 프로세스를 진행할 수 있다. */\n document.dispatchEvent(new CustomEvent('auth-required'))\n break\n\n case 403:\n /* 403 에러가 리턴되면, 도메인 정보가 필요하다는 메시지를 dispatch 한다. 이 auth 모듈 등에서 이 메시지를 받아서 domain-register 프로세스 등을 진행할 수 있다. */\n document.dispatchEvent(new CustomEvent('domain-required'))\n break\n\n default:\n var { name, response, statusCode, bodyText, message } = networkError as ServerParseError\n if (name == 'ServerParseError') {\n message = `[ ${statusCode || ''} : ${response.statusText} ] ${bodyText}`\n } else {\n /* in case this error is instanceof ServerError */\n message = `[ ${statusCode || ''} : ${response.statusText} ] ${message}`\n }\n\n document.dispatchEvent(\n new CustomEvent('notify', {\n detail: {\n level: 'error',\n message,\n ex: networkError\n }\n })\n )\n }\n }\n}\n\nconst NATURE = {\n mutable: false,\n resizable: true,\n rotatable: true,\n properties: [\n {\n type: 'string',\n label: 'endpoint',\n name: 'endpoint'\n }\n ],\n help: 'scene/component/graphql'\n}\n\nexport default class GraphqlClient extends DataSource(RectPath(Shape)) {\n private static _image: HTMLImageElement\n private _client?: ApolloClient<NormalizedCacheObject>\n\n static get image() {\n if (!GraphqlClient._image) {\n GraphqlClient._image = new Image()\n GraphqlClient._image.src = new URL('../icons/symbol-graphql-client.png', import.meta.url).href\n }\n\n return GraphqlClient._image\n }\n\n ready() {\n super.ready()\n\n this.init()\n }\n\n init() {\n var { endpoint } = this.state\n\n if (!endpoint) {\n console.warn('endpoint not defined')\n return\n }\n\n var cache = new InMemoryCache()\n const httpOptions: HttpOptions = {\n uri: endpoint,\n credentials: 'include'\n }\n\n const client = new ApolloClient({\n defaultOptions,\n cache,\n link: from([onError(ERROR_HANDLER), new HttpLink(httpOptions)])\n })\n\n this._client = client\n }\n\n get client(): ApolloClient<NormalizedCacheObject> | undefined {\n return this._client\n }\n\n dispose() {\n super.dispose()\n\n try {\n if (this._client) {\n this._client.stop()\n }\n } catch (e) {\n console.error(e)\n }\n delete this._client\n }\n\n render(context: CanvasRenderingContext2D) {\n /*\n * TODO role이 publisher 인지 subscriber 인지에 따라서 구분할 수 있는 표시를 추가할 것.\n */\n\n var { left, top, width, height } = this.bounds\n\n context.beginPath()\n this.drawImage(context, GraphqlClient.image, left, top, width, height)\n }\n\n onchangeData(data: any, before: any) {\n super.onchangeData(data, before)\n }\n\n get nature() {\n return NATURE\n }\n}\n\nComponent.register('graphql-client', GraphqlClient)\n"]}
@@ -0,0 +1,68 @@
1
+ /// <reference types="node" />
2
+ import { ApolloClient, NormalizedCacheObject } from '@apollo/client/core';
3
+ import { Properties, Shape } from '@hatiolab/things-scene';
4
+ declare const GraphQLQuery_base: (new (...args: any[]) => {
5
+ isDataSource(): boolean;
6
+ _convertDataFormat(data: any, format: string): any;
7
+ }) & typeof Shape;
8
+ declare class GraphQLQuery extends GraphQLQuery_base {
9
+ client?: ApolloClient<NormalizedCacheObject>;
10
+ private _repeatTimer?;
11
+ private _isStarted;
12
+ value: any;
13
+ get nature(): {
14
+ mutable: boolean;
15
+ resizable: boolean;
16
+ rotatable: boolean;
17
+ properties: ({
18
+ type: string;
19
+ label: string;
20
+ name: string;
21
+ property: {
22
+ component: string;
23
+ };
24
+ placeholder?: undefined;
25
+ } | {
26
+ type: string;
27
+ label: string;
28
+ name: string;
29
+ placeholder: string;
30
+ property?: undefined;
31
+ } | {
32
+ type: string;
33
+ label: string;
34
+ name: string;
35
+ property?: undefined;
36
+ placeholder?: undefined;
37
+ })[];
38
+ 'value-property': string;
39
+ help: string;
40
+ };
41
+ onchange(after: Properties, before: Properties): void;
42
+ get period(): number;
43
+ set period(period: number);
44
+ get repeatTimer(): NodeJS.Timeout | undefined;
45
+ set repeatTimer(repeatTimer: NodeJS.Timeout | undefined);
46
+ get autoStart(): any;
47
+ set autoStart(autoStart: any);
48
+ get query(): string | undefined;
49
+ get source(): any;
50
+ set source(source: any);
51
+ dispose(): void;
52
+ ready(): void;
53
+ _initGraphqlQuery(): void;
54
+ _stopRepeater(): void;
55
+ _startRepeater(): void;
56
+ requestData(): Promise<void>;
57
+ }
58
+ export declare class Query extends GraphQLQuery {
59
+ private static _image;
60
+ static get image(): HTMLImageElement;
61
+ render(context: CanvasRenderingContext2D): void;
62
+ }
63
+ export declare class Mutation extends GraphQLQuery {
64
+ private static _image;
65
+ static get image(): HTMLImageElement;
66
+ render(context: CanvasRenderingContext2D): void;
67
+ }
68
+ export {};
@@ -0,0 +1,197 @@
1
+ import gql from 'graphql-tag';
2
+ import { Component, DataSource, RectPath, Shape } from '@hatiolab/things-scene';
3
+ import { buildArgs } from '@operato/graphql';
4
+ const NATURE = {
5
+ mutable: false,
6
+ resizable: true,
7
+ rotatable: true,
8
+ properties: [
9
+ {
10
+ type: 'id-input',
11
+ label: 'client',
12
+ name: 'client',
13
+ property: {
14
+ component: 'graphql-client' // component의 type (null or undefined이면 모든 컴포넌트)
15
+ }
16
+ },
17
+ {
18
+ type: 'number',
19
+ label: 'period',
20
+ name: 'period',
21
+ placeholder: 'SECONDS'
22
+ },
23
+ {
24
+ type: 'graphql',
25
+ label: 'query',
26
+ name: 'query'
27
+ },
28
+ {
29
+ type: 'checkbox',
30
+ label: 'auto-start',
31
+ name: 'autoStart'
32
+ }
33
+ ],
34
+ 'value-property': 'value',
35
+ help: 'scene/component/graphql'
36
+ };
37
+ class GraphQLQuery extends DataSource(RectPath(Shape)) {
38
+ constructor() {
39
+ super(...arguments);
40
+ this._isStarted = false;
41
+ }
42
+ get nature() {
43
+ return NATURE;
44
+ }
45
+ onchange(after, before) {
46
+ if ('value' in after) {
47
+ if (after.value)
48
+ this.requestData();
49
+ }
50
+ }
51
+ get period() {
52
+ return this.state.period * 1000;
53
+ }
54
+ set period(period) {
55
+ this.setState('period', period);
56
+ this._initGraphqlQuery();
57
+ }
58
+ get repeatTimer() {
59
+ return this._repeatTimer;
60
+ }
61
+ set repeatTimer(repeatTimer) {
62
+ this._stopRepeater();
63
+ this._repeatTimer = repeatTimer;
64
+ }
65
+ get autoStart() {
66
+ return this.state.autoStart;
67
+ }
68
+ set autoStart(autoStart) {
69
+ this.setState('autoStart', autoStart);
70
+ }
71
+ get query() {
72
+ var _query = this.state.query;
73
+ var objToVal = (exp) => {
74
+ if (typeof exp === 'string')
75
+ return exp;
76
+ else
77
+ return JSON.stringify(exp);
78
+ };
79
+ var changedQuery = (Component.buildSubstitutor(_query, this, objToVal) || (() => _query))();
80
+ try {
81
+ changedQuery = changedQuery.replace(/\(.*\)/gi, params => {
82
+ let paramObject = eval(`({${params.slice(1, -1)}})`);
83
+ return '(' + buildArgs(paramObject) + ')';
84
+ });
85
+ }
86
+ catch (e) {
87
+ console.log(e);
88
+ }
89
+ return changedQuery;
90
+ }
91
+ get source() {
92
+ return this.getState('source');
93
+ }
94
+ set source(source) {
95
+ this.setState('source', source);
96
+ }
97
+ dispose() {
98
+ super.dispose();
99
+ this._stopRepeater();
100
+ }
101
+ ready() {
102
+ super.ready();
103
+ if (this.autoStart) {
104
+ this._initGraphqlQuery();
105
+ }
106
+ }
107
+ _initGraphqlQuery() {
108
+ if (!this.app.isViewMode)
109
+ return;
110
+ this._stopRepeater();
111
+ this._startRepeater();
112
+ }
113
+ _stopRepeater() {
114
+ if (this.repeatTimer !== undefined) {
115
+ clearTimeout(this._repeatTimer);
116
+ delete this._repeatTimer;
117
+ }
118
+ this._isStarted = false;
119
+ }
120
+ _startRepeater() {
121
+ this._isStarted = true;
122
+ // requestAnimationFrame 이 호출되지 않을 때는 requestData 호출도 하지 않도록 함.
123
+ var _ = () => {
124
+ if (!this._isStarted) {
125
+ return;
126
+ }
127
+ this.requestData();
128
+ if (!Number.isNaN(this.period) && this.period > 0) {
129
+ this._repeatTimer = setTimeout(() => {
130
+ requestAnimationFrame(_);
131
+ }, this.period);
132
+ }
133
+ };
134
+ requestAnimationFrame(_);
135
+ }
136
+ async requestData() {
137
+ var _a, _b, _c;
138
+ if (!this.app.isViewMode)
139
+ return;
140
+ var { client } = this.state;
141
+ var query = this.query;
142
+ var variables = this.value;
143
+ if (client && query) {
144
+ this.client = (_a = this.root.findById(client)) === null || _a === void 0 ? void 0 : _a.client;
145
+ var response;
146
+ if (typeof variables === 'object') {
147
+ response = await ((_b = this.client) === null || _b === void 0 ? void 0 : _b.query({
148
+ query: gql `
149
+ ${query}
150
+ `,
151
+ variables: variables
152
+ }));
153
+ }
154
+ else {
155
+ response = await ((_c = this.client) === null || _c === void 0 ? void 0 : _c.query({
156
+ query: gql `
157
+ ${query}
158
+ `,
159
+ errorPolicy: 'all'
160
+ }));
161
+ }
162
+ console.log('response', response);
163
+ this.data = response;
164
+ }
165
+ }
166
+ }
167
+ export class Query extends GraphQLQuery {
168
+ static get image() {
169
+ if (!Query._image) {
170
+ Query._image = new Image();
171
+ Query._image.src = new URL('../icons/symbol-graphql-query.png', import.meta.url).href;
172
+ }
173
+ return Query._image;
174
+ }
175
+ render(context) {
176
+ var { left, top, width, height } = this.bounds;
177
+ context.beginPath();
178
+ this.drawImage(context, Query.image, left, top, width, height);
179
+ }
180
+ }
181
+ export class Mutation extends GraphQLQuery {
182
+ static get image() {
183
+ if (!Mutation._image) {
184
+ Mutation._image = new Image();
185
+ Mutation._image.src = new URL('../icons/symbol-graphql-mutation.png', import.meta.url).href;
186
+ }
187
+ return Mutation._image;
188
+ }
189
+ render(context) {
190
+ var { left, top, width, height } = this.bounds;
191
+ context.beginPath();
192
+ this.drawImage(context, Mutation.image, left, top, width, height);
193
+ }
194
+ }
195
+ Component.register('graphql-query', Query);
196
+ Component.register('graphql-mutation', Mutation);
197
+ //# sourceMappingURL=graphql-query-and-mutation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql-query-and-mutation.js","sourceRoot":"","sources":["../src/graphql-query-and-mutation.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,aAAa,CAAA;AAG7B,OAAO,EAAE,SAAS,EAAE,UAAU,EAAc,QAAQ,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAC3F,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAI5C,MAAM,MAAM,GAAG;IACb,OAAO,EAAE,KAAK;IACd,SAAS,EAAE,IAAI;IACf,SAAS,EAAE,IAAI;IACf,UAAU,EAAE;QACV;YACE,IAAI,EAAE,UAAU;YAChB,KAAK,EAAE,QAAQ;YACf,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE;gBACR,SAAS,EAAE,gBAAgB,CAAC,gDAAgD;aAC7E;SACF;QACD;YACE,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,QAAQ;YACf,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,SAAS;SACvB;QACD;YACE,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,OAAO;YACd,IAAI,EAAE,OAAO;SACd;QACD;YACE,IAAI,EAAE,UAAU;YAChB,KAAK,EAAE,YAAY;YACnB,IAAI,EAAE,WAAW;SAClB;KACF;IACD,gBAAgB,EAAE,OAAO;IACzB,IAAI,EAAE,yBAAyB;CAChC,CAAA;AAED,MAAM,YAAa,SAAQ,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAAtD;;QAIU,eAAU,GAAY,KAAK,CAAA;IA+IrC,CAAC;IA3IC,IAAI,MAAM;QACR,OAAO,MAAM,CAAA;IACf,CAAC;IAED,QAAQ,CAAC,KAAiB,EAAE,MAAkB;QAC5C,IAAI,OAAO,IAAI,KAAK,EAAE;YACpB,IAAI,KAAK,CAAC,KAAK;gBAAE,IAAI,CAAC,WAAW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AE,MAAM,CAAC,CAAA;IACnE,CAAC;CACF;AAED,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,KAAK,CAAC,CAAA;AAC1C,SAAS,CAAC,QAAQ,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAA","sourcesContent":["import gql from 'graphql-tag'\n\nimport { ApolloClient, NormalizedCacheObject } from '@apollo/client/core'\nimport { Component, DataSource, Properties, RectPath, Shape } from '@hatiolab/things-scene'\nimport { buildArgs } from '@operato/graphql'\n\nimport GraphqlClient from './graphql-client'\n\nconst NATURE = {\n mutable: false,\n resizable: true,\n rotatable: true,\n properties: [\n {\n type: 'id-input',\n label: 'client',\n name: 'client',\n property: {\n component: 'graphql-client' // component의 type (null or undefined이면 모든 컴포넌트)\n }\n },\n {\n type: 'number',\n label: 'period',\n name: 'period',\n placeholder: 'SECONDS'\n },\n {\n type: 'graphql',\n label: 'query',\n name: 'query'\n },\n {\n type: 'checkbox',\n label: 'auto-start',\n name: 'autoStart'\n }\n ],\n 'value-property': 'value',\n help: 'scene/component/graphql'\n}\n\nclass GraphQLQuery extends DataSource(RectPath(Shape)) {\n public client?: ApolloClient<NormalizedCacheObject>\n\n private _repeatTimer?: NodeJS.Timeout\n private _isStarted: boolean = false\n\n value: any\n\n get nature() {\n return NATURE\n }\n\n onchange(after: Properties, before: Properties) {\n if ('value' in after) {\n if (after.value) this.requestData()\n }\n }\n\n get period() {\n return this.state.period * 1000\n }\n\n set period(period) {\n this.setState('period', period)\n this._initGraphqlQuery()\n }\n\n get repeatTimer() {\n return this._repeatTimer\n }\n\n set repeatTimer(repeatTimer) {\n this._stopRepeater()\n this._repeatTimer = repeatTimer\n }\n\n get autoStart() {\n return this.state.autoStart\n }\n\n set autoStart(autoStart) {\n this.setState('autoStart', autoStart)\n }\n\n get query() {\n var _query = this.state.query\n var objToVal = (exp: any) => {\n if (typeof exp === 'string') return exp\n else return JSON.stringify(exp)\n }\n var changedQuery: string | undefined = (Component.buildSubstitutor(_query, this, objToVal) || (() => _query))()\n try {\n changedQuery = changedQuery!.replace(/\\(.*\\)/gi, params => {\n let paramObject = eval(`({${params.slice(1, -1)}})`)\n return '(' + buildArgs(paramObject) + ')'\n })\n } catch (e) {\n console.log(e)\n }\n\n return changedQuery\n }\n\n get source() {\n return this.getState('source')\n }\n\n set source(source) {\n this.setState('source', source)\n }\n\n dispose() {\n super.dispose()\n this._stopRepeater()\n }\n\n ready() {\n super.ready()\n if (this.autoStart) {\n this._initGraphqlQuery()\n }\n }\n\n _initGraphqlQuery() {\n if (!this.app.isViewMode) return\n\n this._stopRepeater()\n this._startRepeater()\n }\n\n _stopRepeater() {\n if (this.repeatTimer !== undefined) {\n clearTimeout(this._repeatTimer!)\n delete this._repeatTimer\n }\n\n this._isStarted = false\n }\n\n _startRepeater() {\n this._isStarted = true\n\n // requestAnimationFrame 이 호출되지 않을 때는 requestData 호출도 하지 않도록 함.\n var _ = () => {\n if (!this._isStarted) {\n return\n }\n this.requestData()\n if (!Number.isNaN(this.period) && this.period > 0) {\n this._repeatTimer = setTimeout(() => {\n requestAnimationFrame(_)\n }, this.period)\n }\n }\n requestAnimationFrame(_)\n }\n\n async requestData() {\n if (!this.app.isViewMode) return\n var { client } = this.state\n var query = this.query\n var variables = this.value\n\n if (client && query) {\n this.client = (this.root.findById(client) as GraphqlClient)?.client\n var response\n\n if (typeof variables === 'object') {\n response = await this.client?.query({\n query: gql`\n ${query}\n `,\n variables: variables\n })\n } else {\n response = await this.client?.query({\n query: gql`\n ${query}\n `,\n errorPolicy: 'all'\n })\n }\n\n console.log('response', response)\n this.data = response\n }\n }\n}\n\nexport class Query extends GraphQLQuery {\n private static _image: HTMLImageElement\n\n static get image() {\n if (!Query._image) {\n Query._image = new Image()\n Query._image.src = new URL('../icons/symbol-graphql-query.png', import.meta.url).href\n }\n\n return Query._image\n }\n\n render(context: CanvasRenderingContext2D) {\n var { left, top, width, height } = this.bounds\n\n context.beginPath()\n this.drawImage(context, Query.image, left, top, width, height)\n }\n}\n\nexport class Mutation extends GraphQLQuery {\n private static _image: HTMLImageElement\n\n static get image() {\n if (!Mutation._image) {\n Mutation._image = new Image()\n Mutation._image.src = new URL('../icons/symbol-graphql-mutation.png', import.meta.url).href\n }\n\n return Mutation._image\n }\n\n render(context: CanvasRenderingContext2D) {\n var { left, top, width, height } = this.bounds\n\n context.beginPath()\n this.drawImage(context, Mutation.image, left, top, width, height)\n }\n}\n\nComponent.register('graphql-query', Query)\nComponent.register('graphql-mutation', Mutation)\n"]}
@@ -0,0 +1,28 @@
1
+ import { Shape } from '@hatiolab/things-scene';
2
+ declare const GraphqlSubscription_base: (new (...args: any[]) => {
3
+ isDataSource(): boolean;
4
+ _convertDataFormat(data: any, format: string): any;
5
+ }) & typeof Shape;
6
+ export default class GraphqlSubscription extends GraphqlSubscription_base {
7
+ private static _image;
8
+ private client?;
9
+ private unsubscribe?;
10
+ static get image(): HTMLImageElement;
11
+ dispose(): void;
12
+ render(context: CanvasRenderingContext2D): void;
13
+ ready(): void;
14
+ get nature(): {
15
+ mutable: boolean;
16
+ resizable: boolean;
17
+ rotatable: boolean;
18
+ properties: {
19
+ type: string;
20
+ label: string;
21
+ name: string;
22
+ }[];
23
+ help: string;
24
+ };
25
+ _initGraphqlSubscription(): void;
26
+ requestData(): Promise<void>;
27
+ }
28
+ export {};