eth-graph-query 2.0.1 → 2.0.20
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 +79 -90
- package/dist/.vite/manifest.json +8 -0
- package/dist/api-query.d.ts +67 -0
- package/dist/eth-graph-query.d.ts +41 -0
- package/dist/index.cjs +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2072 -0
- package/dist/query-builder.d.ts +56 -0
- package/dist/type.d.ts +149 -0
- package/package.json +35 -50
- package/dist/cjs/eth-graph-query.d.ts +0 -35
- package/dist/cjs/eth-graph-query.d.ts.map +0 -1
- package/dist/cjs/eth-graph-query.js +0 -69
- package/dist/cjs/eth-graph-query.js.map +0 -1
- package/dist/cjs/index.d.ts +0 -4
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js +0 -22
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/normal-query.d.ts +0 -15
- package/dist/cjs/normal-query.d.ts.map +0 -1
- package/dist/cjs/normal-query.js +0 -65
- package/dist/cjs/normal-query.js.map +0 -1
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/query-builder.d.ts +0 -52
- package/dist/cjs/query-builder.d.ts.map +0 -1
- package/dist/cjs/query-builder.js +0 -238
- package/dist/cjs/query-builder.js.map +0 -1
- package/dist/cjs/type.d.ts +0 -67
- package/dist/cjs/type.d.ts.map +0 -1
- package/dist/cjs/type.js +0 -25
- package/dist/cjs/type.js.map +0 -1
- package/dist/esm/eth-graph-query.d.ts +0 -35
- package/dist/esm/eth-graph-query.d.ts.map +0 -1
- package/dist/esm/eth-graph-query.js +0 -65
- package/dist/esm/eth-graph-query.js.map +0 -1
- package/dist/esm/index.d.ts +0 -4
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/normal-query.d.ts +0 -15
- package/dist/esm/normal-query.d.ts.map +0 -1
- package/dist/esm/normal-query.js +0 -58
- package/dist/esm/normal-query.js.map +0 -1
- package/dist/esm/package.json +0 -3
- package/dist/esm/query-builder.d.ts +0 -52
- package/dist/esm/query-builder.d.ts.map +0 -1
- package/dist/esm/query-builder.js +0 -234
- package/dist/esm/query-builder.js.map +0 -1
- package/dist/esm/type.d.ts +0 -67
- package/dist/esm/type.d.ts.map +0 -1
- package/dist/esm/type.js +0 -22
- package/dist/esm/type.js.map +0 -1
- package/dist/tsconfig.prod.cjs.tsbuildinfo +0 -1
- package/dist/tsconfig.prod.esm.tsbuildinfo +0 -1
- package/src/eth-graph-query.ts +0 -61
- package/src/index.ts +0 -3
- package/src/normal-query.ts +0 -46
- package/src/query-builder.ts +0 -216
- package/src/type.ts +0 -89
package/src/query-builder.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ElementType,
|
|
3
|
-
GraphObject,
|
|
4
|
-
GraphParams,
|
|
5
|
-
InlineFragmentType,
|
|
6
|
-
Metadata,
|
|
7
|
-
OptionKeys,
|
|
8
|
-
QueryJson,
|
|
9
|
-
WhereOptions,
|
|
10
|
-
} from './type.js';
|
|
11
|
-
|
|
12
|
-
export class QueryBuilder {
|
|
13
|
-
/**
|
|
14
|
-
* Create a query string from a query with json format.
|
|
15
|
-
* @param {QueryJson} query the json format query
|
|
16
|
-
* @returns a query string respective with the json format query
|
|
17
|
-
*/
|
|
18
|
-
static buildJsonQuery(query: QueryJson): string {
|
|
19
|
-
const whereList = [];
|
|
20
|
-
for (const key in query) {
|
|
21
|
-
if (query[key] !== undefined) {
|
|
22
|
-
if (query[key] === null) whereList.push(`${key}: null`);
|
|
23
|
-
if (Array.isArray(query[key])) {
|
|
24
|
-
const queryArray = query[key] as Array<string | number | boolean>;
|
|
25
|
-
whereList.push(`${key}: [${queryArray.map((item) => `"${item}"`).join(', ')}]`);
|
|
26
|
-
} else if (typeof query[key] == 'string') {
|
|
27
|
-
whereList.push(`${key}: "${query[key]}"`);
|
|
28
|
-
} else if (typeof query[key] == 'object') {
|
|
29
|
-
const normalJson: QueryJson = {};
|
|
30
|
-
const operatorJson: QueryJson = {};
|
|
31
|
-
const options = query[key] as { [key: string]: QueryJson | WhereOptions };
|
|
32
|
-
for (const option in options) {
|
|
33
|
-
const value = options[option];
|
|
34
|
-
if (option.length == 1) normalJson[option] = value;
|
|
35
|
-
else {
|
|
36
|
-
if (option[0] == '$') {
|
|
37
|
-
const realOperator = option.slice(1);
|
|
38
|
-
if (OptionKeys.includes(realOperator))
|
|
39
|
-
operatorJson[`${key}_${realOperator}`] = value;
|
|
40
|
-
} else normalJson[option] = value;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (Object.keys(normalJson).length > 0)
|
|
44
|
-
whereList.push(`${key}: {${this.buildJsonQuery(normalJson as QueryJson)}}`);
|
|
45
|
-
if (Object.keys(operatorJson).length > 0)
|
|
46
|
-
whereList.push(this.buildJsonQuery(operatorJson as QueryJson));
|
|
47
|
-
} else whereList.push(`${key}: ${query[key]}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return whereList.join(', ');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Given a json format array as element input, returns the string array represent elements you want to query in the graph.
|
|
55
|
-
* @param {Array<ElementType>} elements A array with {@link ElementType} elements, each element in the array represent a element in the graph you want to query
|
|
56
|
-
* @returns {Array<string>} The string array represent the input array
|
|
57
|
-
*/
|
|
58
|
-
static buildElements(elements: Array<ElementType>): Array<string> {
|
|
59
|
-
const elementList = [];
|
|
60
|
-
for (const element of elements) {
|
|
61
|
-
if (typeof element == 'string') elementList.push(element);
|
|
62
|
-
else {
|
|
63
|
-
const object = element as { collection: string; params: GraphParams };
|
|
64
|
-
elementList.push(this.buildQuery({ collection: object.collection, params: object.params }));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return elementList;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Given a instance of {@link Metadata}, returns the string represent the metadata you want to query
|
|
72
|
-
* @param {Metadata} metadata The instance represent all metadata you want to query
|
|
73
|
-
* @returns The string represent the metadata you want to query
|
|
74
|
-
*/
|
|
75
|
-
static buildMetadata(metadata: Metadata): string {
|
|
76
|
-
let result = '';
|
|
77
|
-
const blockQuery = [];
|
|
78
|
-
if (metadata.blockQuery) {
|
|
79
|
-
if (metadata.blockQuery.hash) blockQuery.push(`hash: "${metadata.blockQuery.hash}"`);
|
|
80
|
-
if (metadata.blockQuery.number) blockQuery.push(`number: ${metadata.blockQuery.number}`);
|
|
81
|
-
if (metadata.blockQuery.number_gte)
|
|
82
|
-
blockQuery.push(`number_gte: ${metadata.blockQuery.number_gte}`);
|
|
83
|
-
}
|
|
84
|
-
const sBlockQuery = blockQuery.join(', ');
|
|
85
|
-
if (sBlockQuery.length > 0) result += `(block: {${sBlockQuery}})`;
|
|
86
|
-
const filters: Array<string> = [];
|
|
87
|
-
const blockFilters: Array<string> = [];
|
|
88
|
-
if (metadata.elements) {
|
|
89
|
-
for (const filter of metadata.elements) {
|
|
90
|
-
if (filter == 'deployment' || filter == 'hasIndexingErrors') {
|
|
91
|
-
const sFilter = filter.toString();
|
|
92
|
-
if (!filters.includes(sFilter)) filters.push(sFilter);
|
|
93
|
-
} else {
|
|
94
|
-
const sFilter = filter.toString();
|
|
95
|
-
if (!blockFilters.includes(sFilter)) blockFilters.push(sFilter);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const blockFilterQuery = blockFilters.join(' ');
|
|
99
|
-
if (blockFilterQuery.length > 0) filters.push(`block{${blockFilterQuery}}`);
|
|
100
|
-
const sFiltersQuery = filters.join(' ');
|
|
101
|
-
if (sFiltersQuery.length > 0) result += `{${sFiltersQuery}}`;
|
|
102
|
-
}
|
|
103
|
-
return result.length > 0 ? `_meta${result}` : '';
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private static _buildInlineFragment(fragment: InlineFragmentType): string {
|
|
107
|
-
let elements = ['id'];
|
|
108
|
-
if (fragment.params?.elements) elements = this.buildElements(fragment.params.elements);
|
|
109
|
-
return `... on ${fragment.collection}{${elements.join(' ')}}`;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Given a instance of Array<{@link InlineFragmentType}>, returns the string represent the inline fragments you want to query
|
|
114
|
-
* @param {Array<InlineFragmentType>} fragments The instance represent the inline fragments you want to query
|
|
115
|
-
* @returns The string represent the inline fragments you want to query
|
|
116
|
-
*/
|
|
117
|
-
static buildInlineFragments(fragments: Array<InlineFragmentType>): string {
|
|
118
|
-
const result = [];
|
|
119
|
-
for (const fragment of fragments) {
|
|
120
|
-
result.push(this._buildInlineFragment(fragment));
|
|
121
|
-
}
|
|
122
|
-
return result.join(' ');
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Given json data, returns the string query. This function only can create a string query for a particular collection.
|
|
127
|
-
* @param {GraphObject} data An data for create query, contains two elements:
|
|
128
|
-
* 1. collection: string - collection name
|
|
129
|
-
* 2. params: GraphParams | undefined - If it is defined, it create a query to the collection
|
|
130
|
-
* @param {Metadata | undefined} metadata If it is defined, the query can get metadata that you defined
|
|
131
|
-
* @returns The string query
|
|
132
|
-
*/
|
|
133
|
-
static buildQuery(data: GraphObject, metadata?: Metadata): string {
|
|
134
|
-
const collection = data.collection;
|
|
135
|
-
const params = data.params;
|
|
136
|
-
const filters: Array<string> = [];
|
|
137
|
-
// build id
|
|
138
|
-
if (params?.id != undefined) filters.push(`id: ${params.id}`);
|
|
139
|
-
// build order
|
|
140
|
-
if (params?.orderBy) filters.push(`orderBy: ${params.orderBy}`);
|
|
141
|
-
// build order direction
|
|
142
|
-
if (params?.orderDirection) filters.push(`orderDirection: ${params.orderDirection}`);
|
|
143
|
-
//build first
|
|
144
|
-
if (params?.first != undefined) {
|
|
145
|
-
if (params.first < 0) params.first = 0;
|
|
146
|
-
else if (params.first > 1000) params.first = 1000;
|
|
147
|
-
filters.push(`first: ${params.first}`);
|
|
148
|
-
}
|
|
149
|
-
// build skip
|
|
150
|
-
if (params?.skip != undefined) {
|
|
151
|
-
if (params.skip < 0) params.skip = 0;
|
|
152
|
-
else if (params.skip > 5000) params.skip = 5000;
|
|
153
|
-
filters.push(`skip: ${params.skip}`);
|
|
154
|
-
}
|
|
155
|
-
// build where
|
|
156
|
-
if (params?.where) {
|
|
157
|
-
const sWhere = this.buildJsonQuery(params.where);
|
|
158
|
-
if (sWhere.length > 0) filters.push(`where: {${sWhere}}`);
|
|
159
|
-
}
|
|
160
|
-
// build block
|
|
161
|
-
if (params?.block) {
|
|
162
|
-
const sBlock = this.buildJsonQuery(params.block);
|
|
163
|
-
if (sBlock.length > 0) filters.push(`block: {${sBlock}}`);
|
|
164
|
-
}
|
|
165
|
-
const filterString = filters.join(', ');
|
|
166
|
-
// build elements
|
|
167
|
-
let elements: Array<string> = ['id'];
|
|
168
|
-
if (params?.elements)
|
|
169
|
-
if (params.elements.length > 0) elements = this.buildElements(params.elements);
|
|
170
|
-
// build inline fragments
|
|
171
|
-
let inlineFragments = '';
|
|
172
|
-
if (params?.inlineFragments)
|
|
173
|
-
if (params.inlineFragments.length > 0)
|
|
174
|
-
inlineFragments = this.buildInlineFragments(params.inlineFragments);
|
|
175
|
-
let finalQuery = '';
|
|
176
|
-
const sElements =
|
|
177
|
-
inlineFragments.length > 0 ? `${elements.join(' ')} ${inlineFragments}` : elements.join(' ');
|
|
178
|
-
if (filterString.length > 0) finalQuery = `${collection}(${filterString}) {${sElements}}`;
|
|
179
|
-
else finalQuery = `${collection} {${sElements}}`;
|
|
180
|
-
if (metadata) {
|
|
181
|
-
const sMetadata = this.buildMetadata(metadata);
|
|
182
|
-
if (sMetadata.length > 0) return `${sMetadata} ${finalQuery}`;
|
|
183
|
-
else return finalQuery;
|
|
184
|
-
}
|
|
185
|
-
return finalQuery;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Given a array contain many json data, return a query string represent a query to all collections that is in a array.
|
|
190
|
-
* @param {Array<GraphObject>} data An array contain data to query to many collections
|
|
191
|
-
* @param {Metadata | undefined} metadata If it is defined, the query can get metadata that you defined
|
|
192
|
-
* @returns The query string
|
|
193
|
-
*/
|
|
194
|
-
static buildMultipleQuery(data: Array<GraphObject>, metadata?: Metadata): string {
|
|
195
|
-
const queries: Array<string> = [];
|
|
196
|
-
for (const item of data)
|
|
197
|
-
queries.push(this.buildQuery({ collection: item.collection, params: item.params }));
|
|
198
|
-
const finalQuery = queries.join(' ');
|
|
199
|
-
if (metadata) {
|
|
200
|
-
const sMetadata = this.buildMetadata(metadata);
|
|
201
|
-
if (sMetadata.length > 0) return `${sMetadata} ${finalQuery}`;
|
|
202
|
-
else return finalQuery;
|
|
203
|
-
}
|
|
204
|
-
return finalQuery;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Create complete query string, you can use directly this query to query to the graph
|
|
209
|
-
* @param {string} query The query string
|
|
210
|
-
* @param {string} queryName The query name(default query)
|
|
211
|
-
* @returns The complete query string
|
|
212
|
-
*/
|
|
213
|
-
static makeFullQuery(query: string, queryName = 'query'): string {
|
|
214
|
-
return `query ${queryName} {${query}}`;
|
|
215
|
-
}
|
|
216
|
-
}
|
package/src/type.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
type BaseQueryType =
|
|
2
|
-
| Array<string | number | boolean>
|
|
3
|
-
| string
|
|
4
|
-
| number
|
|
5
|
-
| boolean
|
|
6
|
-
| null
|
|
7
|
-
| undefined;
|
|
8
|
-
|
|
9
|
-
export const OptionKeys = [
|
|
10
|
-
'contains',
|
|
11
|
-
'contains_nocase',
|
|
12
|
-
'ends_with',
|
|
13
|
-
'end_with_nocase',
|
|
14
|
-
'starts_with',
|
|
15
|
-
'starts_with_nocase',
|
|
16
|
-
'not_contains',
|
|
17
|
-
'not_contains_nocase',
|
|
18
|
-
'not_ends_with',
|
|
19
|
-
'not_ends_with_nocase',
|
|
20
|
-
'not_starts_with',
|
|
21
|
-
'not_starts_with_nocase',
|
|
22
|
-
'gt',
|
|
23
|
-
'gte',
|
|
24
|
-
'lt',
|
|
25
|
-
'lte',
|
|
26
|
-
'not',
|
|
27
|
-
'in',
|
|
28
|
-
'not_in',
|
|
29
|
-
];
|
|
30
|
-
|
|
31
|
-
export type TextWhereOptions = {
|
|
32
|
-
$contains?: BaseQueryType;
|
|
33
|
-
$contains_nocase?: BaseQueryType;
|
|
34
|
-
$ends_with?: BaseQueryType;
|
|
35
|
-
$end_with_nocase?: BaseQueryType;
|
|
36
|
-
$starts_with?: BaseQueryType;
|
|
37
|
-
$starts_with_nocase?: BaseQueryType;
|
|
38
|
-
$not_contains?: BaseQueryType;
|
|
39
|
-
$not_contains_nocase?: BaseQueryType;
|
|
40
|
-
$not_ends_with?: BaseQueryType;
|
|
41
|
-
$not_ends_with_nocase?: BaseQueryType;
|
|
42
|
-
$not_starts_with?: BaseQueryType;
|
|
43
|
-
$not_starts_with_nocase?: BaseQueryType;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export type CommonWhereOptions = {
|
|
47
|
-
$gt?: BaseQueryType;
|
|
48
|
-
$gte?: BaseQueryType;
|
|
49
|
-
$lt?: BaseQueryType;
|
|
50
|
-
$lte?: BaseQueryType;
|
|
51
|
-
$not?: BaseQueryType;
|
|
52
|
-
$in?: BaseQueryType;
|
|
53
|
-
$not_in?: BaseQueryType;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export type WhereOptions = TextWhereOptions & CommonWhereOptions;
|
|
57
|
-
|
|
58
|
-
export type QueryJson = { [key: string]: QueryJson | WhereOptions | BaseQueryType };
|
|
59
|
-
export type BlockQuery = { hash?: string; number?: number; number_gte?: number };
|
|
60
|
-
|
|
61
|
-
export type Metadata = {
|
|
62
|
-
elements?: Array<'deployment' | 'hasIndexingErrors' | 'hash' | 'number' | 'timestamp'>;
|
|
63
|
-
blockQuery?: BlockQuery;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export type ElementType = string | GraphObject;
|
|
67
|
-
export type InlineFragmentType = { collection: string; params?: Pick<GraphParams, 'elements'> };
|
|
68
|
-
|
|
69
|
-
export interface GraphParams {
|
|
70
|
-
elements?: Array<ElementType>;
|
|
71
|
-
inlineFragments?: Array<InlineFragmentType>;
|
|
72
|
-
where?: QueryJson;
|
|
73
|
-
id?: string;
|
|
74
|
-
first?: number;
|
|
75
|
-
orderBy?: string;
|
|
76
|
-
orderDirection?: 'asc' | 'desc';
|
|
77
|
-
skip?: number;
|
|
78
|
-
subgraphError?: 'allow' | 'deny';
|
|
79
|
-
block?: BlockQuery;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface GraphObject {
|
|
83
|
-
collection: string;
|
|
84
|
-
params?: GraphParams;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export type ErrorObject = {
|
|
88
|
-
errors: Array<{ message: string; locations: Array<unknown> }>;
|
|
89
|
-
};
|