@skedulo/pulse-solution-services 0.0.14 → 0.0.16
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/CHANGELOG.md +9 -0
- package/README.md +115 -6
- package/dist/clients/graphql-client.js +1 -1
- package/dist/index.d.ts +102 -37
- package/dist/services/graphql/graphql-batch-builder.d.ts +138 -0
- package/dist/services/graphql/graphql-batch-builder.js +1 -0
- package/dist/services/graphql/graphql-query-batch.d.ts +10 -0
- package/dist/services/graphql/graphql-query-batch.js +1 -0
- package/dist/services/graphql/graphql-query-builder.d.ts +11 -3
- package/dist/services/graphql/graphql-query-builder.js +1 -1
- package/dist/services/graphql/graphql-service.d.ts +78 -25
- package/dist/services/graphql/graphql-service.js +1 -1
- package/dist/services/graphql/queries.d.ts +13 -19
- package/dist/services/graphql/queries.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [v0.0.16] - 2025-06-19
|
|
4
|
+
### Added
|
|
5
|
+
- Support upsert mutation.
|
|
6
|
+
- Support batch queries and mutations.
|
|
7
|
+
|
|
8
|
+
## [v0.0.15] - 2025-05-21
|
|
9
|
+
### Updated
|
|
10
|
+
- Enable GraphQLRequest with variable to graphqlClient
|
|
11
|
+
|
|
3
12
|
## [v0.0.14] - 2025-05-21
|
|
4
13
|
### Added
|
|
5
14
|
- Add graphql mutation option to suppress change events.
|
package/README.md
CHANGED
|
@@ -103,7 +103,7 @@ const artifactClient = context.newArtifactClient(ArtifactType.WEBHOOK);
|
|
|
103
103
|
const webhooks = await artifactClient.list();
|
|
104
104
|
const functionPromises = webhooks.map((webhook: any) => artifactClient.get(webhook.name));
|
|
105
105
|
await Promise.all(functionPromises);
|
|
106
|
-
// Get execution
|
|
106
|
+
// Get execution metrics
|
|
107
107
|
console.log(context.getExecutionMetrics());
|
|
108
108
|
```
|
|
109
109
|
|
|
@@ -187,7 +187,15 @@ const updatedVocabularyItem = await context.vocabularyClient.updateVocabularyIte
|
|
|
187
187
|
#### GraphQL Client
|
|
188
188
|
Executes GraphQL queries and mutations against `/graphql/graphql`.
|
|
189
189
|
```javascript
|
|
190
|
+
// execute final query string
|
|
190
191
|
const queryResult = await context.graphqlClient.execute(queryString, {readOnly: true});
|
|
192
|
+
|
|
193
|
+
// using query string with variables
|
|
194
|
+
const graphQLRequest: GraphQLRequest = {
|
|
195
|
+
query: queryString,
|
|
196
|
+
variables: {}
|
|
197
|
+
}
|
|
198
|
+
const queryUsingVariablesResult = await context.graphqlClient.execute(graphQLRequest, {readOnly: true});
|
|
191
199
|
```
|
|
192
200
|
|
|
193
201
|
#### Config Variable Client
|
|
@@ -523,6 +531,58 @@ console.log(queryResult.endOffset); // 1
|
|
|
523
531
|
|
|
524
532
|
The `X-Graphql-Operation` header is added to all GraphQL requests and contains the value of `operationName`.
|
|
525
533
|
|
|
534
|
+
#### Query by Pages
|
|
535
|
+
```javascript
|
|
536
|
+
// Create a query builder for the Jobs object
|
|
537
|
+
const queryBuilder = context.newQueryBuilder({
|
|
538
|
+
objectName: 'Jobs',
|
|
539
|
+
operationName: 'fetchJobs',
|
|
540
|
+
readOnly: true
|
|
541
|
+
});
|
|
542
|
+
// Fetch data from pages 1 through 5 (inclusive)
|
|
543
|
+
const queryResult = await queryBuilder.executeAll(1, 5);
|
|
544
|
+
|
|
545
|
+
// Fetch data from the first 20 pages
|
|
546
|
+
const queryResult = await queryBuilder.executeAll();
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
The `queryBuilder.executeAll` method supports querying up to a maximum of 20 pages.
|
|
550
|
+
|
|
551
|
+
#### Batch Queries
|
|
552
|
+
|
|
553
|
+
To execute multiple queries in a single request, use `graphqlService.queryBatch`. This method takes an array of query builders, sends them to the GraphQL batch endpoint, and returns an array of query results. This is useful for fetching data from multiple objects efficiently.
|
|
554
|
+
|
|
555
|
+
```javascript
|
|
556
|
+
// Create query builders for Jobs and Contacts
|
|
557
|
+
const jobQueryBuilder = context.newQueryBuilder({
|
|
558
|
+
objectName: 'Jobs',
|
|
559
|
+
operationName: 'fetchJobs',
|
|
560
|
+
readOnly: true
|
|
561
|
+
});
|
|
562
|
+
jobQueryBuilder.withFields(['UID', 'Name'])
|
|
563
|
+
.withFilter('IsActive == true')
|
|
564
|
+
.withLimit(10);
|
|
565
|
+
|
|
566
|
+
const contactQueryBuilder = context.newQueryBuilder({
|
|
567
|
+
objectName: 'Contacts',
|
|
568
|
+
operationName: 'fetchContacts',
|
|
569
|
+
readOnly: true
|
|
570
|
+
});
|
|
571
|
+
contactQueryBuilder.withFields(['UID', 'FirstName', 'LastName'])
|
|
572
|
+
.withFilter('LastName != null')
|
|
573
|
+
.withLimit(5);
|
|
574
|
+
|
|
575
|
+
// Execute batch query
|
|
576
|
+
const queryResults = await context.graphqlService.queryBatch([jobQueryBuilder, contactQueryBuilder]);
|
|
577
|
+
|
|
578
|
+
// Process results
|
|
579
|
+
const jobResults = queryResults[0];
|
|
580
|
+
const contactResults = queryResults[1];
|
|
581
|
+
console.log('Jobs:', jobResults.records);
|
|
582
|
+
console.log('Job Total Count:', jobResults.totalCount);
|
|
583
|
+
console.log('Contacts:', contactResults.records);
|
|
584
|
+
console.log('Contact Total Count:', contactResults.totalCount);
|
|
585
|
+
```
|
|
526
586
|
#### Update data
|
|
527
587
|
```javascript
|
|
528
588
|
const recordsToUpdate = queryResult.records.map((record) => {
|
|
@@ -531,27 +591,76 @@ const recordsToUpdate = queryResult.records.map((record) => {
|
|
|
531
591
|
Name: record.Name + " - Updated"
|
|
532
592
|
}
|
|
533
593
|
});
|
|
534
|
-
const updateResult = await context.graphqlService.
|
|
594
|
+
const updateResult = await context.graphqlService.update({
|
|
535
595
|
objectName: 'Resources',
|
|
536
|
-
operation: GraphqlOperations.UPDATE,
|
|
537
596
|
operationName: 'updateResources',
|
|
538
597
|
records: recordsToUpdate,
|
|
539
|
-
bulkOperation: true // optional
|
|
598
|
+
bulkOperation: true, // optional
|
|
540
599
|
suppressChangeEvents: true // optional
|
|
541
600
|
});
|
|
542
601
|
```
|
|
543
602
|
|
|
603
|
+
#### upsert data
|
|
604
|
+
```javascript
|
|
605
|
+
const upsertResult = await context.graphqlService.upsert({
|
|
606
|
+
objectName: 'Resources',
|
|
607
|
+
operationName: 'updateResources',
|
|
608
|
+
records: recordsToUpsert,
|
|
609
|
+
bulkOperation: true, // optional
|
|
610
|
+
suppressChangeEvents: true, // optional
|
|
611
|
+
keyField: "ExternalId"
|
|
612
|
+
});
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
#### Batch mutations
|
|
616
|
+
```javascript
|
|
617
|
+
// Define mutation parameters for inserting Jobs and deleting Contacts
|
|
618
|
+
const insertJobParams = {
|
|
619
|
+
objectName: 'Jobs',
|
|
620
|
+
operationName: 'insertJobs',
|
|
621
|
+
operation: GraphqlOperations.INSERT,
|
|
622
|
+
records: [
|
|
623
|
+
{ UID: 'job1', Name: 'Developer Job', Status: 'Open' },
|
|
624
|
+
{ UID: 'job2', Name: 'Designer Job', Status: 'Open' }
|
|
625
|
+
],
|
|
626
|
+
bulkOperation: true
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
const deleteContactParams = {
|
|
630
|
+
objectName: 'Contacts',
|
|
631
|
+
operationName: 'deleteContacts',
|
|
632
|
+
operation: GraphqlOperations.DELETE,
|
|
633
|
+
records: [
|
|
634
|
+
{ UID: 'contact1' },
|
|
635
|
+
{ UID: 'contact2' }
|
|
636
|
+
]
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
// Execute batch mutation
|
|
640
|
+
const mutationResults = await context.graphqlService.mutateBatch([insertJobParams, deleteContactParams]);
|
|
641
|
+
|
|
642
|
+
// Process results
|
|
643
|
+
const insertResult = mutationResults[0];
|
|
644
|
+
const deleteResult = mutationResults[1];
|
|
645
|
+
console.log('Inserted Job UIDs:', context.graphqlService.extractUIDs(insertResult));
|
|
646
|
+
console.log('Deleted Contact UIDs:', context.graphqlService.extractUIDs(deleteResult));
|
|
647
|
+
```
|
|
648
|
+
|
|
544
649
|
The `bulkOperation` and `readOnly` options help optimize large-scale mutations and read-heavy queries. When enabled, they automatically add the `X-Skedulo-Bulk-Operation` (for mutations) and `X-Skedulo-Read-Only` (for queries) headers to improve performance.
|
|
545
650
|
To reduce the impact on change event processing, especially during high-volume mutations, the `suppressChangeEvents` option can be used. This disables change history tracking, helping to minimize delays.
|
|
546
651
|
|
|
547
|
-
#### Pagination
|
|
652
|
+
#### Pagination
|
|
653
|
+
|
|
654
|
+
The GraphQL Service supports pagination using either offset-based or cursor-based strategies, allowing you to fetch large datasets incrementally.
|
|
655
|
+
|
|
656
|
+
*Pagination with offset*
|
|
548
657
|
```javascript
|
|
549
658
|
while(queryResult.pageInfo.hasNextPage) {
|
|
550
659
|
queryBuilder.withOffset(queryResult.endOffset + 1);
|
|
551
660
|
queryResult = await queryBuilder.execute();
|
|
552
661
|
}
|
|
553
662
|
```
|
|
554
|
-
|
|
663
|
+
*Pagination with cursor*
|
|
555
664
|
```javascript
|
|
556
665
|
while(queryResult.pageInfo.hasNextPage) {
|
|
557
666
|
queryBuilder.withCursor(queryResult.endCursor);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __decorate=this&&this.__decorate||function(t,
|
|
1
|
+
"use strict";var __decorate=this&&this.__decorate||function(e,t,r,a){var n,o=arguments.length,i=o<3?t:null===a?a=Object.getOwnPropertyDescriptor(t,r):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,r,a);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(i=(o<3?n(i):o>3?n(t,r,i):n(t,r))||i);return o>3&&i&&Object.defineProperty(t,r,i),i},__metadata=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},__awaiter=this&&this.__awaiter||function(e,t,r,a){return new(r||(r=Promise))((function(n,o){function i(e){try{c(a.next(e))}catch(e){o(e)}}function s(e){try{c(a.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,s)}c((a=a.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLClient=void 0;const constants_1=require("../constants"),http_1=require("../constants/http"),monitor_call_1=require("../monitoring/decorators/monitor-call"),base_client_1=require("./base-client");class GraphQLClient extends base_client_1.BaseClient{execute(e){return __awaiter(this,arguments,void 0,(function*(e,t={}){return yield this._performRequest({method:http_1.HttpMethod.POST,endpoint:constants_1.TENANT_ENDPOINTS.GRAPHQL.SINGLE,body:"string"==typeof e?{query:e}:{query:e.query,variables:e.variables},headers:t})}))}executeBatch(e){return __awaiter(this,arguments,void 0,(function*(e,t={}){const r=e.map((e=>"string"==typeof e?{query:e}:{query:e.query,variables:e.variables})),a=yield this._performRequest({method:http_1.HttpMethod.POST,endpoint:constants_1.TENANT_ENDPOINTS.GRAPHQL.BATCH,body:r,headers:t});return a.forEach(((e,t)=>{if(!e.data&&e.errors)throw new Error(`GraphQL batch error at index ${t}: ${JSON.stringify(e.errors)}`)})),a}))}handleResponseData(e){if(Array.isArray(e));else if(!e.data&&e.errors)throw new Error(`GraphQL error: ${JSON.stringify(e.errors)}`)}}exports.GraphQLClient=GraphQLClient,__decorate([(0,monitor_call_1.MonitorCall)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object,Object]),__metadata("design:returntype",Promise)],GraphQLClient.prototype,"execute",null),__decorate([(0,monitor_call_1.MonitorCall)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Array,Object]),__metadata("design:returntype",Promise)],GraphQLClient.prototype,"executeBatch",null);
|
package/dist/index.d.ts
CHANGED
|
@@ -567,11 +567,12 @@ export interface GraphqlParams {
|
|
|
567
567
|
objectName: string;
|
|
568
568
|
operationName: string;
|
|
569
569
|
}
|
|
570
|
-
export interface
|
|
570
|
+
export interface GraphqlMutationParams extends GraphqlParams {
|
|
571
571
|
records: HasId[];
|
|
572
|
-
operation
|
|
572
|
+
operation?: GraphqlOperations;
|
|
573
573
|
bulkOperation?: boolean;
|
|
574
574
|
suppressChangeEvents?: boolean;
|
|
575
|
+
keyField?: string;
|
|
575
576
|
}
|
|
576
577
|
export interface GraphqlQueryParams extends GraphqlParams {
|
|
577
578
|
first?: number;
|
|
@@ -596,27 +597,31 @@ export interface MutationResult {
|
|
|
596
597
|
};
|
|
597
598
|
};
|
|
598
599
|
}
|
|
600
|
+
export interface GraphQLRequest {
|
|
601
|
+
query: string;
|
|
602
|
+
variables: object;
|
|
603
|
+
}
|
|
599
604
|
/**
|
|
600
605
|
* A client for executing GraphQL queries and mutations.
|
|
601
606
|
* Extends the BaseClient to interact with the GraphQL API.
|
|
602
607
|
*/
|
|
603
608
|
export declare class GraphQLClient extends BaseClient {
|
|
604
609
|
/**
|
|
605
|
-
* Executes a GraphQL query or mutation.
|
|
606
|
-
* @param {string} graphqlQuery - The GraphQL query or mutation string.
|
|
610
|
+
* Executes a single GraphQL query or mutation.
|
|
611
|
+
* @param {string | GraphQLRequest} graphqlQuery - The GraphQL query or mutation string.
|
|
607
612
|
* @param {Record<string, string>} headers - Additional headers to include in the request.
|
|
608
613
|
* @returns {Promise<GraphQlResponse>} - The response from the GraphQL API.
|
|
609
614
|
* @throws {Error} - If the request fails or if there are errors in the response.
|
|
610
615
|
*/
|
|
611
|
-
execute(graphqlQuery: string, headers?: Record<string, string>): Promise<GraphQlResponse>;
|
|
616
|
+
execute(graphqlQuery: string | GraphQLRequest, headers?: Record<string, string>): Promise<GraphQlResponse>;
|
|
612
617
|
/**
|
|
613
|
-
* Executes a GraphQL
|
|
614
|
-
* @param {string}
|
|
618
|
+
* Executes a batch of GraphQL queries or mutations.
|
|
619
|
+
* @param {Array<string | GraphQLRequest>} graphqlQueries - An array of GraphQL queries or mutations.
|
|
615
620
|
* @param {Record<string, string>} headers - Additional headers to include in the request.
|
|
616
|
-
* @returns {Promise<GraphQlResponse>} -
|
|
617
|
-
* @throws {Error} - If the request fails or if there are errors in the
|
|
621
|
+
* @returns {Promise<GraphQlResponse[]>} - An array of responses from the GraphQL API.
|
|
622
|
+
* @throws {Error} - If the request fails or if there are errors in any of the responses.
|
|
618
623
|
*/
|
|
619
|
-
executeBatch(
|
|
624
|
+
executeBatch(graphqlQueries: Array<string | GraphQLRequest>, headers?: Record<string, string>): Promise<GraphQlResponse[]>;
|
|
620
625
|
protected handleResponseData(responseData: any): void;
|
|
621
626
|
}
|
|
622
627
|
/**
|
|
@@ -1035,54 +1040,106 @@ export declare class GeoService {
|
|
|
1035
1040
|
createKey(origin: LatLng, destination: LatLng): string;
|
|
1036
1041
|
}
|
|
1037
1042
|
/**
|
|
1038
|
-
*
|
|
1043
|
+
* Service for executing GraphQL queries and mutations.
|
|
1039
1044
|
*/
|
|
1040
1045
|
export declare class GraphQLService {
|
|
1041
1046
|
private client;
|
|
1042
1047
|
/**
|
|
1043
|
-
*
|
|
1044
|
-
* @param {GraphQLClient} client - The GraphQL client
|
|
1048
|
+
* Constructor to initialize the GraphQL client.
|
|
1049
|
+
* @param {GraphQLClient} client - The GraphQL client instance.
|
|
1045
1050
|
*/
|
|
1046
1051
|
constructor(client: GraphQLClient);
|
|
1047
1052
|
/**
|
|
1048
|
-
* Executes a GraphQL query
|
|
1049
|
-
* @param {GraphqlQueryParams} params - The query
|
|
1050
|
-
* @returns {Promise<QueryResult>} - The query result
|
|
1053
|
+
* Executes a GraphQL query.
|
|
1054
|
+
* @param {GraphqlQueryParams} params - The parameters for the GraphQL query.
|
|
1055
|
+
* @returns {Promise<QueryResult>} - The query result containing records and pagination info.
|
|
1056
|
+
* @throws {Error} - If the query execution fails.
|
|
1051
1057
|
*/
|
|
1052
1058
|
query(params: GraphqlQueryParams): Promise<QueryResult>;
|
|
1053
1059
|
/**
|
|
1054
|
-
*
|
|
1055
|
-
* @param {
|
|
1056
|
-
* @
|
|
1057
|
-
|
|
1060
|
+
* Executes a batch of queries.
|
|
1061
|
+
* @param {GraphQLQueryBuilder[]} builders - Array of query builders.
|
|
1062
|
+
* @returns {Promise<QueryResult[]>} - Array of query results.
|
|
1063
|
+
*/
|
|
1064
|
+
queryBatch(builders: GraphQLQueryBuilder[]): Promise<QueryResult[]>;
|
|
1065
|
+
/**
|
|
1066
|
+
* Executes a batch of GraphQL queries.
|
|
1067
|
+
* @param {GraphqlQueryParams[]} paramsArray - An array of query parameters.
|
|
1068
|
+
* @returns {Promise<QueryResult[]>} - An array of query results.
|
|
1069
|
+
* @throws {Error} - If the batch execution fails.
|
|
1058
1070
|
*/
|
|
1059
|
-
|
|
1071
|
+
private executeBatchQueries;
|
|
1060
1072
|
/**
|
|
1061
|
-
*
|
|
1073
|
+
* Executes a GraphQL mutation.
|
|
1074
|
+
* @param {GraphqlMutationParams} params - The parameters for the GraphQL mutation.
|
|
1075
|
+
* @returns {Promise<MutationResult>} - The mutation result containing affected UIDs.
|
|
1076
|
+
* @throws {Error} - If the mutation execution fails or the operation is invalid.
|
|
1062
1077
|
*/
|
|
1063
|
-
|
|
1078
|
+
mutate(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
1064
1079
|
/**
|
|
1065
|
-
*
|
|
1080
|
+
* Executes a batch of GraphQL mutations.
|
|
1081
|
+
* @param {GraphqlMutationParams[]} paramsArray - An array of mutation parameters.
|
|
1082
|
+
* @returns {Promise<MutationResult[]>} - An array of mutation results.
|
|
1083
|
+
* @throws {Error} - If the batch execution fails or if any mutation has empty records.
|
|
1066
1084
|
*/
|
|
1067
|
-
|
|
1085
|
+
mutateBatch(paramsArray: GraphqlMutationParams[]): Promise<MutationResult[]>;
|
|
1068
1086
|
/**
|
|
1069
|
-
*
|
|
1087
|
+
* Deletes objects using a GraphQL mutation.
|
|
1088
|
+
* @param {GraphqlMutationParams} params - The parameters for the delete mutation.
|
|
1089
|
+
* @returns {Promise<MutationResult>} - The mutation result containing deleted UIDs.
|
|
1090
|
+
* @throws {Error} - If the delete operation fails.
|
|
1070
1091
|
*/
|
|
1071
|
-
|
|
1092
|
+
delete(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
1072
1093
|
/**
|
|
1073
|
-
*
|
|
1074
|
-
* @param {
|
|
1075
|
-
* @returns {
|
|
1094
|
+
* Updates objects using a GraphQL mutation.
|
|
1095
|
+
* @param {GraphqlMutationParams} params - The parameters for the update mutation.
|
|
1096
|
+
* @returns {Promise<MutationResult>} - The mutation result containing updated UIDs.
|
|
1097
|
+
* @throws {Error} - If the update operation fails.
|
|
1076
1098
|
*/
|
|
1077
|
-
|
|
1099
|
+
update(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
1078
1100
|
/**
|
|
1079
|
-
*
|
|
1080
|
-
* @param {
|
|
1081
|
-
* @returns {
|
|
1101
|
+
* Inserts objects using a GraphQL mutation.
|
|
1102
|
+
* @param {GraphqlMutationParams} params - The parameters for the insert mutation.
|
|
1103
|
+
* @returns {Promise<MutationResult>} - The mutation result containing inserted UIDs.
|
|
1104
|
+
* @throws {Error} - If the insert operation fails.
|
|
1105
|
+
*/
|
|
1106
|
+
insert(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
1107
|
+
/**
|
|
1108
|
+
* Upserts objects using a GraphQL mutation.
|
|
1109
|
+
* @param {GraphqlMutationParams} params - The parameters for the upsert mutation, including keyField.
|
|
1110
|
+
* @returns {Promise<MutationResult>} - The mutation result containing upserted UIDs.
|
|
1111
|
+
* @throws {Error} - If the upsert operation fails.
|
|
1112
|
+
*/
|
|
1113
|
+
upsert(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
1114
|
+
/**
|
|
1115
|
+
* Extracts UIDs from a mutation response.
|
|
1116
|
+
* @param {MutationResult} response - The mutation response containing the schema.
|
|
1117
|
+
* @returns {string[]} - An array of UIDs extracted from the response.
|
|
1118
|
+
*/
|
|
1119
|
+
extractUIDs(response: MutationResult): string[];
|
|
1120
|
+
/**
|
|
1121
|
+
* Converts object name to GraphQL query name format.
|
|
1122
|
+
* @param {string} objectName - The object name to convert.
|
|
1123
|
+
* @returns {string} - The formatted query name.
|
|
1082
1124
|
*/
|
|
1083
1125
|
private getQueryName;
|
|
1126
|
+
/**
|
|
1127
|
+
* Generates headers for GraphQL queries.
|
|
1128
|
+
* @param {GraphqlQueryParams} params - The query parameters containing operationName and readOnly flag.
|
|
1129
|
+
* @returns {Record<string, string>} - The headers for the query request.
|
|
1130
|
+
*/
|
|
1084
1131
|
private getQueryHeaders;
|
|
1132
|
+
/**
|
|
1133
|
+
* Generates headers for GraphQL mutations.
|
|
1134
|
+
* @param {GraphqlMutationParams} params - The mutation parameters containing operationName and optional flags.
|
|
1135
|
+
* @returns {Record<string, string>} - The headers for the mutation request.
|
|
1136
|
+
*/
|
|
1085
1137
|
private getMutationHeaders;
|
|
1138
|
+
/**
|
|
1139
|
+
* Generates operation header for GraphQL requests.
|
|
1140
|
+
* @param {string} operationName - The name of the GraphQL operation.
|
|
1141
|
+
* @returns {Record<string, string>} - The operation header.
|
|
1142
|
+
*/
|
|
1086
1143
|
private getOperationHeader;
|
|
1087
1144
|
}
|
|
1088
1145
|
/**
|
|
@@ -1104,7 +1161,7 @@ export declare class GraphQLQueryBuilder {
|
|
|
1104
1161
|
private queryParams;
|
|
1105
1162
|
/**
|
|
1106
1163
|
* Creates an instance of GraphQLQueryBuilder.
|
|
1107
|
-
* @param {
|
|
1164
|
+
* @param {GraphqlQueryParams} queryParams - The parameters for the query.
|
|
1108
1165
|
* @param {boolean} [isParent=false] - Indicates if this query is a parent query.
|
|
1109
1166
|
*/
|
|
1110
1167
|
constructor(queryParams: GraphqlQueryParams, isParent?: boolean);
|
|
@@ -1149,13 +1206,21 @@ export declare class GraphQLQueryBuilder {
|
|
|
1149
1206
|
*/
|
|
1150
1207
|
private formatQueryFields;
|
|
1151
1208
|
/**
|
|
1152
|
-
* Builds the GraphQL query parameters in a
|
|
1209
|
+
* Builds the GraphQL query parameters in a structured format.
|
|
1153
1210
|
*/
|
|
1154
1211
|
build(): GraphqlQueryParams;
|
|
1155
1212
|
/**
|
|
1156
|
-
* Executes the query using the GraphQL service
|
|
1213
|
+
* Executes the query using the GraphQL service.
|
|
1157
1214
|
*/
|
|
1158
1215
|
execute(): Promise<QueryResult>;
|
|
1216
|
+
/**
|
|
1217
|
+
* Executes all queries for the configured object, fetching records from the specified page range, up to a maximum of 20 pages.
|
|
1218
|
+
* @param {number} [fromPage=1] - The starting page number (1-based, defaults to 1).
|
|
1219
|
+
* @param {number} [toPage=20] - The ending page number (defaults to 20, capped at 20).
|
|
1220
|
+
* @returns {Promise<QueryResult>} - A query result containing all records in the page range and pagination info.
|
|
1221
|
+
* @throws {Error} - If the query execution fails, no GraphQL service is set, or invalid page parameters are provided.
|
|
1222
|
+
*/
|
|
1223
|
+
executeAll(fromPage?: number, toPage?: number): Promise<QueryResult>;
|
|
1159
1224
|
/**
|
|
1160
1225
|
* Generates a string representation of the GraphQL query for debugging.
|
|
1161
1226
|
*/
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { GraphQLService } from "./graphql-service";
|
|
2
|
+
import { GraphqlQueryParams, QueryResult, MutationResult } from "../../interfaces/graphql";
|
|
3
|
+
/**
|
|
4
|
+
* A utility class for building GraphQL queries or mutations dynamically.
|
|
5
|
+
*/
|
|
6
|
+
export declare class GraphQLQueryBuilder {
|
|
7
|
+
private objectName;
|
|
8
|
+
operationName: string;
|
|
9
|
+
private operationType;
|
|
10
|
+
private fields;
|
|
11
|
+
private filters;
|
|
12
|
+
private input;
|
|
13
|
+
private first?;
|
|
14
|
+
private offset?;
|
|
15
|
+
private orderBy?;
|
|
16
|
+
private after?;
|
|
17
|
+
private parentQueries;
|
|
18
|
+
private childQueries;
|
|
19
|
+
private isParent;
|
|
20
|
+
private graphqlService;
|
|
21
|
+
private queryParams;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of GraphQLQueryBuilder.
|
|
24
|
+
* @param queryParams - The parameters for the query or mutation.
|
|
25
|
+
* @param isParent - Indicates if this is a parent query.
|
|
26
|
+
*/
|
|
27
|
+
constructor(queryParams: GraphqlQueryParams, isParent?: boolean);
|
|
28
|
+
/**
|
|
29
|
+
* Sets the GraphQL service to use for executing the operation.
|
|
30
|
+
* @param graphqlService - The GraphQL service instance.
|
|
31
|
+
* @returns The query builder instance for chaining.
|
|
32
|
+
*/
|
|
33
|
+
setGraphqlService(graphqlService: GraphQLService): this;
|
|
34
|
+
/**
|
|
35
|
+
* Specifies the fields to be retrieved. Prevents duplicates.
|
|
36
|
+
* @param fields - Array of field names to include.
|
|
37
|
+
* @returns The query builder instance for chaining.
|
|
38
|
+
*/
|
|
39
|
+
withFields(fields: string[]): this;
|
|
40
|
+
/**
|
|
41
|
+
* Adds a filter condition to the query.
|
|
42
|
+
* @param condition - The filter condition string.
|
|
43
|
+
* @returns The query builder instance for chaining.
|
|
44
|
+
* @throws Error if applied to a mutation or parent query.
|
|
45
|
+
*/
|
|
46
|
+
withFilter(condition: string): this;
|
|
47
|
+
/**
|
|
48
|
+
* Sets the input object for a mutation.
|
|
49
|
+
* @param input - The mutation input object.
|
|
50
|
+
* @returns The query builder instance for chaining.
|
|
51
|
+
* @throws Error if applied to a query.
|
|
52
|
+
*/
|
|
53
|
+
withInput(input: Record<string, any>): this;
|
|
54
|
+
/**
|
|
55
|
+
* Sets a limit on the number of records to retrieve.
|
|
56
|
+
* @param first - The maximum number of records.
|
|
57
|
+
* @returns The query builder instance for chaining.
|
|
58
|
+
* @throws Error if applied to a mutation or parent query.
|
|
59
|
+
*/
|
|
60
|
+
withLimit(first: number): this;
|
|
61
|
+
/**
|
|
62
|
+
* Sets an offset for pagination.
|
|
63
|
+
* @param offset - The number of records to skip.
|
|
64
|
+
* @returns The query builder instance for chaining.
|
|
65
|
+
* @throws Error if applied to a mutation or parent query.
|
|
66
|
+
*/
|
|
67
|
+
withOffset(offset: number): this;
|
|
68
|
+
/**
|
|
69
|
+
* Sets the order in which records should be retrieved.
|
|
70
|
+
* @param orderBy - The field to order by.
|
|
71
|
+
* @returns The query builder instance for chaining.
|
|
72
|
+
* @throws Error if applied to a mutation or parent query.
|
|
73
|
+
*/
|
|
74
|
+
withOrderBy(orderBy: string): this;
|
|
75
|
+
/**
|
|
76
|
+
* Applies cursor-based pagination.
|
|
77
|
+
* @param after - The cursor to paginate after.
|
|
78
|
+
* @returns The query builder instance for chaining.
|
|
79
|
+
* @throws Error if applied to a mutation or parent query.
|
|
80
|
+
*/
|
|
81
|
+
withCursor(after: string): this;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a parent query.
|
|
84
|
+
* @param objectName - The name of the parent object.
|
|
85
|
+
* @returns The parent query builder instance.
|
|
86
|
+
*/
|
|
87
|
+
withParentQuery(objectName: string): GraphQLQueryBuilder;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a child query.
|
|
90
|
+
* @param objectName - The name of the child object.
|
|
91
|
+
* @returns The child query builder instance.
|
|
92
|
+
*/
|
|
93
|
+
withChildQuery(objectName: string): GraphQLQueryBuilder;
|
|
94
|
+
/**
|
|
95
|
+
* Recursively builds the GraphQL query fields, handling nested queries.
|
|
96
|
+
* @returns The formatted fields string.
|
|
97
|
+
*/
|
|
98
|
+
private formatQueryFields;
|
|
99
|
+
/**
|
|
100
|
+
* Builds the GraphQL operation parameters.
|
|
101
|
+
* @returns The structured query or mutation parameters.
|
|
102
|
+
*/
|
|
103
|
+
build(): GraphqlQueryParams;
|
|
104
|
+
/**
|
|
105
|
+
* Executes the query or mutation.
|
|
106
|
+
* @returns A promise resolving to the operation result.
|
|
107
|
+
* @throws Error if no GraphQL service is set.
|
|
108
|
+
*/
|
|
109
|
+
execute(): Promise<QueryResult | MutationResult>;
|
|
110
|
+
/**
|
|
111
|
+
* Generates a string representation of the GraphQL operation for debugging.
|
|
112
|
+
* @returns The formatted operation string.
|
|
113
|
+
*/
|
|
114
|
+
toString(): string;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* A utility class for building and executing a batch of GraphQL queries and mutations.
|
|
118
|
+
*/
|
|
119
|
+
export declare class GraphQLBatchBuilder {
|
|
120
|
+
private operations;
|
|
121
|
+
/**
|
|
122
|
+
* Adds a query or mutation to the batch.
|
|
123
|
+
* @param builder - The GraphQLQueryBuilder instance for the query or mutation.
|
|
124
|
+
* @returns The batch builder instance for chaining.
|
|
125
|
+
*/
|
|
126
|
+
add(builder: GraphQLQueryBuilder): this;
|
|
127
|
+
/**
|
|
128
|
+
* Executes the batch of queries and mutations.
|
|
129
|
+
* @returns A promise resolving to an array of query or mutation results.
|
|
130
|
+
* @throws Error if no operations are added or if GraphQL service is missing.
|
|
131
|
+
*/
|
|
132
|
+
execute(): Promise<(QueryResult | MutationResult)[]>;
|
|
133
|
+
/**
|
|
134
|
+
* Generates a string representation of the batch for debugging.
|
|
135
|
+
* @returns A string containing all operations in the batch.
|
|
136
|
+
*/
|
|
137
|
+
toString(): string;
|
|
138
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(t,e,r,i){return new(r||(r=Promise))((function(o,s){function n(t){try{h(i.next(t))}catch(t){s(t)}}function a(t){try{h(i.throw(t))}catch(t){s(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(n,a)}h((i=i.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLBatchBuilder=exports.GraphQLQueryBuilder=void 0;const graphql_1=require("../../interfaces/graphql");class GraphQLQueryBuilder{constructor(t,e=!1){if(this.fields=new Set(["UID"]),this.filters=[],this.input=null,this.parentQueries={},this.childQueries={},this.graphqlService=null,!t.objectName||!t.operationName)throw new Error("objectName and operationName are required.");this.queryParams=t,this.objectName=t.objectName.charAt(0).toLowerCase()+t.objectName.slice(1),this.operationName=t.operationName,this.operationType=t.operationType||"query",this.isParent=e}setGraphqlService(t){return this.graphqlService=t,this}withFields(t){return t.forEach((t=>this.fields.add(t))),this}withFilter(t){if("mutation"===this.operationType)throw new Error("Cannot apply filters to a mutation.");if(this.isParent)throw new Error(`Cannot apply filters to parent query for object: ${this.objectName}`);return this.filters.push(t),this}withInput(t){if("query"===this.operationType)throw new Error("Cannot apply input to a query.");if(!t||0===Object.keys(t).length)throw new Error("Mutation input cannot be empty.");return this.input=t,this}withLimit(t){if("mutation"===this.operationType)throw new Error("Cannot apply limit to a mutation.");if(this.isParent)throw new Error(`Cannot apply limit to parent query for object: ${this.objectName}`);return this.first=t,this}withOffset(t){if("mutation"===this.operationType)throw new Error("Cannot apply offset to a mutation.");if(this.isParent)throw new Error(`Cannot apply offset to parent query for object: ${this.objectName}`);return this.offset=t,this}withOrderBy(t){if("mutation"===this.operationType)throw new Error("Cannot apply orderBy to a mutation.");if(this.isParent)throw new Error(`Cannot apply orderBy to parent query for object: ${this.objectName}`);return this.orderBy=t,this}withCursor(t){if("mutation"===this.operationType)throw new Error("Cannot apply cursor to a mutation.");if(this.isParent)throw new Error(`Cannot apply cursor to parent query for object: ${this.objectName}`);return this.after=t,this}withParentQuery(t){const e=new GraphQLQueryBuilder({objectName:t,operationName:this.operationName,operationType:this.operationType},!0);return this.parentQueries[t]=e,e}withChildQuery(t){const e=new GraphQLQueryBuilder({objectName:t,operationName:this.operationName,operationType:this.operationType});return this.childQueries[t]=e,e}formatQueryFields(){const t=[...this.fields];for(const[e,r]of Object.entries(this.parentQueries))t.push(`${e} { ${r.formatQueryFields()} }`);for(const[e,r]of Object.entries(this.childQueries)){const i=r.build(),o=i.filter?`(filter: "${i.filter}")`:"";t.push(`${e} ${o} { ${i.fields||"UID"} }`)}return t.filter(Boolean).join(", ")}build(){const t={objectName:this.objectName,operationName:this.operationName,operationType:this.operationType,fields:this.formatQueryFields(),readOnly:this.queryParams.readOnly};return"query"===this.operationType?(t.filter=this.filters.length>0?this.filters.join(" AND "):void 0,t.first=this.first,t.offset=this.offset,t.orderBy=this.orderBy,t.after=this.after):t.input=this.input||void 0,t}execute(){var t;if(!this.graphqlService)throw new Error(`No GraphQL service set for ${this.operationType} execution on ${this.objectName}.`);const e=this.build();if("mutation"===this.operationType){const r={objectName:this.objectName,operationName:this.operationName,operation:(null===(t=this.input)||void 0===t?void 0:t.operation)||graphql_1.GraphqlOperations.UPDATE,records:this.input?[this.input]:[],fields:e.fields?e.fields.split(", "):["UID"]};return this.graphqlService.mutate(r)}return this.graphqlService.query(e)}toString(){const t=[];"query"===this.operationType?(this.first&&t.push(`first: ${this.first}`),this.offset&&t.push(`offset: ${this.offset}`),this.orderBy&&t.push(`orderBy: "${this.orderBy}"`),this.after&&t.push(`after: "${this.after}"`),this.filters.length&&t.push(`filter: "${this.filters.join(" AND ")}"`)):this.input&&t.push(`input: ${JSON.stringify(this.input)}`);const e=t.length?`(${t.join(", ")})`:"";return`${this.operationType} ${this.operationName} { ${this.objectName}${e} { ${this.formatQueryFields()} } }`}}exports.GraphQLQueryBuilder=GraphQLQueryBuilder;class GraphQLBatchBuilder{constructor(){this.operations=[]}add(t){return this.operations.push(t),this}execute(){return __awaiter(this,void 0,void 0,(function*(){if(!this.operations.length)return[];const t=this.operations[0].graphqlService;if(!t)throw new Error("No GraphQL service set for batch execution.");if(this.operations.some((e=>e.graphqlService!==t)))throw new Error("All operations must use the same GraphQL service.");const e=this.operations.map((t=>t.build()));return t.executeBatch(e)}))}toString(){return this.operations.map((t=>t.toString())).join("\n")}}exports.GraphQLBatchBuilder=GraphQLBatchBuilder;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { QueryResult } from "../../interfaces";
|
|
2
|
+
import { GraphQLQueryBuilder } from "./graphql-query-builder";
|
|
3
|
+
export declare class GraphQLQueryBatch {
|
|
4
|
+
/**
|
|
5
|
+
* Executes a batch of queries.
|
|
6
|
+
* @param {GraphQLQueryBuilder[]} builders - Array of query builders.
|
|
7
|
+
* @returns {Promise<QueryResult[]>} - Array of query results.
|
|
8
|
+
*/
|
|
9
|
+
execute(builders: GraphQLQueryBuilder[]): Promise<QueryResult[]>;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(t,e,r,n){return new(r||(r=Promise))((function(a,c){function i(t){try{u(n.next(t))}catch(t){c(t)}}function o(t){try{u(n.throw(t))}catch(t){c(t)}}function u(t){var e;t.done?a(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(i,o)}u((n=n.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLQueryBatch=void 0;class GraphQLQueryBatch{execute(t){return __awaiter(this,void 0,void 0,(function*(){if(!t.length)return[];const e=t[0].graphqlService;if(!e)throw new Error("No GraphQL service set for batch execution.");const r=t.map((t=>t.build()));return e.queryBatch(r)}))}}exports.GraphQLQueryBatch=GraphQLQueryBatch;
|
|
@@ -19,7 +19,7 @@ export declare class GraphQLQueryBuilder {
|
|
|
19
19
|
private queryParams;
|
|
20
20
|
/**
|
|
21
21
|
* Creates an instance of GraphQLQueryBuilder.
|
|
22
|
-
* @param {
|
|
22
|
+
* @param {GraphqlQueryParams} queryParams - The parameters for the query.
|
|
23
23
|
* @param {boolean} [isParent=false] - Indicates if this query is a parent query.
|
|
24
24
|
*/
|
|
25
25
|
constructor(queryParams: GraphqlQueryParams, isParent?: boolean);
|
|
@@ -64,13 +64,21 @@ export declare class GraphQLQueryBuilder {
|
|
|
64
64
|
*/
|
|
65
65
|
private formatQueryFields;
|
|
66
66
|
/**
|
|
67
|
-
* Builds the GraphQL query parameters in a
|
|
67
|
+
* Builds the GraphQL query parameters in a structured format.
|
|
68
68
|
*/
|
|
69
69
|
build(): GraphqlQueryParams;
|
|
70
70
|
/**
|
|
71
|
-
* Executes the query using the GraphQL service
|
|
71
|
+
* Executes the query using the GraphQL service.
|
|
72
72
|
*/
|
|
73
73
|
execute(): Promise<QueryResult>;
|
|
74
|
+
/**
|
|
75
|
+
* Executes all queries for the configured object, fetching records from the specified page range, up to a maximum of 20 pages.
|
|
76
|
+
* @param {number} [fromPage=1] - The starting page number (1-based, defaults to 1).
|
|
77
|
+
* @param {number} [toPage=20] - The ending page number (defaults to 20, capped at 20).
|
|
78
|
+
* @returns {Promise<QueryResult>} - A query result containing all records in the page range and pagination info.
|
|
79
|
+
* @throws {Error} - If the query execution fails, no GraphQL service is set, or invalid page parameters are provided.
|
|
80
|
+
*/
|
|
81
|
+
executeAll(fromPage?: number, toPage?: number): Promise<QueryResult>;
|
|
74
82
|
/**
|
|
75
83
|
* Generates a string representation of the GraphQL query for debugging.
|
|
76
84
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLQueryBuilder=void 0;class GraphQLQueryBuilder{constructor(e,
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(s,a){function o(e){try{h(i.next(e))}catch(e){a(e)}}function n(e){try{h(i.throw(e))}catch(e){a(e)}}function h(e){var t;e.done?s(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,n)}h((i=i.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLQueryBuilder=void 0;class GraphQLQueryBuilder{constructor(e,t=!1){this.fields=new Set(["UID"]),this.filters=[],this.parentQueries={},this.childQueries={},this.graphqlService=null,this.queryParams=e,this.objectName=e.objectName.charAt(0).toLowerCase()+e.objectName.slice(1),this.operationName=e.operationName,this.isParent=t}setGraphqlService(e){this.graphqlService=e}withFields(e){return e.forEach((e=>this.fields.add(e))),this}withFilter(e){if(this.isParent)throw new Error("Cannot apply filters to a parent query.");return this.filters.push(e),this}withLimit(e){if(this.isParent)throw new Error("Cannot apply limit to a parent query.");return this.first=e,this}withOffset(e){if(this.isParent)throw new Error("Cannot apply offset to a parent query.");return this.offset=e,this}withOrderBy(e){if(this.isParent)throw new Error("Cannot apply orderBy to a parent query.");return this.orderBy=e,this}withCursor(e){if(this.isParent)throw new Error("Cannot apply cursor to a parent query.");return this.after=e,this}withParentQuery(e){const t=new GraphQLQueryBuilder({objectName:e,operationName:this.operationName},!0);return this.parentQueries[e]=t,t}withChildQuery(e){const t=new GraphQLQueryBuilder({objectName:e,operationName:this.operationName});return this.childQueries[e]=t,t}formatQueryFields(){const e=Object.entries(this.parentQueries).map((([e,t])=>`${e} { ${t.formatQueryFields()} }`)).join(", "),t=Object.entries(this.childQueries).map((([e,t])=>{const r=t.build();return`${e} ${r.filter?`(filter: "${r.filter}")`:""} { ${r.fields||"UID"} }`})).join(", ");return[...this.fields,e,t].filter(Boolean).join(", ")}build(){return{objectName:this.objectName,operationName:this.operationName,fields:this.formatQueryFields(),filter:this.filters.length>0?this.filters.join(" AND "):void 0,first:this.first,offset:this.offset,orderBy:this.orderBy,after:this.after,readOnly:this.queryParams.readOnly}}execute(){if(!this.graphqlService)return Promise.reject(new Error("No GraphQL service set for query execution."));const e=this.build();return e.readOnly=this.queryParams.readOnly,this.graphqlService.query(e)}executeAll(){return __awaiter(this,arguments,void 0,(function*(e=1,t=20){if(!this.graphqlService)throw new Error("No GraphQL service set for query execution.");if(e<1)throw new Error("fromPage must be at least 1.");if(t<e)throw new Error("toPage must be greater than or equal to fromPage.");const r=Math.min(t,20),i=this.build(),s=yield this.graphqlService.query(i),a=s.totalCount;if(0===a)return{records:[],totalCount:0,pageInfo:{hasNextPage:!1},endCursor:"",endOffset:0};const o=this.first||200,n=Math.ceil(a/o),h=Math.min(r,n);if(0===Math.max(0,h-e+1))return{records:[],totalCount:a,pageInfo:{hasNextPage:h<n},endCursor:"",endOffset:(e-1)*o};const u=[];for(let t=e-1;t<h;t++){const e=new GraphQLQueryBuilder({objectName:this.objectName,operationName:this.operationName,readOnly:this.queryParams.readOnly});e.setGraphqlService(this.graphqlService),e.fields=new Set(this.fields),e.filters=[...this.filters],e.first=o,e.offset=t*o,e.orderBy=this.orderBy,e.after=this.after,u.push(e)}const l=yield this.graphqlService.queryBatch(u),f=l.flatMap((e=>e.records)),c=l[l.length-1];return{records:f,totalCount:s.totalCount,pageInfo:c.pageInfo,endCursor:c.endCursor,endOffset:c.endOffset}}))}toString(){return`{ ${this.objectName} { ${this.formatQueryFields()} } }`}}exports.GraphQLQueryBuilder=GraphQLQueryBuilder;
|
|
@@ -1,53 +1,106 @@
|
|
|
1
1
|
import { GraphQLClient } from "../../clients/graphql-client";
|
|
2
|
-
import {
|
|
2
|
+
import { GraphqlMutationParams, GraphqlQueryParams, MutationResult, QueryResult } from "../../interfaces/graphql";
|
|
3
|
+
import { GraphQLQueryBuilder } from "./graphql-query-builder";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* Service for executing GraphQL queries and mutations.
|
|
5
6
|
*/
|
|
6
7
|
export declare class GraphQLService {
|
|
7
8
|
private client;
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param {GraphQLClient} client - The GraphQL client
|
|
10
|
+
* Constructor to initialize the GraphQL client.
|
|
11
|
+
* @param {GraphQLClient} client - The GraphQL client instance.
|
|
11
12
|
*/
|
|
12
13
|
constructor(client: GraphQLClient);
|
|
13
14
|
/**
|
|
14
|
-
* Executes a GraphQL query
|
|
15
|
-
* @param {GraphqlQueryParams} params - The query
|
|
16
|
-
* @returns {Promise<QueryResult>} - The query result
|
|
15
|
+
* Executes a GraphQL query.
|
|
16
|
+
* @param {GraphqlQueryParams} params - The parameters for the GraphQL query.
|
|
17
|
+
* @returns {Promise<QueryResult>} - The query result containing records and pagination info.
|
|
18
|
+
* @throws {Error} - If the query execution fails.
|
|
17
19
|
*/
|
|
18
20
|
query(params: GraphqlQueryParams): Promise<QueryResult>;
|
|
19
21
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @param {
|
|
22
|
-
* @
|
|
23
|
-
* @returns {Promise<any>} - The response from the delete operation.
|
|
22
|
+
* Executes a batch of queries.
|
|
23
|
+
* @param {GraphQLQueryBuilder[]} builders - Array of query builders.
|
|
24
|
+
* @returns {Promise<QueryResult[]>} - Array of query results.
|
|
24
25
|
*/
|
|
25
|
-
|
|
26
|
+
queryBatch(builders: GraphQLQueryBuilder[]): Promise<QueryResult[]>;
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
+
* Executes a batch of GraphQL queries.
|
|
29
|
+
* @param {GraphqlQueryParams[]} paramsArray - An array of query parameters.
|
|
30
|
+
* @returns {Promise<QueryResult[]>} - An array of query results.
|
|
31
|
+
* @throws {Error} - If the batch execution fails.
|
|
28
32
|
*/
|
|
29
|
-
|
|
33
|
+
private executeBatchQueries;
|
|
30
34
|
/**
|
|
31
|
-
*
|
|
35
|
+
* Executes a GraphQL mutation.
|
|
36
|
+
* @param {GraphqlMutationParams} params - The parameters for the GraphQL mutation.
|
|
37
|
+
* @returns {Promise<MutationResult>} - The mutation result containing affected UIDs.
|
|
38
|
+
* @throws {Error} - If the mutation execution fails or the operation is invalid.
|
|
32
39
|
*/
|
|
33
|
-
|
|
40
|
+
mutate(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
34
41
|
/**
|
|
35
|
-
*
|
|
42
|
+
* Executes a batch of GraphQL mutations.
|
|
43
|
+
* @param {GraphqlMutationParams[]} paramsArray - An array of mutation parameters.
|
|
44
|
+
* @returns {Promise<MutationResult[]>} - An array of mutation results.
|
|
45
|
+
* @throws {Error} - If the batch execution fails or if any mutation has empty records.
|
|
36
46
|
*/
|
|
37
|
-
|
|
47
|
+
mutateBatch(paramsArray: GraphqlMutationParams[]): Promise<MutationResult[]>;
|
|
38
48
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @param {
|
|
41
|
-
* @returns {
|
|
49
|
+
* Deletes objects using a GraphQL mutation.
|
|
50
|
+
* @param {GraphqlMutationParams} params - The parameters for the delete mutation.
|
|
51
|
+
* @returns {Promise<MutationResult>} - The mutation result containing deleted UIDs.
|
|
52
|
+
* @throws {Error} - If the delete operation fails.
|
|
42
53
|
*/
|
|
43
|
-
|
|
54
|
+
delete(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
44
55
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @param {
|
|
47
|
-
* @returns {
|
|
56
|
+
* Updates objects using a GraphQL mutation.
|
|
57
|
+
* @param {GraphqlMutationParams} params - The parameters for the update mutation.
|
|
58
|
+
* @returns {Promise<MutationResult>} - The mutation result containing updated UIDs.
|
|
59
|
+
* @throws {Error} - If the update operation fails.
|
|
60
|
+
*/
|
|
61
|
+
update(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
62
|
+
/**
|
|
63
|
+
* Inserts objects using a GraphQL mutation.
|
|
64
|
+
* @param {GraphqlMutationParams} params - The parameters for the insert mutation.
|
|
65
|
+
* @returns {Promise<MutationResult>} - The mutation result containing inserted UIDs.
|
|
66
|
+
* @throws {Error} - If the insert operation fails.
|
|
67
|
+
*/
|
|
68
|
+
insert(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
69
|
+
/**
|
|
70
|
+
* Upserts objects using a GraphQL mutation.
|
|
71
|
+
* @param {GraphqlMutationParams} params - The parameters for the upsert mutation, including keyField.
|
|
72
|
+
* @returns {Promise<MutationResult>} - The mutation result containing upserted UIDs.
|
|
73
|
+
* @throws {Error} - If the upsert operation fails.
|
|
74
|
+
*/
|
|
75
|
+
upsert(params: GraphqlMutationParams): Promise<MutationResult>;
|
|
76
|
+
/**
|
|
77
|
+
* Extracts UIDs from a mutation response.
|
|
78
|
+
* @param {MutationResult} response - The mutation response containing the schema.
|
|
79
|
+
* @returns {string[]} - An array of UIDs extracted from the response.
|
|
80
|
+
*/
|
|
81
|
+
extractUIDs(response: MutationResult): string[];
|
|
82
|
+
/**
|
|
83
|
+
* Converts object name to GraphQL query name format.
|
|
84
|
+
* @param {string} objectName - The object name to convert.
|
|
85
|
+
* @returns {string} - The formatted query name.
|
|
48
86
|
*/
|
|
49
87
|
private getQueryName;
|
|
88
|
+
/**
|
|
89
|
+
* Generates headers for GraphQL queries.
|
|
90
|
+
* @param {GraphqlQueryParams} params - The query parameters containing operationName and readOnly flag.
|
|
91
|
+
* @returns {Record<string, string>} - The headers for the query request.
|
|
92
|
+
*/
|
|
50
93
|
private getQueryHeaders;
|
|
94
|
+
/**
|
|
95
|
+
* Generates headers for GraphQL mutations.
|
|
96
|
+
* @param {GraphqlMutationParams} params - The mutation parameters containing operationName and optional flags.
|
|
97
|
+
* @returns {Record<string, string>} - The headers for the mutation request.
|
|
98
|
+
*/
|
|
51
99
|
private getMutationHeaders;
|
|
100
|
+
/**
|
|
101
|
+
* Generates operation header for GraphQL requests.
|
|
102
|
+
* @param {string} operationName - The name of the GraphQL operation.
|
|
103
|
+
* @returns {Record<string, string>} - The operation header.
|
|
104
|
+
*/
|
|
52
105
|
private getOperationHeader;
|
|
53
106
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __decorate=this&&this.__decorate||function(e,t,a,r){var o,n=arguments.length,i=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,a):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,a,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(i=(n<3?o(i):n>3?o(t,a,i):o(t,a))||i);return n>3&&i&&Object.defineProperty(t,a,i),i},__metadata=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},__awaiter=this&&this.__awaiter||function(e,t,a,r){return new(a||(a=Promise))((function(o,n){function i(e){try{d(r.next(e))}catch(e){n(e)}}function s(e){try{d(r.throw(e))}catch(e){n(e)}}function d(e){var t;e.done?o(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(i,s)}d((r=r.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLService=void 0;const constants_1=require("../../constants"),request_header_constants_1=require("../../core/request-header-constants"),logging_1=require("../../logging"),queries_1=require("./queries");class GraphQLService{constructor(e){this.client=e}query(e){return __awaiter(this,void 0,void 0,(function*(){var t,a,r,o,n,i,s,d,u,_,
|
|
1
|
+
"use strict";var __decorate=this&&this.__decorate||function(e,t,a,r){var o,n=arguments.length,i=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,a):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,a,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(i=(n<3?o(i):n>3?o(t,a,i):o(t,a))||i);return n>3&&i&&Object.defineProperty(t,a,i),i},__metadata=this&&this.__metadata||function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},__awaiter=this&&this.__awaiter||function(e,t,a,r){return new(a||(a=Promise))((function(o,n){function i(e){try{d(r.next(e))}catch(e){n(e)}}function s(e){try{d(r.throw(e))}catch(e){n(e)}}function d(e){var t;e.done?o(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(i,s)}d((r=r.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.GraphQLService=void 0;const constants_1=require("../../constants"),request_header_constants_1=require("../../core/request-header-constants"),logging_1=require("../../logging"),queries_1=require("./queries");class GraphQLService{constructor(e){this.client=e}query(e){return __awaiter(this,void 0,void 0,(function*(){var t,a,r,o,n,i,s,d,u,c,_,l;e.objectName=this.getQueryName(e.objectName);const p=(0,queries_1.FETCH_RECORDS_QUERY)(e),h=this.getQueryHeaders(e),v=yield this.client.execute(p,h),g={records:(null===(a=null===(t=null==v?void 0:v.data)||void 0===t?void 0:t[e.objectName])||void 0===a?void 0:a.edges.map((e=>e.node)))||[],totalCount:(null===(o=null===(r=null==v?void 0:v.data)||void 0===r?void 0:r[e.objectName])||void 0===o?void 0:o.totalCount)||0,pageInfo:(null===(i=null===(n=null==v?void 0:v.data)||void 0===n?void 0:n[e.objectName])||void 0===i?void 0:i.pageInfo)||{}};if(g.records.length>0){const t=(null===(u=null===(d=null===(s=null==v?void 0:v.data)||void 0===s?void 0:s[e.objectName])||void 0===d?void 0:d.edges)||void 0===u?void 0:u.map((e=>e.offset)))||[],a=(null===(l=null===(_=null===(c=null==v?void 0:v.data)||void 0===c?void 0:c[e.objectName])||void 0===_?void 0:_.edges)||void 0===l?void 0:l.map((e=>e.cursor)))||[];g.endOffset=t[t.length-1],g.endCursor=a[a.length-1]}return g}))}queryBatch(e){return __awaiter(this,void 0,void 0,(function*(){if(!e.length)return[];const t=e.map((e=>e.build()));return this.executeBatchQueries(t)}))}executeBatchQueries(e){return __awaiter(this,void 0,void 0,(function*(){if(!e.length)return[];const t=e.map((e=>{const t=Object.assign(Object.assign({},e),{objectName:this.getQueryName(e.objectName)});return(0,queries_1.FETCH_RECORDS_QUERY)(t)})),a=this.getQueryHeaders(e[0]);return(yield this.client.executeBatch(t,a)).map(((t,a)=>{var r,o,n,i,s,d,u,c,_,l,p,h;const v=e[a],g={records:(null===(o=null===(r=null==t?void 0:t.data)||void 0===r?void 0:r[v.objectName])||void 0===o?void 0:o.edges.map((e=>e.node)))||[],totalCount:(null===(i=null===(n=null==t?void 0:t.data)||void 0===n?void 0:n[v.objectName])||void 0===i?void 0:i.totalCount)||0,pageInfo:(null===(d=null===(s=null==t?void 0:t.data)||void 0===s?void 0:s[v.objectName])||void 0===d?void 0:d.pageInfo)||{}};if(g.records.length>0){const e=(null===(_=null===(c=null===(u=null==t?void 0:t.data)||void 0===u?void 0:u[v.objectName])||void 0===c?void 0:c.edges)||void 0===_?void 0:_.map((e=>e.offset)))||[],a=(null===(h=null===(p=null===(l=null==t?void 0:t.data)||void 0===l?void 0:l[v.objectName])||void 0===p?void 0:p.edges)||void 0===h?void 0:h.map((e=>e.cursor)))||[];g.endOffset=e[e.length-1],g.endCursor=a[a.length-1]}return g}))}))}mutate(e){return __awaiter(this,void 0,void 0,(function*(){if(!e.records||0===e.records.length)return Promise.resolve({data:{schema:{}}});let t="";switch(e.operation){case constants_1.GraphqlOperations.DELETE:t=(0,queries_1.DELETE_OBJECTS_MUTATION)(e);break;case constants_1.GraphqlOperations.INSERT:case constants_1.GraphqlOperations.UPDATE:case constants_1.GraphqlOperations.UPSERT:t=(0,queries_1.MUTATE_OBJECTS_MUTATION)(e);break;default:throw new Error(`Invalid operation: ${e.operation}`)}const a=this.getMutationHeaders(e);return yield this.client.execute(t,a)}))}mutateBatch(e){return __awaiter(this,void 0,void 0,(function*(){if(!e.length)return[];e.forEach(((e,t)=>{if(!e.records||0===e.records.length)throw new Error(`Mutation at index ${t} has empty or null records for operation '${e.operationName}'`)}));const t=e.map((e=>{switch(e.operation){case constants_1.GraphqlOperations.DELETE:return(0,queries_1.DELETE_OBJECTS_MUTATION)(e);case constants_1.GraphqlOperations.INSERT:case constants_1.GraphqlOperations.UPDATE:case constants_1.GraphqlOperations.UPSERT:return(0,queries_1.MUTATE_OBJECTS_MUTATION)(e);default:throw new Error(`Invalid operation: ${e.operation}`)}})),a=this.getMutationHeaders(e[0]);return yield this.client.executeBatch(t,a)}))}delete(e){return __awaiter(this,void 0,void 0,(function*(){return e.operation=constants_1.GraphqlOperations.DELETE,this.mutate(e)}))}update(e){return __awaiter(this,void 0,void 0,(function*(){return e.operation=constants_1.GraphqlOperations.UPDATE,this.mutate(e)}))}insert(e){return __awaiter(this,void 0,void 0,(function*(){return e.operation=constants_1.GraphqlOperations.INSERT,this.mutate(e)}))}upsert(e){return __awaiter(this,void 0,void 0,(function*(){return e.operation=constants_1.GraphqlOperations.UPSERT,this.mutate(e)}))}extractUIDs(e){var t;return(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.schema)?Object.values(e.data.schema):[]}getQueryName(e){return e.charAt(0).toLowerCase()+e.slice(1)}getQueryHeaders(e){const t=this.getOperationHeader(e.operationName);return e.readOnly&&(t[request_header_constants_1.REQUEST_HEADERS.X_SKEDULO_READ_ONLY]="true"),t}getMutationHeaders(e){const t=this.getOperationHeader(e.operationName);return e.bulkOperation&&(t[request_header_constants_1.REQUEST_HEADERS.X_SKEDULO_BULK_OPERATION]="true"),e.suppressChangeEvents&&(t[request_header_constants_1.REQUEST_HEADERS.X_SKEDULO_SUPPRESS_CHANGE_EVENTS]="true"),t}getOperationHeader(e){return{[request_header_constants_1.REQUEST_HEADERS.X_GRAPHQL_OPERATION]:e}}}exports.GraphQLService=GraphQLService,__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"query",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Array]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"queryBatch",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"mutate",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Array]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"mutateBatch",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"delete",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"update",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"insert",null),__decorate([(0,logging_1.LogMethod)(),__metadata("design:type",Function),__metadata("design:paramtypes",[Object]),__metadata("design:returntype",Promise)],GraphQLService.prototype,"upsert",null);
|
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GraphqlMutationParams, GraphqlQueryParams } from "../../interfaces/graphql";
|
|
2
2
|
/**
|
|
3
|
-
* Constructs a GraphQL query
|
|
4
|
-
*
|
|
5
|
-
* @
|
|
6
|
-
* @returns The constructed GraphQL query string.
|
|
3
|
+
* Constructs a GraphQL query to fetch records for a given object.
|
|
4
|
+
* @param {GraphqlQueryParams} params - The parameters for the GraphQL query.
|
|
5
|
+
* @returns {string} - The constructed GraphQL query string.
|
|
7
6
|
*/
|
|
8
7
|
export declare const FETCH_RECORDS_QUERY: ({ objectName, operationName, filter, first, offset, after, orderBy, fields, }: GraphqlQueryParams) => string;
|
|
9
8
|
/**
|
|
10
|
-
* Constructs a GraphQL mutation
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
* @param operationName - The graphql operation name.
|
|
14
|
-
* @param records - The records to be deleted.
|
|
15
|
-
* @returns The constructed GraphQL mutation string.
|
|
9
|
+
* Constructs a GraphQL mutation to delete objects.
|
|
10
|
+
* @param {GraphqlMutationParams} params - The parameters for the GraphQL mutation.
|
|
11
|
+
* @returns {string} - The constructed GraphQL mutation string.
|
|
16
12
|
*/
|
|
17
|
-
export declare const DELETE_OBJECTS_MUTATION: ({ objectName, operationName, records }:
|
|
13
|
+
export declare const DELETE_OBJECTS_MUTATION: ({ objectName, operationName, records }: GraphqlMutationParams) => string;
|
|
18
14
|
/**
|
|
19
|
-
* Constructs a GraphQL mutation
|
|
20
|
-
*
|
|
21
|
-
* @
|
|
22
|
-
* @
|
|
23
|
-
* @param operation - The operation type ("insert" or "update").
|
|
24
|
-
* @returns The constructed GraphQL mutation string.
|
|
15
|
+
* Constructs a GraphQL mutation to insert, update, or upsert objects.
|
|
16
|
+
* @param {GraphqlMutationParams} params - The parameters for the GraphQL mutation.
|
|
17
|
+
* @returns {string} - The constructed GraphQL mutation string.
|
|
18
|
+
* @throws {Error} - If the operation type is invalid.
|
|
25
19
|
*/
|
|
26
|
-
export declare const
|
|
20
|
+
export declare const MUTATE_OBJECTS_MUTATION: ({ objectName, operationName, records, operation, keyField, }: GraphqlMutationParams) => string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MUTATE_OBJECTS_MUTATION=exports.DELETE_OBJECTS_MUTATION=exports.FETCH_RECORDS_QUERY=void 0;const FETCH_RECORDS_QUERY=({objectName:e,operationName:n,filter:t,first:o=200,offset:r=0,after:T,orderBy:E,fields:s})=>`\n query ${n} {\n ${e}(${[t?`filter: "${t}"`:"",T?`after: "${T}"`:"",`first: ${o}`,r?`offset: ${r}`:"",E?`orderBy: "${E}"`:""].filter((e=>e)).join(", ")}) {\n totalCount\n pageInfo{\n hasNextPage\n }\n edges {\n cursor\n offset\n node {\n ${s||"UID"}\n }\n }\n }\n }\n `;exports.FETCH_RECORDS_QUERY=FETCH_RECORDS_QUERY;const DELETE_OBJECTS_MUTATION=({objectName:e,operationName:n,records:t})=>`\n mutation ${n} {\n schema {\n ${t.map(((n,t)=>`alias${t+1}:delete${e}(UID: "${n.UID}")`)).join("\n ")}\n }\n }`;exports.DELETE_OBJECTS_MUTATION=DELETE_OBJECTS_MUTATION;const MUTATE_OBJECTS_MUTATION=({objectName:e,operationName:n,records:t,operation:o,keyField:r})=>{let T;return T="upsert"===o?t.map(((n,t)=>`\n alias${t+1}:upsert${e}(\n keyField: "${r||"UID"}",\n input: {\n ${Object.entries(n).map((([e,n])=>`${e}: ${"string"==typeof n?`"${n}"`:n}`)).join(", ")}\n }\n )\n `)).join("\n "):t.map(((n,t)=>`\n alias${t+1}:${o}${e}(\n input: {\n ${Object.entries(n).map((([e,n])=>`${e}: ${"string"==typeof n?`"${n}"`:n}`)).join(", ")}\n }\n )\n `)).join("\n "),`\n mutation ${n} {\n schema {\n ${T}\n }\n }\n `};exports.MUTATE_OBJECTS_MUTATION=MUTATE_OBJECTS_MUTATION;
|
package/package.json
CHANGED