@geins/core 0.3.4-canary → 0.3.5-canary

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.
@@ -2,5 +2,5 @@ yarn run v1.22.22
2
2
  $ rollup -c
3
3
  
4
4
  src/index.ts → dist/index.cjs, dist/index.esm.js...
5
- created dist/index.cjs, dist/index.esm.js in 6.1s
6
- Done in 6.66s.
5
+ created dist/index.cjs, dist/index.esm.js in 6.3s
6
+ Done in 6.89s.
package/CHANGELOG.md CHANGED
@@ -1,12 +1,20 @@
1
1
  # @geins/core
2
2
 
3
- ## 0.3.4-canary
3
+ ## 0.3.5-canary
4
4
 
5
5
  ### Patch Changes
6
6
 
7
7
  - d666708: added settingvars to GraphQLService
8
+ - 2158dcd: Possiblity to use string as query for the merchantAPI
8
9
  - 5f86eeb: ci fixes
9
10
 
11
+ ## 0.3.4
12
+
13
+ ### Patch Changes
14
+
15
+ - 3cf633e: added settingvars to GraphQLService
16
+ - 3cf633e: ci fixes
17
+
10
18
  ## 0.3.3
11
19
 
12
20
  ### Patch Changes
@@ -40,4 +40,30 @@ describe('GeinsCMS', () => {
40
40
  expect(result!.channels).toBeDefined();
41
41
  expect(result!.channels.length).toBeGreaterThan(0);
42
42
  });
43
+
44
+ it('should get result from custom string query', async () => {
45
+ const MY_QUERY = `
46
+ query getChannels {
47
+ channels {
48
+ name
49
+ url
50
+ }
51
+ }
52
+ `;
53
+
54
+ interface Channels {
55
+ channels: {
56
+ name: string;
57
+ url: string;
58
+ }[];
59
+ }
60
+
61
+ const result = await graphqlClient.query<Channels>({
62
+ queryAsString: MY_QUERY,
63
+ });
64
+
65
+ expect(result).toBeDefined();
66
+ expect(result!.channels).toBeDefined();
67
+ expect(result!.channels.length).toBeGreaterThan(0);
68
+ });
43
69
  });
