eth-graph-query 1.0.2 → 1.0.6
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 +38 -5
- package/dist/cjs/index.d.ts +9 -5
- package/dist/cjs/index.js +7 -6
- package/dist/cjs/normal-query.d.ts +4 -4
- package/dist/cjs/query-builder.d.ts +10 -4
- package/dist/cjs/query-builder.js +100 -9
- package/dist/cjs/type.d.ts +38 -2
- package/dist/cjs/type.js +22 -0
- package/dist/esm/index.d.ts +9 -5
- package/dist/esm/index.js +7 -6
- package/dist/esm/normal-query.d.ts +4 -4
- package/dist/esm/query-builder.d.ts +10 -4
- package/dist/esm/query-builder.js +100 -9
- package/dist/esm/type.d.ts +38 -2
- package/dist/esm/type.js +21 -1
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.prod.esm.tsbuildinfo +1 -1
- package/package.json +3 -2
- package/src/index.ts +16 -9
- package/src/normal-query.ts +4 -4
- package/src/query-builder.ts +86 -11
- package/src/type.ts +80 -2
- package/src/index.js +0 -36
- package/src/normal-query.js +0 -45
- package/src/query-builder.js +0 -74
- package/src/type.js +0 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
eth-graph-query
|
|
3
3
|
</h1>
|
|
4
4
|
|
|
5
|
-
Simple package for create query
|
|
5
|
+
Simple package for create query to [the GraphQL](https://thegraph.com/).
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -13,11 +13,17 @@ npm install eth-graph-query
|
|
|
13
13
|
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
- Or if you use `yarn`
|
|
17
|
+
|
|
18
|
+
```shell
|
|
19
|
+
yarn add eth-graph-query
|
|
20
|
+
```
|
|
21
|
+
|
|
16
22
|
---
|
|
17
23
|
|
|
18
24
|
### Usage
|
|
19
25
|
|
|
20
|
-
- The first thing you have to do is
|
|
26
|
+
- The first thing you have to do is creating a query instance
|
|
21
27
|
|
|
22
28
|
```js
|
|
23
29
|
const query = new EthGraphQuery(root);
|
|
@@ -33,13 +39,40 @@ const result = await query.query('collection1', { elements: ['element1', 'elemen
|
|
|
33
39
|
|
|
34
40
|
```js
|
|
35
41
|
const result = await query.mergeQuery([
|
|
36
|
-
{collection: 'collection1', {
|
|
37
|
-
{collection: 'collection2', {
|
|
38
|
-
])
|
|
42
|
+
{ collection: 'collection1', params: { elements: ['element11', 'element12'] } },
|
|
43
|
+
{ collection: 'collection2', params: { elements: ['element21', 'element22'] } },
|
|
44
|
+
]);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- You can create a complex query
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
const result1 = await query.mergeQuery([
|
|
51
|
+
{ collection: 'collection1', params: { elements: ['element11', 'element12'], where: { element11: 'abc' } } },
|
|
52
|
+
{
|
|
53
|
+
collection: 'collection2',
|
|
54
|
+
params: {
|
|
55
|
+
elements: [
|
|
56
|
+
'element21',
|
|
57
|
+
{
|
|
58
|
+
collection: 'collection3',
|
|
59
|
+
params: { elements: ['element31'], where: { id: { in: ['123'] }, element31: 'element31' }, first: 50 },
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
where: { element21: '123', collection3: { element31: '123' } },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
]);
|
|
39
66
|
```
|
|
40
67
|
|
|
41
68
|
---
|
|
42
69
|
|
|
70
|
+
### API
|
|
71
|
+
|
|
72
|
+
Read the [API Docs](https://github.com/phamhongphuc1999/eth-graph-query/blob/main/docs/api.md)
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
43
76
|
### For develop
|
|
44
77
|
|
|
45
78
|
- To test package, you can run below command
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from 'axios';
|
|
2
2
|
import NormalQuery from './normal-query';
|
|
3
|
-
import { GraphParams } from './type';
|
|
3
|
+
import { GraphParams, Metadata } from './type';
|
|
4
4
|
export default class EthGraphQuery extends NormalQuery {
|
|
5
|
+
queryName: string;
|
|
5
6
|
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
6
|
-
|
|
7
|
-
query
|
|
8
|
-
mergeQuery(data: Array<{
|
|
7
|
+
protected _fetch<T>(query: string): Promise<T>;
|
|
8
|
+
query<T = any>(data: {
|
|
9
9
|
collection: string;
|
|
10
10
|
params?: GraphParams;
|
|
11
|
-
}
|
|
11
|
+
}, metadata?: Metadata): Promise<T>;
|
|
12
|
+
mergeQuery<T = any>(data: Array<{
|
|
13
|
+
collection: string;
|
|
14
|
+
params?: GraphParams;
|
|
15
|
+
}>, metadata?: Metadata): Promise<T>;
|
|
12
16
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -5,17 +5,18 @@ const query_builder_1 = require("./query-builder");
|
|
|
5
5
|
class EthGraphQuery extends normal_query_1.default {
|
|
6
6
|
constructor(rootUrl, config) {
|
|
7
7
|
super(rootUrl, config);
|
|
8
|
+
this.queryName = 'query';
|
|
8
9
|
}
|
|
9
10
|
async _fetch(query) {
|
|
10
11
|
return await this.post('', { query: query });
|
|
11
12
|
}
|
|
12
|
-
async query(
|
|
13
|
-
const sQuery = query_builder_1.default.buildQuery(collection, params);
|
|
14
|
-
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery));
|
|
13
|
+
async query(data, metadata) {
|
|
14
|
+
const sQuery = query_builder_1.default.buildQuery({ collection: data.collection, params: data.params }, metadata);
|
|
15
|
+
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery, this.queryName));
|
|
15
16
|
}
|
|
16
|
-
async mergeQuery(data) {
|
|
17
|
-
const sQuery = query_builder_1.default.mergeQuery(data);
|
|
18
|
-
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery));
|
|
17
|
+
async mergeQuery(data, metadata) {
|
|
18
|
+
const sQuery = query_builder_1.default.mergeQuery(data, metadata);
|
|
19
|
+
return await this._fetch(query_builder_1.default.makeFullQuery(sQuery, this.queryName));
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
exports.default = EthGraphQuery;
|
|
@@ -7,8 +7,8 @@ export default class NormalQuery {
|
|
|
7
7
|
root: string;
|
|
8
8
|
config: AxiosRequestConfig;
|
|
9
9
|
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
10
|
-
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
11
|
-
post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
12
|
-
put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
-
del<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
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
14
|
}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { GraphParams, QueryJson } from './type';
|
|
1
|
+
import { ElementType, GraphParams, Metadata, QueryJson } from './type';
|
|
2
2
|
export default class QueryBuilder {
|
|
3
|
+
private static isWhereOptions;
|
|
3
4
|
static buildJsonQuery(query: QueryJson): string;
|
|
4
|
-
static
|
|
5
|
+
static buildElements(elements: Array<ElementType>): Array<string>;
|
|
6
|
+
static buildMetadata(metadata: Metadata): string;
|
|
7
|
+
static buildQuery(data: {
|
|
8
|
+
collection: string;
|
|
9
|
+
params?: GraphParams;
|
|
10
|
+
}, metadata?: Metadata): string;
|
|
5
11
|
static mergeQuery(data: Array<{
|
|
6
12
|
collection: string;
|
|
7
13
|
params?: GraphParams;
|
|
8
|
-
}
|
|
9
|
-
static makeFullQuery(query: string): string;
|
|
14
|
+
}>, metadata?: Metadata): string;
|
|
15
|
+
static makeFullQuery(query: string, queryName?: string): string;
|
|
10
16
|
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const type_1 = require("./type");
|
|
3
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
|
+
}
|
|
4
16
|
static buildJsonQuery(query) {
|
|
5
17
|
const whereList = [];
|
|
6
18
|
for (const key in query) {
|
|
@@ -9,6 +21,16 @@ class QueryBuilder {
|
|
|
9
21
|
const queryArray = query[key];
|
|
10
22
|
whereList.push(`${key}: [${queryArray.map((item) => `"${item}"`).join(', ')}]`);
|
|
11
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
|
+
}
|
|
12
34
|
else if (typeof query[key] == 'object')
|
|
13
35
|
whereList.push(`${key}: {${this.buildJsonQuery(query[key])}}`);
|
|
14
36
|
else if (typeof query[key] == 'string')
|
|
@@ -19,7 +41,59 @@ class QueryBuilder {
|
|
|
19
41
|
}
|
|
20
42
|
return whereList.join(', ');
|
|
21
43
|
}
|
|
22
|
-
static
|
|
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({ collection: object.collection, params: object.params }));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return elementList;
|
|
55
|
+
}
|
|
56
|
+
static buildMetadata(metadata) {
|
|
57
|
+
let result = '';
|
|
58
|
+
const blockQuery = [];
|
|
59
|
+
if (metadata.blockQuery) {
|
|
60
|
+
if (metadata.blockQuery.hash)
|
|
61
|
+
blockQuery.push(`hash: "${metadata.blockQuery.hash}"`);
|
|
62
|
+
if (metadata.blockQuery.number)
|
|
63
|
+
blockQuery.push(`number: ${metadata.blockQuery.number}`);
|
|
64
|
+
if (metadata.blockQuery.number_gte)
|
|
65
|
+
blockQuery.push(`number_gte: ${metadata.blockQuery.number_gte}`);
|
|
66
|
+
}
|
|
67
|
+
const sBlockQuery = blockQuery.join(', ');
|
|
68
|
+
if (sBlockQuery.length > 0)
|
|
69
|
+
result += `(block: {${sBlockQuery}})`;
|
|
70
|
+
const filters = [];
|
|
71
|
+
const blockFilters = [];
|
|
72
|
+
if (metadata.elements) {
|
|
73
|
+
for (const filter of metadata.elements) {
|
|
74
|
+
if (filter == 'deployment' || filter == 'hasIndexingErrors') {
|
|
75
|
+
const sFilter = filter.toString();
|
|
76
|
+
if (!filters.includes(sFilter))
|
|
77
|
+
filters.push(sFilter);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
const sFilter = filter.toString();
|
|
81
|
+
if (!blockFilters.includes(sFilter))
|
|
82
|
+
blockFilters.push(sFilter);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const blockFilterQuery = blockFilters.join(' ');
|
|
86
|
+
if (blockFilterQuery.length > 0)
|
|
87
|
+
filters.push(`block{${blockFilterQuery}}`);
|
|
88
|
+
const sFiltersQuery = filters.join(' ');
|
|
89
|
+
if (sFiltersQuery.length > 0)
|
|
90
|
+
result += `{${sFiltersQuery}}`;
|
|
91
|
+
}
|
|
92
|
+
return result.length > 0 ? `_meta${result}` : '';
|
|
93
|
+
}
|
|
94
|
+
static buildQuery(data, metadata) {
|
|
95
|
+
const collection = data.collection;
|
|
96
|
+
const params = data.params;
|
|
23
97
|
const filters = [];
|
|
24
98
|
if (params?.id != undefined)
|
|
25
99
|
filters.push(`id: ${params.id}`);
|
|
@@ -55,20 +129,37 @@ class QueryBuilder {
|
|
|
55
129
|
let elements = ['id'];
|
|
56
130
|
if (params?.elements)
|
|
57
131
|
if (params.elements.length > 0)
|
|
58
|
-
elements = params.elements;
|
|
132
|
+
elements = this.buildElements(params.elements);
|
|
133
|
+
let finalQuery = '';
|
|
59
134
|
if (filterString.length > 0)
|
|
60
|
-
|
|
135
|
+
finalQuery = `${collection}(${filterString}) {${elements.join(' ')}}`;
|
|
61
136
|
else
|
|
62
|
-
|
|
137
|
+
finalQuery = `${collection} {${elements.join(' ')}}`;
|
|
138
|
+
if (metadata) {
|
|
139
|
+
const sMetadata = this.buildMetadata(metadata);
|
|
140
|
+
if (sMetadata.length > 0)
|
|
141
|
+
return `${sMetadata} ${finalQuery}`;
|
|
142
|
+
else
|
|
143
|
+
return finalQuery;
|
|
144
|
+
}
|
|
145
|
+
return finalQuery;
|
|
63
146
|
}
|
|
64
|
-
static mergeQuery(data) {
|
|
147
|
+
static mergeQuery(data, metadata) {
|
|
65
148
|
const queries = [];
|
|
66
149
|
for (const item of data)
|
|
67
|
-
queries.push(this.buildQuery(item.collection, item.params));
|
|
68
|
-
|
|
150
|
+
queries.push(this.buildQuery({ collection: item.collection, params: item.params }));
|
|
151
|
+
const finalQuery = queries.join(' ');
|
|
152
|
+
if (metadata) {
|
|
153
|
+
const sMetadata = this.buildMetadata(metadata);
|
|
154
|
+
if (sMetadata.length > 0)
|
|
155
|
+
return `${sMetadata} ${finalQuery}`;
|
|
156
|
+
else
|
|
157
|
+
return finalQuery;
|
|
158
|
+
}
|
|
159
|
+
return finalQuery;
|
|
69
160
|
}
|
|
70
|
-
static makeFullQuery(query) {
|
|
71
|
-
return `query
|
|
161
|
+
static makeFullQuery(query, queryName = 'query') {
|
|
162
|
+
return `query ${queryName} {${query}}`;
|
|
72
163
|
}
|
|
73
164
|
}
|
|
74
165
|
exports.default = QueryBuilder;
|
package/dist/cjs/type.d.ts
CHANGED
|
@@ -1,13 +1,48 @@
|
|
|
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;
|
|
1
28
|
export type QueryJson = {
|
|
2
|
-
[key: string]: QueryJson |
|
|
29
|
+
[key: string]: QueryJson | WhereOptions | BaseQueryType;
|
|
3
30
|
};
|
|
4
31
|
export type BlockQuery = {
|
|
5
32
|
hash?: string;
|
|
6
33
|
number?: number;
|
|
7
34
|
number_gte?: number;
|
|
8
35
|
};
|
|
36
|
+
export type Metadata = {
|
|
37
|
+
elements?: Array<'deployment' | 'hasIndexingErrors' | 'hash' | 'number' | 'timestamp'>;
|
|
38
|
+
blockQuery?: BlockQuery;
|
|
39
|
+
};
|
|
40
|
+
export type ElementType = string | {
|
|
41
|
+
collection: string;
|
|
42
|
+
params?: GraphParams;
|
|
43
|
+
};
|
|
9
44
|
export interface GraphParams {
|
|
10
|
-
elements?: Array<
|
|
45
|
+
elements?: Array<ElementType>;
|
|
11
46
|
where?: QueryJson;
|
|
12
47
|
id?: string;
|
|
13
48
|
first?: number;
|
|
@@ -17,3 +52,4 @@ export interface GraphParams {
|
|
|
17
52
|
subgraphError?: 'allow' | 'deny';
|
|
18
53
|
block?: BlockQuery;
|
|
19
54
|
}
|
|
55
|
+
export {};
|
package/dist/cjs/type.js
CHANGED
|
@@ -1,2 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
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
|
+
'lte',
|
|
21
|
+
'not',
|
|
22
|
+
'in',
|
|
23
|
+
'not_in',
|
|
24
|
+
];
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from 'axios';
|
|
2
2
|
import NormalQuery from './normal-query';
|
|
3
|
-
import { GraphParams } from './type';
|
|
3
|
+
import { GraphParams, Metadata } from './type';
|
|
4
4
|
export default class EthGraphQuery extends NormalQuery {
|
|
5
|
+
queryName: string;
|
|
5
6
|
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
6
|
-
|
|
7
|
-
query
|
|
8
|
-
mergeQuery(data: Array<{
|
|
7
|
+
protected _fetch<T>(query: string): Promise<T>;
|
|
8
|
+
query<T = any>(data: {
|
|
9
9
|
collection: string;
|
|
10
10
|
params?: GraphParams;
|
|
11
|
-
}
|
|
11
|
+
}, metadata?: Metadata): Promise<T>;
|
|
12
|
+
mergeQuery<T = any>(data: Array<{
|
|
13
|
+
collection: string;
|
|
14
|
+
params?: GraphParams;
|
|
15
|
+
}>, metadata?: Metadata): Promise<T>;
|
|
12
16
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -3,16 +3,17 @@ import QueryBuilder from './query-builder';
|
|
|
3
3
|
export default class EthGraphQuery extends NormalQuery {
|
|
4
4
|
constructor(rootUrl, config) {
|
|
5
5
|
super(rootUrl, config);
|
|
6
|
+
this.queryName = 'query';
|
|
6
7
|
}
|
|
7
8
|
async _fetch(query) {
|
|
8
9
|
return await this.post('', { query: query });
|
|
9
10
|
}
|
|
10
|
-
async query(
|
|
11
|
-
const sQuery = QueryBuilder.buildQuery(collection, params);
|
|
12
|
-
return await this._fetch(QueryBuilder.makeFullQuery(sQuery));
|
|
11
|
+
async query(data, metadata) {
|
|
12
|
+
const sQuery = QueryBuilder.buildQuery({ collection: data.collection, params: data.params }, metadata);
|
|
13
|
+
return await this._fetch(QueryBuilder.makeFullQuery(sQuery, this.queryName));
|
|
13
14
|
}
|
|
14
|
-
async mergeQuery(data) {
|
|
15
|
-
const sQuery = QueryBuilder.mergeQuery(data);
|
|
16
|
-
return await this._fetch(QueryBuilder.makeFullQuery(sQuery));
|
|
15
|
+
async mergeQuery(data, metadata) {
|
|
16
|
+
const sQuery = QueryBuilder.mergeQuery(data, metadata);
|
|
17
|
+
return await this._fetch(QueryBuilder.makeFullQuery(sQuery, this.queryName));
|
|
17
18
|
}
|
|
18
19
|
}
|
|
@@ -7,8 +7,8 @@ export default class NormalQuery {
|
|
|
7
7
|
root: string;
|
|
8
8
|
config: AxiosRequestConfig;
|
|
9
9
|
constructor(rootUrl: string, config?: AxiosRequestConfig);
|
|
10
|
-
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
11
|
-
post<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
12
|
-
put<B = any, T = any>(url: string, data?: B, config?: AxiosRequestConfig): Promise<T>;
|
|
13
|
-
del<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
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
14
|
}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { GraphParams, QueryJson } from './type';
|
|
1
|
+
import { ElementType, GraphParams, Metadata, QueryJson } from './type';
|
|
2
2
|
export default class QueryBuilder {
|
|
3
|
+
private static isWhereOptions;
|
|
3
4
|
static buildJsonQuery(query: QueryJson): string;
|
|
4
|
-
static
|
|
5
|
+
static buildElements(elements: Array<ElementType>): Array<string>;
|
|
6
|
+
static buildMetadata(metadata: Metadata): string;
|
|
7
|
+
static buildQuery(data: {
|
|
8
|
+
collection: string;
|
|
9
|
+
params?: GraphParams;
|
|
10
|
+
}, metadata?: Metadata): string;
|
|
5
11
|
static mergeQuery(data: Array<{
|
|
6
12
|
collection: string;
|
|
7
13
|
params?: GraphParams;
|
|
8
|
-
}
|
|
9
|
-
static makeFullQuery(query: string): string;
|
|
14
|
+
}>, metadata?: Metadata): string;
|
|
15
|
+
static makeFullQuery(query: string, queryName?: string): string;
|
|
10
16
|
}
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
+
import { OptionKeys } from './type';
|
|
1
2
|
export default class QueryBuilder {
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
static isWhereOptions(data) {
|
|
5
|
+
const keys = Object.keys(data);
|
|
6
|
+
if (keys.length == 0)
|
|
7
|
+
return true;
|
|
8
|
+
for (const key of keys) {
|
|
9
|
+
if (!OptionKeys.includes(key))
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
2
14
|
static buildJsonQuery(query) {
|
|
3
15
|
const whereList = [];
|
|
4
16
|
for (const key in query) {
|
|
@@ -7,6 +19,16 @@ export default class QueryBuilder {
|
|
|
7
19
|
const queryArray = query[key];
|
|
8
20
|
whereList.push(`${key}: [${queryArray.map((item) => `"${item}"`).join(', ')}]`);
|
|
9
21
|
}
|
|
22
|
+
else if (this.isWhereOptions(query[key])) {
|
|
23
|
+
const realJson = {};
|
|
24
|
+
const options = query[key];
|
|
25
|
+
for (const option in options) {
|
|
26
|
+
const value = options[option];
|
|
27
|
+
if (value)
|
|
28
|
+
realJson[`${key}_${option}`] = value;
|
|
29
|
+
}
|
|
30
|
+
whereList.push(`${this.buildJsonQuery(realJson)}`);
|
|
31
|
+
}
|
|
10
32
|
else if (typeof query[key] == 'object')
|
|
11
33
|
whereList.push(`${key}: {${this.buildJsonQuery(query[key])}}`);
|
|
12
34
|
else if (typeof query[key] == 'string')
|
|
@@ -17,7 +39,59 @@ export default class QueryBuilder {
|
|
|
17
39
|
}
|
|
18
40
|
return whereList.join(', ');
|
|
19
41
|
}
|
|
20
|
-
static
|
|
42
|
+
static buildElements(elements) {
|
|
43
|
+
const elementList = [];
|
|
44
|
+
for (const element of elements) {
|
|
45
|
+
if (typeof element == 'string')
|
|
46
|
+
elementList.push(element);
|
|
47
|
+
else {
|
|
48
|
+
const object = element;
|
|
49
|
+
elementList.push(this.buildQuery({ collection: object.collection, params: object.params }));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return elementList;
|
|
53
|
+
}
|
|
54
|
+
static buildMetadata(metadata) {
|
|
55
|
+
let result = '';
|
|
56
|
+
const blockQuery = [];
|
|
57
|
+
if (metadata.blockQuery) {
|
|
58
|
+
if (metadata.blockQuery.hash)
|
|
59
|
+
blockQuery.push(`hash: "${metadata.blockQuery.hash}"`);
|
|
60
|
+
if (metadata.blockQuery.number)
|
|
61
|
+
blockQuery.push(`number: ${metadata.blockQuery.number}`);
|
|
62
|
+
if (metadata.blockQuery.number_gte)
|
|
63
|
+
blockQuery.push(`number_gte: ${metadata.blockQuery.number_gte}`);
|
|
64
|
+
}
|
|
65
|
+
const sBlockQuery = blockQuery.join(', ');
|
|
66
|
+
if (sBlockQuery.length > 0)
|
|
67
|
+
result += `(block: {${sBlockQuery}})`;
|
|
68
|
+
const filters = [];
|
|
69
|
+
const blockFilters = [];
|
|
70
|
+
if (metadata.elements) {
|
|
71
|
+
for (const filter of metadata.elements) {
|
|
72
|
+
if (filter == 'deployment' || filter == 'hasIndexingErrors') {
|
|
73
|
+
const sFilter = filter.toString();
|
|
74
|
+
if (!filters.includes(sFilter))
|
|
75
|
+
filters.push(sFilter);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
const sFilter = filter.toString();
|
|
79
|
+
if (!blockFilters.includes(sFilter))
|
|
80
|
+
blockFilters.push(sFilter);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const blockFilterQuery = blockFilters.join(' ');
|
|
84
|
+
if (blockFilterQuery.length > 0)
|
|
85
|
+
filters.push(`block{${blockFilterQuery}}`);
|
|
86
|
+
const sFiltersQuery = filters.join(' ');
|
|
87
|
+
if (sFiltersQuery.length > 0)
|
|
88
|
+
result += `{${sFiltersQuery}}`;
|
|
89
|
+
}
|
|
90
|
+
return result.length > 0 ? `_meta${result}` : '';
|
|
91
|
+
}
|
|
92
|
+
static buildQuery(data, metadata) {
|
|
93
|
+
const collection = data.collection;
|
|
94
|
+
const params = data.params;
|
|
21
95
|
const filters = [];
|
|
22
96
|
if (params?.id != undefined)
|
|
23
97
|
filters.push(`id: ${params.id}`);
|
|
@@ -53,19 +127,36 @@ export default class QueryBuilder {
|
|
|
53
127
|
let elements = ['id'];
|
|
54
128
|
if (params?.elements)
|
|
55
129
|
if (params.elements.length > 0)
|
|
56
|
-
elements = params.elements;
|
|
130
|
+
elements = this.buildElements(params.elements);
|
|
131
|
+
let finalQuery = '';
|
|
57
132
|
if (filterString.length > 0)
|
|
58
|
-
|
|
133
|
+
finalQuery = `${collection}(${filterString}) {${elements.join(' ')}}`;
|
|
59
134
|
else
|
|
60
|
-
|
|
135
|
+
finalQuery = `${collection} {${elements.join(' ')}}`;
|
|
136
|
+
if (metadata) {
|
|
137
|
+
const sMetadata = this.buildMetadata(metadata);
|
|
138
|
+
if (sMetadata.length > 0)
|
|
139
|
+
return `${sMetadata} ${finalQuery}`;
|
|
140
|
+
else
|
|
141
|
+
return finalQuery;
|
|
142
|
+
}
|
|
143
|
+
return finalQuery;
|
|
61
144
|
}
|
|
62
|
-
static mergeQuery(data) {
|
|
145
|
+
static mergeQuery(data, metadata) {
|
|
63
146
|
const queries = [];
|
|
64
147
|
for (const item of data)
|
|
65
|
-
queries.push(this.buildQuery(item.collection, item.params));
|
|
66
|
-
|
|
148
|
+
queries.push(this.buildQuery({ collection: item.collection, params: item.params }));
|
|
149
|
+
const finalQuery = queries.join(' ');
|
|
150
|
+
if (metadata) {
|
|
151
|
+
const sMetadata = this.buildMetadata(metadata);
|
|
152
|
+
if (sMetadata.length > 0)
|
|
153
|
+
return `${sMetadata} ${finalQuery}`;
|
|
154
|
+
else
|
|
155
|
+
return finalQuery;
|
|
156
|
+
}
|
|
157
|
+
return finalQuery;
|
|
67
158
|
}
|
|
68
|
-
static makeFullQuery(query) {
|
|
69
|
-
return `query
|
|
159
|
+
static makeFullQuery(query, queryName = 'query') {
|
|
160
|
+
return `query ${queryName} {${query}}`;
|
|
70
161
|
}
|
|
71
162
|
}
|
package/dist/esm/type.d.ts
CHANGED
|
@@ -1,13 +1,48 @@
|
|
|
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;
|
|
1
28
|
export type QueryJson = {
|
|
2
|
-
[key: string]: QueryJson |
|
|
29
|
+
[key: string]: QueryJson | WhereOptions | BaseQueryType;
|
|
3
30
|
};
|
|
4
31
|
export type BlockQuery = {
|
|
5
32
|
hash?: string;
|
|
6
33
|
number?: number;
|
|
7
34
|
number_gte?: number;
|
|
8
35
|
};
|
|
36
|
+
export type Metadata = {
|
|
37
|
+
elements?: Array<'deployment' | 'hasIndexingErrors' | 'hash' | 'number' | 'timestamp'>;
|
|
38
|
+
blockQuery?: BlockQuery;
|
|
39
|
+
};
|
|
40
|
+
export type ElementType = string | {
|
|
41
|
+
collection: string;
|
|
42
|
+
params?: GraphParams;
|
|
43
|
+
};
|
|
9
44
|
export interface GraphParams {
|
|
10
|
-
elements?: Array<
|
|
45
|
+
elements?: Array<ElementType>;
|
|
11
46
|
where?: QueryJson;
|
|
12
47
|
id?: string;
|
|
13
48
|
first?: number;
|
|
@@ -17,3 +52,4 @@ export interface GraphParams {
|
|
|
17
52
|
subgraphError?: 'allow' | 'deny';
|
|
18
53
|
block?: BlockQuery;
|
|
19
54
|
}
|
|
55
|
+
export {};
|