eth-graph-query 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.
- package/README.md +76 -0
- package/dist/cjs/core/index.d.ts +12 -0
- package/dist/cjs/core/index.js +21 -0
- package/dist/cjs/core/normal-query.d.ts +14 -0
- package/dist/cjs/core/normal-query.js +28 -0
- package/dist/cjs/core/query-builder.d.ts +10 -0
- package/dist/cjs/core/query-builder.js +74 -0
- package/dist/cjs/data-set.d.ts +5 -0
- package/dist/cjs/data-set.js +8 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/normal-query.d.ts +14 -0
- package/dist/cjs/normal-query.js +28 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/query-builder.d.ts +12 -0
- package/dist/cjs/query-builder.js +108 -0
- package/dist/cjs/type.d.ts +51 -0
- package/dist/cjs/type.js +24 -0
- package/dist/esm/core/index.d.ts +12 -0
- package/dist/esm/core/index.js +18 -0
- package/dist/esm/core/normal-query.d.ts +14 -0
- package/dist/esm/core/normal-query.js +24 -0
- package/dist/esm/core/query-builder.d.ts +10 -0
- package/dist/esm/core/query-builder.js +71 -0
- package/dist/esm/data-set.d.ts +5 -0
- package/dist/esm/data-set.js +5 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/normal-query.d.ts +14 -0
- package/dist/esm/normal-query.js +24 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/query-builder.d.ts +12 -0
- package/dist/esm/query-builder.js +105 -0
- package/dist/esm/type.d.ts +51 -0
- package/dist/esm/type.js +21 -0
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
- package/package.json +89 -0
- package/src/index.ts +24 -0
- package/src/normal-query.ts +34 -0
- package/src/query-builder.ts +89 -0
- package/src/type.ts +86 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
<h1>
|
|
2
|
+
eth-graph-query
|
|
3
|
+
</h1>
|
|
4
|
+
|
|
5
|
+
Simple package for create query command to [the GraphQL](https://thegraph.com/).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
npm install eth-graph-query
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
### Usage
|
|
19
|
+
|
|
20
|
+
- The first thing you have to do is creating a query instance
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const query = new EthGraphQuery(root);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- This package has two query options. To create simple query
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const result = await query.query('collection1', { elements: ['element1', 'element2'] });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- To create a multiple query
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
const result = await query.mergeQuery([
|
|
36
|
+
{ collection: 'collection1', params: { elements: ['element11', 'element12'] } },
|
|
37
|
+
{ collection: 'collection2', params: { elements: ['element21', 'element22'] } },
|
|
38
|
+
]);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
- You can create a complex query
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const result1 = await query.mergeQuery([
|
|
45
|
+
{ collection: 'collection1', params: { elements: ['element11', 'element12'], where: { element11: 'abc' } } },
|
|
46
|
+
{
|
|
47
|
+
collection: 'collection2',
|
|
48
|
+
params: {
|
|
49
|
+
elements: [
|
|
50
|
+
'element21',
|
|
51
|
+
{
|
|
52
|
+
collection: 'collection3',
|
|
53
|
+
params: { elements: ['element31'], where: { id: { in: ['123'] }, element31: 'element31' }, first: 50 },
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
where: { element21: '123', collection3: { element31: '123' } },
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
]);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
### For develop
|
|
65
|
+
|
|
66
|
+
- To test package, you can run below command
|
|
67
|
+
|
|
68
|
+
```shell
|
|
69
|
+
npm run test
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
- Before running above command, you must create `.env` file
|
|
73
|
+
|
|
74
|
+
```shell
|
|
75
|
+
cp .env.example .env
|
|
76
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import NormalQuery from './normal-query';
|
|
3
|
+
import { GraphParams } from '../type';
|
|
4
|
+
export default class CoreGraphQuery extends NormalQuery {
|
|
5
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
6
|
+
protected _fetch(query: string): Promise<any>;
|
|
7
|
+
query(collection: string, params?: GraphParams): Promise<any>;
|
|
8
|
+
mergeQuery(data: Array<{
|
|
9
|
+
collection: string;
|
|
10
|
+
params?: GraphParams;
|
|
11
|
+
}>): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const normal_query_1 = require("./normal-query");
|
|
4
|
+
const query_builder_1 = require("./query-builder");
|
|
5
|
+
class CoreGraphQuery extends normal_query_1.default {
|
|
6
|
+
constructor(rootUrl, config) {
|
|
7
|
+
super(rootUrl, config);
|
|
8
|
+
}
|
|
9
|
+
async _fetch(query) {
|
|
10
|
+
return await this.post('', { query: query });
|
|
11
|
+
}
|
|
12
|
+
async query(collection, params) {
|
|
13
|
+
const sQuery = query_builder_1.default.buildQuery(collection, params);
|
|
14
|
+
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery));
|
|
15
|
+
}
|
|
16
|
+
async mergeQuery(data) {
|
|
17
|
+
const sQuery = query_builder_1.default.mergeQuery(data);
|
|
18
|
+
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.default = CoreGraphQuery;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export declare const defaultHeader: {
|
|
3
|
+
Accept: string;
|
|
4
|
+
'Content-Type': string;
|
|
5
|
+
};
|
|
6
|
+
export default class NormalQuery {
|
|
7
|
+
root: string;
|
|
8
|
+
config: AxiosRequestConfig;
|
|
9
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
10
|
+
protected get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
11
|
+
protected post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
12
|
+
protected put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
+
protected del<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultHeader = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
const axios_1 = require("axios");
|
|
6
|
+
exports.defaultHeader = { Accept: 'application/json', 'Content-Type': 'application/json' };
|
|
7
|
+
function responseBody(res) {
|
|
8
|
+
return res.data;
|
|
9
|
+
}
|
|
10
|
+
class NormalQuery {
|
|
11
|
+
constructor(rootUrl, config) {
|
|
12
|
+
this.root = rootUrl;
|
|
13
|
+
this.config = config ? { ...config, ...{ headers: exports.defaultHeader } } : { headers: exports.defaultHeader };
|
|
14
|
+
}
|
|
15
|
+
async get(url, config) {
|
|
16
|
+
return await axios_1.default.get(`${this.root}${url}`, { ...config, ...this.config }).then(responseBody);
|
|
17
|
+
}
|
|
18
|
+
async post(url, data, config) {
|
|
19
|
+
return await axios_1.default.post(`${this.root}${url}`, data, { ...config, ...this.config }).then(responseBody);
|
|
20
|
+
}
|
|
21
|
+
async put(url, data, config) {
|
|
22
|
+
return await axios_1.default.put(`${this.root}${url}`, data, { ...config, ...this.config }).then(responseBody);
|
|
23
|
+
}
|
|
24
|
+
async del(url, config) {
|
|
25
|
+
return await axios_1.default.delete(`${this.root}${url}`, { ...config, ...this.config }).then(responseBody);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.default = NormalQuery;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GraphParams, QueryJson } from '../type';
|
|
2
|
+
export default class QueryBuilder {
|
|
3
|
+
static buildJsonQuery(query: QueryJson): string;
|
|
4
|
+
static buildQuery(collection: string, params?: GraphParams): string;
|
|
5
|
+
static mergeQuery(data: Array<{
|
|
6
|
+
collection: string;
|
|
7
|
+
params?: GraphParams;
|
|
8
|
+
}>): string;
|
|
9
|
+
static makeFullQuery(query: string): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class QueryBuilder {
|
|
4
|
+
static buildJsonQuery(query) {
|
|
5
|
+
const whereList = [];
|
|
6
|
+
for (const key in query) {
|
|
7
|
+
if (query[key] != undefined) {
|
|
8
|
+
if (Array.isArray(query[key])) {
|
|
9
|
+
const queryArray = query[key];
|
|
10
|
+
whereList.push(`${key}: [${queryArray.map((item) => `"${item}"`).join(', ')}]`);
|
|
11
|
+
}
|
|
12
|
+
else if (typeof query[key] == 'object')
|
|
13
|
+
whereList.push(`${key}: {${this.buildJsonQuery(query[key])}}`);
|
|
14
|
+
else if (typeof query[key] == 'string')
|
|
15
|
+
whereList.push(`${key}: "${query[key]}"`);
|
|
16
|
+
else
|
|
17
|
+
whereList.push(`${key}: ${query[key]}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return whereList.join(', ');
|
|
21
|
+
}
|
|
22
|
+
static buildQuery(collection, params) {
|
|
23
|
+
const filters = [];
|
|
24
|
+
if (params?.id != undefined)
|
|
25
|
+
filters.push(`id: ${params.id}`);
|
|
26
|
+
if (params?.orderBy)
|
|
27
|
+
filters.push(`orderBy: ${params.orderBy}`);
|
|
28
|
+
if (params?.orderDirection)
|
|
29
|
+
filters.push(`orderDirection: ${params.orderDirection}`);
|
|
30
|
+
if (params?.first != undefined) {
|
|
31
|
+
if (params.first < 0)
|
|
32
|
+
params.first = 0;
|
|
33
|
+
else if (params.first > 1000)
|
|
34
|
+
params.first = 1000;
|
|
35
|
+
filters.push(`first: ${params.first}`);
|
|
36
|
+
}
|
|
37
|
+
if (params?.skip != undefined) {
|
|
38
|
+
if (params.skip < 0)
|
|
39
|
+
params.skip = 0;
|
|
40
|
+
else if (params.skip > 5000)
|
|
41
|
+
params.skip = 5000;
|
|
42
|
+
filters.push(`skip: ${params.skip}`);
|
|
43
|
+
}
|
|
44
|
+
if (params?.where) {
|
|
45
|
+
const sWhere = this.buildJsonQuery(params.where);
|
|
46
|
+
if (sWhere.length > 0)
|
|
47
|
+
filters.push(`where: {${sWhere}}`);
|
|
48
|
+
}
|
|
49
|
+
if (params?.block) {
|
|
50
|
+
const sBlock = this.buildJsonQuery(params.block);
|
|
51
|
+
if (sBlock.length > 0)
|
|
52
|
+
filters.push(`block: {${sBlock}}`);
|
|
53
|
+
}
|
|
54
|
+
const filterString = filters.join(', ');
|
|
55
|
+
let elements = ['id'];
|
|
56
|
+
if (params?.elements)
|
|
57
|
+
if (params.elements.length > 0)
|
|
58
|
+
elements = params.elements;
|
|
59
|
+
if (filterString.length > 0)
|
|
60
|
+
return `${collection}(${filterString}) {${elements.join(' ')}}`;
|
|
61
|
+
else
|
|
62
|
+
return `${collection} {${elements.join(' ')}}`;
|
|
63
|
+
}
|
|
64
|
+
static mergeQuery(data) {
|
|
65
|
+
const queries = [];
|
|
66
|
+
for (const item of data)
|
|
67
|
+
queries.push(this.buildQuery(item.collection, item.params));
|
|
68
|
+
return queries.join(' ');
|
|
69
|
+
}
|
|
70
|
+
static makeFullQuery(query) {
|
|
71
|
+
return `query MyQuery {${query}}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.default = QueryBuilder;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import NormalQuery from './normal-query';
|
|
3
|
+
import { GraphParams } from './type';
|
|
4
|
+
export default class CoreGraphQuery extends NormalQuery {
|
|
5
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
6
|
+
protected _fetch(query: string): Promise<any>;
|
|
7
|
+
query(collection: string, params?: GraphParams): Promise<any>;
|
|
8
|
+
mergeQuery(data: Array<{
|
|
9
|
+
collection: string;
|
|
10
|
+
params?: GraphParams;
|
|
11
|
+
}>): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const normal_query_1 = require("./normal-query");
|
|
4
|
+
const query_builder_1 = require("./query-builder");
|
|
5
|
+
class CoreGraphQuery extends normal_query_1.default {
|
|
6
|
+
constructor(rootUrl, config) {
|
|
7
|
+
super(rootUrl, config);
|
|
8
|
+
}
|
|
9
|
+
async _fetch(query) {
|
|
10
|
+
return await this.post('', { query: query });
|
|
11
|
+
}
|
|
12
|
+
async query(collection, params) {
|
|
13
|
+
const sQuery = query_builder_1.default.buildQuery(collection, params);
|
|
14
|
+
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery));
|
|
15
|
+
}
|
|
16
|
+
async mergeQuery(data) {
|
|
17
|
+
const sQuery = query_builder_1.default.mergeQuery(data);
|
|
18
|
+
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.default = CoreGraphQuery;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export declare const defaultHeader: {
|
|
3
|
+
Accept: string;
|
|
4
|
+
'Content-Type': string;
|
|
5
|
+
};
|
|
6
|
+
export default class NormalQuery {
|
|
7
|
+
root: string;
|
|
8
|
+
config: AxiosRequestConfig;
|
|
9
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
10
|
+
protected get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
11
|
+
protected post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
12
|
+
protected put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
+
protected del<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultHeader = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
const axios_1 = require("axios");
|
|
6
|
+
exports.defaultHeader = { Accept: 'application/json', 'Content-Type': 'application/json' };
|
|
7
|
+
function responseBody(res) {
|
|
8
|
+
return res.data;
|
|
9
|
+
}
|
|
10
|
+
class NormalQuery {
|
|
11
|
+
constructor(rootUrl, config) {
|
|
12
|
+
this.root = rootUrl;
|
|
13
|
+
this.config = config ? { ...config, ...{ headers: exports.defaultHeader } } : { headers: exports.defaultHeader };
|
|
14
|
+
}
|
|
15
|
+
async get(url, config) {
|
|
16
|
+
return await axios_1.default.get(`${this.root}${url}`, { ...config, ...this.config }).then(responseBody);
|
|
17
|
+
}
|
|
18
|
+
async post(url, data, config) {
|
|
19
|
+
return await axios_1.default.post(`${this.root}${url}`, data, { ...config, ...this.config }).then(responseBody);
|
|
20
|
+
}
|
|
21
|
+
async put(url, data, config) {
|
|
22
|
+
return await axios_1.default.put(`${this.root}${url}`, data, { ...config, ...this.config }).then(responseBody);
|
|
23
|
+
}
|
|
24
|
+
async del(url, config) {
|
|
25
|
+
return await axios_1.default.delete(`${this.root}${url}`, { ...config, ...this.config }).then(responseBody);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.default = NormalQuery;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ElementType, GraphParams, QueryJson } from './type';
|
|
2
|
+
export default class QueryBuilder {
|
|
3
|
+
static isWhereOptions(data: any): boolean;
|
|
4
|
+
static buildJsonQuery(query: QueryJson): string;
|
|
5
|
+
static buildElements(elements: Array<ElementType>): Array<string>;
|
|
6
|
+
static buildQuery(collection: string, params?: GraphParams): string;
|
|
7
|
+
static mergeQuery(data: Array<{
|
|
8
|
+
collection: string;
|
|
9
|
+
params?: GraphParams;
|
|
10
|
+
}>): string;
|
|
11
|
+
static makeFullQuery(query: string): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const type_1 = require("./type");
|
|
4
|
+
class QueryBuilder {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
static isWhereOptions(data) {
|
|
7
|
+
const keys = Object.keys(data);
|
|
8
|
+
if (keys.length == 0)
|
|
9
|
+
return true;
|
|
10
|
+
for (const key of keys) {
|
|
11
|
+
if (!type_1.OptionKeys.includes(key))
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
static buildJsonQuery(query) {
|
|
17
|
+
const whereList = [];
|
|
18
|
+
for (const key in query) {
|
|
19
|
+
if (query[key] != undefined) {
|
|
20
|
+
if (Array.isArray(query[key])) {
|
|
21
|
+
const queryArray = query[key];
|
|
22
|
+
whereList.push(`${key}: [${queryArray.map((item) => `"${item}"`).join(', ')}]`);
|
|
23
|
+
}
|
|
24
|
+
else if (this.isWhereOptions(query[key])) {
|
|
25
|
+
const realJson = {};
|
|
26
|
+
const options = query[key];
|
|
27
|
+
for (const option in options) {
|
|
28
|
+
const value = options[option];
|
|
29
|
+
if (value)
|
|
30
|
+
realJson[`${key}_${option}`] = value;
|
|
31
|
+
}
|
|
32
|
+
whereList.push(`${this.buildJsonQuery(realJson)}`);
|
|
33
|
+
}
|
|
34
|
+
else if (typeof query[key] == 'object')
|
|
35
|
+
whereList.push(`${key}: {${this.buildJsonQuery(query[key])}}`);
|
|
36
|
+
else if (typeof query[key] == 'string')
|
|
37
|
+
whereList.push(`${key}: "${query[key]}"`);
|
|
38
|
+
else
|
|
39
|
+
whereList.push(`${key}: ${query[key]}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return whereList.join(', ');
|
|
43
|
+
}
|
|
44
|
+
static buildElements(elements) {
|
|
45
|
+
const elementList = [];
|
|
46
|
+
for (const element of elements) {
|
|
47
|
+
if (typeof element == 'string')
|
|
48
|
+
elementList.push(element);
|
|
49
|
+
else {
|
|
50
|
+
const object = element;
|
|
51
|
+
elementList.push(this.buildQuery(object.collection, object.params));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return elementList;
|
|
55
|
+
}
|
|
56
|
+
static buildQuery(collection, params) {
|
|
57
|
+
const filters = [];
|
|
58
|
+
if (params?.id != undefined)
|
|
59
|
+
filters.push(`id: ${params.id}`);
|
|
60
|
+
if (params?.orderBy)
|
|
61
|
+
filters.push(`orderBy: ${params.orderBy}`);
|
|
62
|
+
if (params?.orderDirection)
|
|
63
|
+
filters.push(`orderDirection: ${params.orderDirection}`);
|
|
64
|
+
if (params?.first != undefined) {
|
|
65
|
+
if (params.first < 0)
|
|
66
|
+
params.first = 0;
|
|
67
|
+
else if (params.first > 1000)
|
|
68
|
+
params.first = 1000;
|
|
69
|
+
filters.push(`first: ${params.first}`);
|
|
70
|
+
}
|
|
71
|
+
if (params?.skip != undefined) {
|
|
72
|
+
if (params.skip < 0)
|
|
73
|
+
params.skip = 0;
|
|
74
|
+
else if (params.skip > 5000)
|
|
75
|
+
params.skip = 5000;
|
|
76
|
+
filters.push(`skip: ${params.skip}`);
|
|
77
|
+
}
|
|
78
|
+
if (params?.where) {
|
|
79
|
+
const sWhere = this.buildJsonQuery(params.where);
|
|
80
|
+
if (sWhere.length > 0)
|
|
81
|
+
filters.push(`where: {${sWhere}}`);
|
|
82
|
+
}
|
|
83
|
+
if (params?.block) {
|
|
84
|
+
const sBlock = this.buildJsonQuery(params.block);
|
|
85
|
+
if (sBlock.length > 0)
|
|
86
|
+
filters.push(`block: {${sBlock}}`);
|
|
87
|
+
}
|
|
88
|
+
const filterString = filters.join(', ');
|
|
89
|
+
let elements = ['id'];
|
|
90
|
+
if (params?.elements)
|
|
91
|
+
if (params.elements.length > 0)
|
|
92
|
+
elements = this.buildElements(params.elements);
|
|
93
|
+
if (filterString.length > 0)
|
|
94
|
+
return `${collection}(${filterString}) {${elements.join(' ')}}`;
|
|
95
|
+
else
|
|
96
|
+
return `${collection} {${elements.join(' ')}}`;
|
|
97
|
+
}
|
|
98
|
+
static mergeQuery(data) {
|
|
99
|
+
const queries = [];
|
|
100
|
+
for (const item of data)
|
|
101
|
+
queries.push(this.buildQuery(item.collection, item.params));
|
|
102
|
+
return queries.join(' ');
|
|
103
|
+
}
|
|
104
|
+
static makeFullQuery(query) {
|
|
105
|
+
return `query MyQuery {${query}}`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.default = QueryBuilder;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
type BaseQueryType = Array<string> | string | number | boolean | undefined;
|
|
2
|
+
export declare const OptionKeys: string[];
|
|
3
|
+
export type OptionsKey = 'contains' | 'contains_nocase' | 'ends_with' | 'end_with_nocase' | 'starts_with' | 'starts_with_nocase' | 'not_contains' | 'not_contains_nocase' | 'not_ends_with' | 'not_ends_with_nocase' | 'not_starts_with' | 'not_starts_with_nocase' | 'gt' | 'gte' | 'lt' | 'lte' | 'not' | 'in' | 'not_in';
|
|
4
|
+
export type TextWhereOptions = {
|
|
5
|
+
contains?: BaseQueryType;
|
|
6
|
+
contains_nocase?: BaseQueryType;
|
|
7
|
+
ends_with?: BaseQueryType;
|
|
8
|
+
end_with_nocase?: BaseQueryType;
|
|
9
|
+
starts_with?: BaseQueryType;
|
|
10
|
+
starts_with_nocase?: BaseQueryType;
|
|
11
|
+
not_contains?: BaseQueryType;
|
|
12
|
+
not_contains_nocase?: BaseQueryType;
|
|
13
|
+
not_ends_with?: BaseQueryType;
|
|
14
|
+
not_ends_with_nocase?: BaseQueryType;
|
|
15
|
+
not_starts_with?: BaseQueryType;
|
|
16
|
+
not_starts_with_nocase?: BaseQueryType;
|
|
17
|
+
};
|
|
18
|
+
export type CommonWhereOptions = {
|
|
19
|
+
gt?: BaseQueryType;
|
|
20
|
+
gte?: BaseQueryType;
|
|
21
|
+
lt?: BaseQueryType;
|
|
22
|
+
lte?: BaseQueryType;
|
|
23
|
+
not?: BaseQueryType;
|
|
24
|
+
in?: BaseQueryType;
|
|
25
|
+
not_in?: BaseQueryType;
|
|
26
|
+
};
|
|
27
|
+
export type WhereOptions = TextWhereOptions & CommonWhereOptions;
|
|
28
|
+
export type QueryJson = {
|
|
29
|
+
[key: string]: QueryJson | WhereOptions | BaseQueryType;
|
|
30
|
+
};
|
|
31
|
+
export type BlockQuery = {
|
|
32
|
+
hash?: string;
|
|
33
|
+
number?: number;
|
|
34
|
+
number_gte?: number;
|
|
35
|
+
};
|
|
36
|
+
export type ElementType = string | {
|
|
37
|
+
collection: string;
|
|
38
|
+
params?: GraphParams;
|
|
39
|
+
};
|
|
40
|
+
export interface GraphParams {
|
|
41
|
+
elements?: Array<ElementType>;
|
|
42
|
+
where?: QueryJson;
|
|
43
|
+
id?: string;
|
|
44
|
+
first?: number;
|
|
45
|
+
orderBy?: string;
|
|
46
|
+
orderDirection?: 'asc' | 'desc';
|
|
47
|
+
skip?: number;
|
|
48
|
+
subgraphError?: 'allow' | 'deny';
|
|
49
|
+
block?: BlockQuery;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
package/dist/cjs/type.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OptionKeys = void 0;
|
|
4
|
+
exports.OptionKeys = [
|
|
5
|
+
'contains',
|
|
6
|
+
'contains_nocase',
|
|
7
|
+
'ends_with',
|
|
8
|
+
'end_with_nocase',
|
|
9
|
+
'starts_with',
|
|
10
|
+
'starts_with_nocase',
|
|
11
|
+
'not_contains',
|
|
12
|
+
'not_contains_nocase',
|
|
13
|
+
'not_ends_with',
|
|
14
|
+
'not_ends_with_nocase',
|
|
15
|
+
'not_starts_with',
|
|
16
|
+
'not_starts_with_nocase',
|
|
17
|
+
'gt',
|
|
18
|
+
'gte',
|
|
19
|
+
'lt',
|
|
20
|
+
'lts',
|
|
21
|
+
'not',
|
|
22
|
+
'in',
|
|
23
|
+
'not_in',
|
|
24
|
+
];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import NormalQuery from './normal-query';
|
|
3
|
+
import { GraphParams } from '../type';
|
|
4
|
+
export default class CoreGraphQuery extends NormalQuery {
|
|
5
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
6
|
+
protected _fetch(query: string): Promise<any>;
|
|
7
|
+
query(collection: string, params?: GraphParams): Promise<any>;
|
|
8
|
+
mergeQuery(data: Array<{
|
|
9
|
+
collection: string;
|
|
10
|
+
params?: GraphParams;
|
|
11
|
+
}>): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import NormalQuery from './normal-query';
|
|
2
|
+
import QueryBuilder from './query-builder';
|
|
3
|
+
export default class CoreGraphQuery extends NormalQuery {
|
|
4
|
+
constructor(rootUrl, config) {
|
|
5
|
+
super(rootUrl, config);
|
|
6
|
+
}
|
|
7
|
+
async _fetch(query) {
|
|
8
|
+
return await this.post('', { query: query });
|
|
9
|
+
}
|
|
10
|
+
async query(collection, params) {
|
|
11
|
+
const sQuery = QueryBuilder.buildQuery(collection, params);
|
|
12
|
+
return await this._fetch(QueryBuilder.makeFullQuery(sQuery));
|
|
13
|
+
}
|
|
14
|
+
async mergeQuery(data) {
|
|
15
|
+
const sQuery = QueryBuilder.mergeQuery(data);
|
|
16
|
+
return await this._fetch(QueryBuilder.makeFullQuery(sQuery));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export declare const defaultHeader: {
|
|
3
|
+
Accept: string;
|
|
4
|
+
'Content-Type': string;
|
|
5
|
+
};
|
|
6
|
+
export default class NormalQuery {
|
|
7
|
+
root: string;
|
|
8
|
+
config: AxiosRequestConfig;
|
|
9
|
+
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
10
|
+
protected get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
11
|
+
protected post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
12
|
+
protected put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
+
protected del<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
export const defaultHeader = { Accept: 'application/json', 'Content-Type': 'application/json' };
|
|
4
|
+
function responseBody(res) {
|
|
5
|
+
return res.data;
|
|
6
|
+
}
|
|
7
|
+
export default class NormalQuery {
|
|
8
|
+
constructor(rootUrl, config) {
|
|
9
|
+
this.root = rootUrl;
|
|
10
|
+
this.config = config ? { ...config, ...{ headers: defaultHeader } } : { headers: defaultHeader };
|
|
11
|
+
}
|
|
12
|
+
async get(url, config) {
|
|
13
|
+
return await axios.get(`${this.root}${url}`, { ...config, ...this.config }).then(responseBody);
|
|
14
|
+
}
|
|
15
|
+
async post(url, data, config) {
|
|
16
|
+
return await axios.post(`${this.root}${url}`, data, { ...config, ...this.config }).then(responseBody);
|
|
17
|
+
}
|
|
18
|
+
async put(url, data, config) {
|
|
19
|
+
return await axios.put(`${this.root}${url}`, data, { ...config, ...this.config }).then(responseBody);
|
|
20
|
+
}
|
|
21
|
+
async del(url, config) {
|
|
22
|
+
return await axios.delete(`${this.root}${url}`, { ...config, ...this.config }).then(responseBody);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GraphParams, QueryJson } from '../type';
|
|
2
|
+
export default class QueryBuilder {
|
|
3
|
+
static buildJsonQuery(query: QueryJson): string;
|
|
4
|
+
static buildQuery(collection: string, params?: GraphParams): string;
|
|
5
|
+
static mergeQuery(data: Array<{
|
|
6
|
+
collection: string;
|
|
7
|
+
params?: GraphParams;
|
|
8
|
+
}>): string;
|
|
9
|
+
static makeFullQuery(query: string): string;
|
|
10
|
+
}
|