@@ -1,4 +1,4 @@
1
- import { ApolloClient, NormalizedCacheObject, FetchPolicy, ApolloQueryResult, FetchResult, OperationVariables } from '@apollo/client/core';
1
+ import { ApolloClient, NormalizedCacheObject, DocumentNode, FetchPolicy, ApolloQueryResult, FetchResult, OperationVariables } from '@apollo/client/core';
2
2
  export declare enum FetchPolicyOptions {
3
3
  CACHE_FIRST = "cache-first",
4
4
  NETWORK_ONLY = "network-only",
@@ -23,7 +23,8 @@ export interface MerchantApiClientOptions {
23
23
  userToken?: string;
24
24
  }
25
25
  export interface GraphQLQueryOptions {
26
- query?: any;
26
+ query?: DocumentNode | string | undefined;
27
+ queryAsString?: string | undefined;
27
28
  variables?: any;
28
29
  requestOptions?: RequestOptions;
29
30
  }
package/dist/index.cjs CHANGED
@@ -16162,15 +16162,18 @@ var MerchantApiClient = /** @class */ (function () {
16162
16162
  }
16163
16163
  return exports.FetchPolicyOptions.NETWORK_ONLY;
16164
16164
  };
16165
- MerchantApiClient.prototype.getOperationObject = function (operationType, document, variables, options) {
16165
+ MerchantApiClient.prototype.getOperationObject = function (operationType, operationOptions) {
16166
16166
  var _a;
16167
16167
  var _b;
16168
- if (variables === void 0) { variables = {}; }
16169
- if (options === void 0) { options = {}; }
16168
+ var query = operationOptions.query, queryAsString = operationOptions.queryAsString, variables = operationOptions.variables, requestOptions = operationOptions.requestOptions;
16169
+ var queryDocument = query || gql(queryAsString || '');
16170
+ var options = requestOptions || {};
16170
16171
  var token = this._userToken || ((_b = this._cookieService) === null || _b === void 0 ? void 0 : _b.get(AUTH_COOKIES.USER_AUTH));
16171
- // remove typename from variables
16172
+ if (!queryDocument) {
16173
+ throw new Error('Query is required');
16174
+ }
16172
16175
  var operationObj = (_a = {},
16173
- _a[operationType] = document,
16176
+ _a[operationType] = queryDocument,
16174
16177
  _a.variables = variables,
16175
16178
  _a.fetchPolicy = this.getFetchPolicy(operationType, options.fetchPolicy),
16176
16179
  _a.pollInterval = options.pollInterval || this.pollInterval,
@@ -16186,20 +16189,18 @@ var MerchantApiClient = /** @class */ (function () {
16186
16189
  };
16187
16190
  MerchantApiClient.prototype.runQuery = function (options) {
16188
16191
  return __awaiter(this, void 0, void 0, function () {
16189
- var query, variables, requestOptions, q;
16192
+ var q;
16190
16193
  return __generator(this, function (_a) {
16191
- query = options.query, variables = options.variables, requestOptions = options.requestOptions;
16192
- q = this.getOperationObject(exports.OperationType.QUERY, query, variables, requestOptions);
16194
+ q = this.getOperationObject(exports.OperationType.QUERY, options);
16193
16195
  return [2 /*return*/, this._apolloClient.query(q)];
16194
16196
  });
16195
16197
  });
16196
16198
  };
16197
16199
  MerchantApiClient.prototype.runMutation = function (options) {
16198
16200
  return __awaiter(this, void 0, void 0, function () {
16199
- var query, variables, requestOptions, q;
16201
+ var q;
16200
16202
  return __generator(this, function (_a) {
16201
- query = options.query, variables = options.variables, requestOptions = options.requestOptions;
16202
- q = this.getOperationObject(exports.OperationType.MUTATION, query, variables, requestOptions);
16203
+ q = this.getOperationObject(exports.OperationType.MUTATION, options);
16203
16204
  return [2 /*return*/, this._apolloClient.mutate(q)];
16204
16205
  });
16205
16206
  });
@@ -21373,37 +21374,45 @@ var ChannelService = /** @class */ (function (_super) {
21373
21374
  return ChannelService;
21374
21375
  }(BaseApiService));
21375
21376
 
21377
+ /**
21378
+ * GraphQLService class provides methods to interact with a GraphQL API.
21379
+ * It extends the BaseApiService and includes methods for querying and mutating data.
21380
+ */
21376
21381
  var GraphQLService = /** @class */ (function (_super) {
21377
21382
  __extends(GraphQLService, _super);
21378
21383
  function GraphQLService(client, geinsSettings) {
21379
21384
  return _super.call(this, client, geinsSettings) || this;
21380
21385
  }
21381
21386
  GraphQLService.prototype.verifyQueryOptions = function (options) {
21382
- var _a, _b, _c, _d;
21383
- if (!options.query) {
21384
- throw new Error('Query is required');
21385
- }
21386
- if (!(((_d = (_c = (_b = (_a = options.query) === null || _a === void 0 ? void 0 : _a.definitions) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.variableDefinitions) === null || _d === void 0 ? void 0 : _d.length) > 0)) {
21387
- console.log('NO VARS');
21388
- return options;
21389
- }
21390
- var queryOptions = __assign({}, options);
21387
+ var _a, _b, _c, _d, _e;
21388
+ var returnOptions = __assign({}, options);
21389
+ if (!options.query && !options.queryAsString) {
21390
+ throw new Error('Query or queryAsString is required');
21391
+ }
21392
+ // check if query is a string and convert it to a gql object
21393
+ typeof returnOptions.query === 'string' && (returnOptions.query = gql(options.queryAsString || ''));
21394
+ var definition = (_b = (_a = returnOptions.query) === null || _a === void 0 ? void 0 : _a.definitions) === null || _b === void 0 ? void 0 : _b[0];
21395
+ if (!definition ||
21396
+ definition.kind !== 'OperationDefinition' ||
21397
+ !(((_d = (_c = definition.variableDefinitions) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0)) {
21398
+ return returnOptions;
21399
+ }
21400
+ var queryOptions = __assign({}, returnOptions);
21401
+ var queryDocument = queryOptions.query;
21391
21402
  queryOptions.variables = this.createVariables(queryOptions.variables);
21392
- var queryVars = queryOptions.query.definitions[0].variableDefinitions.map(function (v) { return v.variable.name.value; });
21403
+ var queryVars = ((_e = queryDocument.definitions[0].variableDefinitions) === null || _e === void 0 ? void 0 : _e.map(function (v) { return v.variable.name.value; })) || [];
21393
21404
  var providedVars = Object.keys(queryOptions.variables);
21394
21405
  providedVars.forEach(function (v) {
21395
- if (!queryVars.includes(v)) {
21406
+ if (queryVars && !queryVars.includes(v)) {
21396
21407
  delete queryOptions.variables[v];
21397
21408
  }
21398
21409
  });
21399
21410
  return queryOptions;
21400
21411
  };
21401
21412
  /**
21402
- * Generic method to run any GraphQL query
21403
- * @param query - The GraphQL query DocumentNode or gql query object
21404
- * @param variables - Variables to pass to the query
21405
- * @param options - Optional options like fetchPolicy, pollInterval, etc.
21406
- * @returns The result data of the query or null
21413
+ * Executes a GraphQL query with the provided options.
21414
+ * @param options - The options for the GraphQL query.
21415
+ * @returns A promise that resolves to the query result or null if an error occurs.
21407
21416
  */
21408
21417
  GraphQLService.prototype.query = function (options) {
21409
21418
  return __awaiter(this, void 0, void 0, function () {
@@ -21425,17 +21434,15 @@ var GraphQLService = /** @class */ (function (_super) {
21425
21434
  error_1 = _b.sent();
21426
21435
  console.error('Query error:', error_1);
21427
21436
  return [2 /*return*/, null];
21428
- case 4: return [2 /*return*/, null];
21437
+ case 4: return [2 /*return*/];
21429
21438
  }
21430
21439
  });
21431
21440
  });
21432
21441
  };
21433
21442
  /**
21434
- * Generic method to run any GraphQL mutation
21435
- * @param mutation - The GraphQL mutation DocumentNode or gql mutation object
21436
- * @param variables - Variables to pass to the mutation
21437
- * @param options - Optional options like fetchPolicy, etc.
21438
- * @returns The result data of the mutation or null
21443
+ * Executes a GraphQL mutation with the provided options.
21444
+ * @param options - The options for the GraphQL mutation.
21445
+ * @returns A promise that resolves to the mutation result or null if an error occurs.
21439
21446
  */
21440
21447
  GraphQLService.prototype.mutation = function (options) {
21441
21448
  return __awaiter(this, void 0, void 0, function () {
package/dist/index.esm.js CHANGED
@@ -16160,15 +16160,18 @@ var MerchantApiClient = /** @class */ (function () {
16160
16160
  }
16161
16161
  return FetchPolicyOptions.NETWORK_ONLY;
16162
16162
  };
16163
- MerchantApiClient.prototype.getOperationObject = function (operationType, document, variables, options) {
16163
+ MerchantApiClient.prototype.getOperationObject = function (operationType, operationOptions) {
16164
16164
  var _a;
16165
16165
  var _b;
16166
- if (variables === void 0) { variables = {}; }
16167
- if (options === void 0) { options = {}; }
16166
+ var query = operationOptions.query, queryAsString = operationOptions.queryAsString, variables = operationOptions.variables, requestOptions = operationOptions.requestOptions;
16167
+ var queryDocument = query || gql(queryAsString || '');
16168
+ var options = requestOptions || {};
16168
16169
  var token = this._userToken || ((_b = this._cookieService) === null || _b === void 0 ? void 0 : _b.get(AUTH_COOKIES.USER_AUTH));
16169
- // remove typename from variables
16170
+ if (!queryDocument) {
16171
+ throw new Error('Query is required');
16172
+ }
16170
16173
  var operationObj = (_a = {},
16171
- _a[operationType] = document,
16174
+ _a[operationType] = queryDocument,
16172
16175
  _a.variables = variables,
16173
16176
  _a.fetchPolicy = this.getFetchPolicy(operationType, options.fetchPolicy),
16174
16177
  _a.pollInterval = options.pollInterval || this.pollInterval,
@@ -16184,20 +16187,18 @@ var MerchantApiClient = /** @class */ (function () {
16184
16187
  };
16185
16188
  MerchantApiClient.prototype.runQuery = function (options) {
16186
16189
  return __awaiter(this, void 0, void 0, function () {
16187
- var query, variables, requestOptions, q;
16190
+ var q;
16188
16191
  return __generator(this, function (_a) {
16189
- query = options.query, variables = options.variables, requestOptions = options.requestOptions;
16190
- q = this.getOperationObject(OperationType.QUERY, query, variables, requestOptions);
16192
+ q = this.getOperationObject(OperationType.QUERY, options);
16191
16193
  return [2 /*return*/, this._apolloClient.query(q)];
16192
16194
  });
16193
16195
  });
16194
16196
  };
16195
16197
  MerchantApiClient.prototype.runMutation = function (options) {
16196
16198
  return __awaiter(this, void 0, void 0, function () {
16197
- var query, variables, requestOptions, q;
16199
+ var q;
16198
16200
  return __generator(this, function (_a) {
16199
- query = options.query, variables = options.variables, requestOptions = options.requestOptions;
16200
- q = this.getOperationObject(OperationType.MUTATION, query, variables, requestOptions);
16201
+ q = this.getOperationObject(OperationType.MUTATION, options);
16201
16202
  return [2 /*return*/, this._apolloClient.mutate(q)];
16202
16203
  });
16203
16204
  });
@@ -21371,37 +21372,45 @@ var ChannelService = /** @class */ (function (_super) {
21371
21372
  return ChannelService;
21372
21373
  }(BaseApiService));
21373
21374
 
21375
+ /**
21376
+ * GraphQLService class provides methods to interact with a GraphQL API.
21377
+ * It extends the BaseApiService and includes methods for querying and mutating data.
21378
+ */
21374
21379
  var GraphQLService = /** @class */ (function (_super) {
21375
21380
  __extends(GraphQLService, _super);
21376
21381
  function GraphQLService(client, geinsSettings) {
21377
21382
  return _super.call(this, client, geinsSettings) || this;
21378
21383
  }
21379
21384
  GraphQLService.prototype.verifyQueryOptions = function (options) {
21380
- var _a, _b, _c, _d;
21381
- if (!options.query) {
21382
- throw new Error('Query is required');
21383
- }
21384
- if (!(((_d = (_c = (_b = (_a = options.query) === null || _a === void 0 ? void 0 : _a.definitions) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.variableDefinitions) === null || _d === void 0 ? void 0 : _d.length) > 0)) {
21385
- console.log('NO VARS');
21386
- return options;
21387
- }
21388
- var queryOptions = __assign({}, options);
21385
+ var _a, _b, _c, _d, _e;
21386
+ var returnOptions = __assign({}, options);
21387
+ if (!options.query && !options.queryAsString) {
21388
+ throw new Error('Query or queryAsString is required');
21389
+ }
21390
+ // check if query is a string and convert it to a gql object
21391
+ typeof returnOptions.query === 'string' && (returnOptions.query = gql(options.queryAsString || ''));
21392
+ var definition = (_b = (_a = returnOptions.query) === null || _a === void 0 ? void 0 : _a.definitions) === null || _b === void 0 ? void 0 : _b[0];
21393
+ if (!definition ||
21394
+ definition.kind !== 'OperationDefinition' ||
21395
+ !(((_d = (_c = definition.variableDefinitions) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0)) {
21396
+ return returnOptions;
21397
+ }
21398
+ var queryOptions = __assign({}, returnOptions);
21399
+ var queryDocument = queryOptions.query;
21389
21400
  queryOptions.variables = this.createVariables(queryOptions.variables);
21390
- var queryVars = queryOptions.query.definitions[0].variableDefinitions.map(function (v) { return v.variable.name.value; });
21401
+ var queryVars = ((_e = queryDocument.definitions[0].variableDefinitions) === null || _e === void 0 ? void 0 : _e.map(function (v) { return v.variable.name.value; })) || [];
21391
21402
  var providedVars = Object.keys(queryOptions.variables);
21392
21403
  providedVars.forEach(function (v) {
21393
- if (!queryVars.includes(v)) {
21404
+ if (queryVars && !queryVars.includes(v)) {
21394
21405
  delete queryOptions.variables[v];
21395
21406
  }
21396
21407
  });
21397
21408
  return queryOptions;
21398
21409
  };
21399
21410
  /**
21400
- * Generic method to run any GraphQL query
21401
- * @param query - The GraphQL query DocumentNode or gql query object
21402
- * @param variables - Variables to pass to the query
21403
- * @param options - Optional options like fetchPolicy, pollInterval, etc.
21404
- * @returns The result data of the query or null
21411
+ * Executes a GraphQL query with the provided options.
21412
+ * @param options - The options for the GraphQL query.
21413
+ * @returns A promise that resolves to the query result or null if an error occurs.
21405
21414
  */
21406
21415
  GraphQLService.prototype.query = function (options) {
21407
21416
  return __awaiter(this, void 0, void 0, function () {
@@ -21423,17 +21432,15 @@ var GraphQLService = /** @class */ (function (_super) {
21423
21432
  error_1 = _b.sent();
21424
21433
  console.error('Query error:', error_1);
21425
21434
  return [2 /*return*/, null];
21426
- case 4: return [2 /*return*/, null];
21435
+ case 4: return [2 /*return*/];
21427
21436
  }
21428
21437
  });
21429
21438
  });
21430
21439
  };
21431
21440
  /**
21432
- * Generic method to run any GraphQL mutation
21433
- * @param mutation - The GraphQL mutation DocumentNode or gql mutation object
21434
- * @param variables - Variables to pass to the mutation
21435
- * @param options - Optional options like fetchPolicy, etc.
21436
- * @returns The result data of the mutation or null
21441
+ * Executes a GraphQL mutation with the provided options.
21442
+ * @param options - The options for the GraphQL mutation.
21443
+ * @returns A promise that resolves to the mutation result or null if an error occurs.
21437
21444
  */
21438
21445
  GraphQLService.prototype.mutation = function (options) {
21439
21446
  return __awaiter(this, void 0, void 0, function () {
@@ -1,23 +1,23 @@
1
1
  import type { GeinsSettings } from '@geins/types';
2
2
  import { BaseApiService } from '../base/baseApiService';
3
3
  import { GraphQLQueryOptions } from '../api-client/merchantApiClient';
4
+ /**
5
+ * GraphQLService class provides methods to interact with a GraphQL API.
6
+ * It extends the BaseApiService and includes methods for querying and mutating data.
7
+ */
4
8
  export declare class GraphQLService extends BaseApiService {
5
9
  constructor(client: any, geinsSettings: GeinsSettings);
6
10
  protected verifyQueryOptions(options: GraphQLQueryOptions): GraphQLQueryOptions;
7
11
  /**
8
- * Generic method to run any GraphQL query
9
- * @param query - The GraphQL query DocumentNode or gql query object
10
- * @param variables - Variables to pass to the query
11
- * @param options - Optional options like fetchPolicy, pollInterval, etc.
12
- * @returns The result data of the query or null
12
+ * Executes a GraphQL query with the provided options.
13
+ * @param options - The options for the GraphQL query.
14
+ * @returns A promise that resolves to the query result or null if an error occurs.
13
15
  */
14
16
  query<T = any>(options: GraphQLQueryOptions): Promise<T | null>;
15
17
  /**
16
- * Generic method to run any GraphQL mutation
17
- * @param mutation - The GraphQL mutation DocumentNode or gql mutation object
18
- * @param variables - Variables to pass to the mutation
19
- * @param options - Optional options like fetchPolicy, etc.
20
- * @returns The result data of the mutation or null
18
+ * Executes a GraphQL mutation with the provided options.
19
+ * @param options - The options for the GraphQL mutation.
20
+ * @returns A promise that resolves to the mutation result or null if an error occurs.
21
21
  */
22
22
  mutation<T = any>(options: GraphQLQueryOptions): Promise<T | null>;
23
23
  protected parseResult(result: any): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geins/core",
3
- "version": "0.3.4-canary",
3
+ "version": "0.3.5-canary",
4
4
  "main": "dist/index.cjs",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "broadcast-channel": "^7.0.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@geins/types": "0.3.4-canary",
24
- "@geins/eslint-config": "0.1.5-canary",
23
+ "@geins/types": "0.3.5-canary",
24
+ "@geins/eslint-config": "0.1.6-canary",
25
25
  "@rollup/plugin-commonjs": "^28.0.0",
26
26
  "@rollup/plugin-graphql": "^2.0.5",
27
27
  "@rollup/plugin-node-resolve": "^15.3.0",
@@ -10,6 +10,7 @@ import {
10
10
  FetchResult,
11
11
  OperationVariables,
12
12
  } from '@apollo/client/core';
13
+ import { gql } from '@apollo/client/core';
13
14
 
14
15
  export enum FetchPolicyOptions {
15
16
  CACHE_FIRST = 'cache-first',
@@ -39,7 +40,8 @@ export interface MerchantApiClientOptions {
39
40
  }
40
41
 
41
42
  export interface GraphQLQueryOptions {
42
- query?: any;
43
+ query?: DocumentNode | string | undefined;
44
+ queryAsString?: string | undefined;
43
45
  variables?: any;
44
46
  requestOptions?: RequestOptions;
45
47
  }
@@ -108,17 +110,18 @@ export class MerchantApiClient {
108
110
  return FetchPolicyOptions.NETWORK_ONLY;
109
111
  }
110
112
 
111
- private getOperationObject(
112
- operationType: OperationType,
113
- document: DocumentNode,
114
- variables: OperationVariables = {},
115
- options: RequestOptions = {},
116
- ) {
113
+ private getOperationObject(operationType: OperationType, operationOptions: GraphQLQueryOptions) {
114
+ const { query, queryAsString, variables, requestOptions } = operationOptions;
115
+ const queryDocument = query || gql(queryAsString || '');
116
+ const options = requestOptions || {};
117
117
  const token = this._userToken || this._cookieService?.get(AUTH_COOKIES.USER_AUTH);
118
- // remove typename from variables
118
+
119
+ if (!queryDocument) {
120
+ throw new Error('Query is required');
121
+ }
119
122
 
120
123
  const operationObj: any = {
121
- [operationType]: document,
124
+ [operationType]: queryDocument,
122
125
  variables,
123
126
  fetchPolicy: this.getFetchPolicy(operationType, options.fetchPolicy),
124
127
  pollInterval: options.pollInterval || this.pollInterval,
@@ -138,16 +141,14 @@ export class MerchantApiClient {
138
141
  async runQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(
139
142
  options: GraphQLQueryOptions,
140
143
  ): Promise<ApolloQueryResult<TData>> {
141
- const { query, variables, requestOptions } = options;
142
- const q = this.getOperationObject(OperationType.QUERY, query, variables, requestOptions);
144
+ const q = this.getOperationObject(OperationType.QUERY, options);
143
145
  return this._apolloClient.query<TData, TVariables>(q);
144
146
  }
145
147
 
146
148
  async runMutation<TData = any, TVariables extends OperationVariables = OperationVariables>(
147
149
  options: GraphQLQueryOptions,
148
150
  ): Promise<FetchResult<TData>> {
149
- const { query, variables, requestOptions } = options;
150
- const q = this.getOperationObject(OperationType.MUTATION, query, variables, requestOptions);
151
+ const q = this.getOperationObject(OperationType.MUTATION, options);
151
152
  return this._apolloClient.mutate<TData, TVariables>(q);
152
153
  }
153
154
  }
@@ -1,48 +1,59 @@
1
1
  import type { GeinsSettings } from '@geins/types';
2
2
  import { BaseApiService } from '../base/baseApiService';
3
3
  import { GraphQLQueryOptions } from '../api-client/merchantApiClient';
4
+ import { DocumentNode, OperationDefinitionNode } from 'graphql';
5
+ import { gql } from '@apollo/client/core';
4
6
 
7
+ /**
8
+ * GraphQLService class provides methods to interact with a GraphQL API.
9
+ * It extends the BaseApiService and includes methods for querying and mutating data.
10
+ */
5
11
  export class GraphQLService extends BaseApiService {
6
12
  constructor(client: any, geinsSettings: GeinsSettings) {
7
13
  super(client, geinsSettings);
8
14
  }
9
15
  protected verifyQueryOptions(options: GraphQLQueryOptions): GraphQLQueryOptions {
10
- if (!options.query) {
11
- throw new Error('Query is required');
16
+ const returnOptions = { ...options };
17
+ if (!options.query && !options.queryAsString) {
18
+ throw new Error('Query or queryAsString is required');
12
19
  }
13
- if (!(options.query?.definitions?.[0]?.variableDefinitions?.length > 0)) {
14
- console.log('NO VARS');
15
- return options;
20
+ // check if query is a string and convert it to a gql object
21
+ typeof returnOptions.query === 'string' && (returnOptions.query = gql(options.queryAsString || ''));
22
+
23
+ const definition = returnOptions.query?.definitions?.[0] as OperationDefinitionNode;
24
+ if (
25
+ !definition ||
26
+ definition.kind !== 'OperationDefinition' ||
27
+ !((definition.variableDefinitions?.length ?? 0) > 0)
28
+ ) {
29
+ return returnOptions;
16
30
  }
17
31
 
18
- const queryOptions = { ...options };
32
+ const queryOptions = { ...returnOptions };
33
+ const queryDocument = queryOptions.query as DocumentNode;
19
34
  queryOptions.variables = this.createVariables(queryOptions.variables);
20
-
21
- const queryVars = queryOptions.query.definitions[0].variableDefinitions.map(
22
- (v: any) => v.variable.name.value,
23
- );
35
+ const queryVars =
36
+ (queryDocument.definitions[0] as OperationDefinitionNode).variableDefinitions?.map(
37
+ (v: any) => v.variable.name.value,
38
+ ) || [];
24
39
 
25
40
  const providedVars = Object.keys(queryOptions.variables);
26
41
  providedVars.forEach(v => {
27
- if (!queryVars.includes(v)) {
42
+ if (queryVars && !queryVars.includes(v)) {
28
43
  delete queryOptions.variables[v];
29
44
  }
30
45
  });
31
46
 
32
47
  return queryOptions;
33
48
  }
34
-
35
49
  /**
36
- * Generic method to run any GraphQL query
37
- * @param query - The GraphQL query DocumentNode or gql query object
38
- * @param variables - Variables to pass to the query
39
- * @param options - Optional options like fetchPolicy, pollInterval, etc.
40
- * @returns The result data of the query or null
50
+ * Executes a GraphQL query with the provided options.
51
+ * @param options - The options for the GraphQL query.
52
+ * @returns A promise that resolves to the query result or null if an error occurs.
41
53
  */
42
54
  async query<T = any>(options: GraphQLQueryOptions): Promise<T | null> {
43
55
  try {
44
56
  const queryOptions = await this.verifyQueryOptions(options);
45
-
46
57
  const result = this.cleanObject(await this.runQuery(queryOptions));
47
58
  const parsedResult = this.parseResult(result);
48
59
  return parsedResult as T;
@@ -50,15 +61,12 @@ export class GraphQLService extends BaseApiService {
50
61
  console.error('Query error:', error);
51
62
  return null;
52
63
  }
53
- return null;
54
64
  }
55
65
 
56
66
  /**
57
- * Generic method to run any GraphQL mutation
58
- * @param mutation - The GraphQL mutation DocumentNode or gql mutation object
59
- * @param variables - Variables to pass to the mutation
60
- * @param options - Optional options like fetchPolicy, etc.
61
- * @returns The result data of the mutation or null
67
+ * Executes a GraphQL mutation with the provided options.
68
+ * @param options - The options for the GraphQL mutation.
69
+ * @returns A promise that resolves to the mutation result or null if an error occurs.
62
70
  */
63
71
  async mutation<T = any>(options: GraphQLQueryOptions): Promise<T | null> {
64
72
  try {