@graphql-mesh/apollo-link 2.0.0-alpha-e65eb8d69.0 → 2.0.0-alpha-a952bb598.0

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.
Files changed (4) hide show
  1. package/index.d.ts +7 -5
  2. package/index.js +20 -27
  3. package/index.mjs +20 -27
  4. package/package.json +9 -3
package/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { ApolloLink, FetchResult, Observable, Operation } from '@apollo/client/core';
2
- import { MeshInstance } from '@graphql-mesh/runtime';
1
+ import { ApolloLink } from '@apollo/client/core';
2
+ import { ExecuteMeshFn, SubscribeMeshFn } from '@graphql-mesh/runtime';
3
+ export interface MeshApolloRequestHandlerOptions {
4
+ execute: ExecuteMeshFn;
5
+ subscribe?: SubscribeMeshFn;
6
+ }
3
7
  export declare class MeshApolloLink extends ApolloLink {
4
- mesh$: Promise<MeshInstance>;
5
- constructor(getBuiltMesh: () => Promise<MeshInstance>);
6
- request(operation: Operation): Observable<FetchResult>;
8
+ constructor(options: MeshApolloRequestHandlerOptions);
7
9
  }
package/index.js CHANGED
@@ -3,46 +3,34 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const core = require('@apollo/client/core');
6
+ const utils = require('@graphql-tools/utils');
6
7
  const graphql = require('graphql');
7
8
 
8
- class MeshApolloLink extends core.ApolloLink {
9
- constructor(getBuiltMesh) {
10
- super();
11
- this.mesh$ = getBuiltMesh();
12
- }
13
- request(operation) {
9
+ const ROOT_VALUE = {};
10
+ function createMeshApolloRequestHandler(options) {
11
+ return function meshApolloRequestHandler(operation) {
14
12
  const operationAst = graphql.getOperationAST(operation.query, operation.operationName);
15
13
  if (!operationAst) {
16
14
  throw new Error('GraphQL operation not found');
17
15
  }
16
+ const operationFn = operationAst.operation === 'subscription' ? options.subscribe : options.execute;
18
17
  return new core.Observable(observer => {
19
18
  Promise.resolve()
20
19
  .then(async () => {
21
- const mesh = await this.mesh$;
22
- try {
23
- if (operationAst.operation === 'subscription') {
24
- const subscriptionResult = await mesh.subscribe(operation.query, operation.variables, operation.getContext(), {}, operation.operationName);
25
- if (Symbol.asyncIterator in subscriptionResult) {
26
- for await (const result of subscriptionResult) {
27
- if (observer.closed) {
28
- return;
29
- }
30
- observer.next(result);
31
- }
32
- observer.complete();
33
- }
34
- }
35
- else {
36
- const result = await mesh.execute(operation.query, operation.variables, operation.getContext(), {}, operation.operationName);
37
- if (!observer.closed) {
38
- observer.next(result);
39
- observer.complete();
20
+ const results = await operationFn(operation.query, operation.variables, operation.getContext(), ROOT_VALUE, operation.operationName);
21
+ if (utils.isAsyncIterable(results)) {
22
+ for await (const result of results) {
23
+ if (observer.closed) {
24
+ return;
40
25
  }
26
+ observer.next(result);
41
27
  }
28
+ observer.complete();
42
29
  }
43
- catch (error) {
30
+ else {
44
31
  if (!observer.closed) {
45
- observer.error(error);
32
+ observer.next(results);
33
+ observer.complete();
46
34
  }
47
35
  }
48
36
  })
@@ -52,6 +40,11 @@ class MeshApolloLink extends core.ApolloLink {
52
40
  }
53
41
  });
54
42
  });
43
+ };
44
+ }
45
+ class MeshApolloLink extends core.ApolloLink {
46
+ constructor(options) {
47
+ super(createMeshApolloRequestHandler(options));
55
48
  }
56
49
  }
57
50
 
package/index.mjs CHANGED
@@ -1,44 +1,32 @@
1
1
  import { ApolloLink, Observable } from '@apollo/client/core';
2
+ import { isAsyncIterable } from '@graphql-tools/utils';
2
3
  import { getOperationAST } from 'graphql';
3
4
 
4
- class MeshApolloLink extends ApolloLink {
5
- constructor(getBuiltMesh) {
6
- super();
7
- this.mesh$ = getBuiltMesh();
8
- }
9
- request(operation) {
5
+ const ROOT_VALUE = {};
6
+ function createMeshApolloRequestHandler(options) {
7
+ return function meshApolloRequestHandler(operation) {
10
8
  const operationAst = getOperationAST(operation.query, operation.operationName);
11
9
  if (!operationAst) {
12
10
  throw new Error('GraphQL operation not found');
13
11
  }
12
+ const operationFn = operationAst.operation === 'subscription' ? options.subscribe : options.execute;
14
13
  return new Observable(observer => {
15
14
  Promise.resolve()
16
15
  .then(async () => {
17
- const mesh = await this.mesh$;
18
- try {
19
- if (operationAst.operation === 'subscription') {
20
- const subscriptionResult = await mesh.subscribe(operation.query, operation.variables, operation.getContext(), {}, operation.operationName);
21
- if (Symbol.asyncIterator in subscriptionResult) {
22
- for await (const result of subscriptionResult) {
23
- if (observer.closed) {
24
- return;
25
- }
26
- observer.next(result);
27
- }
28
- observer.complete();
29
- }
30
- }
31
- else {
32
- const result = await mesh.execute(operation.query, operation.variables, operation.getContext(), {}, operation.operationName);
33
- if (!observer.closed) {
34
- observer.next(result);
35
- observer.complete();
16
+ const results = await operationFn(operation.query, operation.variables, operation.getContext(), ROOT_VALUE, operation.operationName);
17
+ if (isAsyncIterable(results)) {
18
+ for await (const result of results) {
19
+ if (observer.closed) {
20
+ return;
36
21
  }
22
+ observer.next(result);
37
23
  }
24
+ observer.complete();
38
25
  }
39
- catch (error) {
26
+ else {
40
27
  if (!observer.closed) {
41
- observer.error(error);
28
+ observer.next(results);
29
+ observer.complete();
42
30
  }
43
31
  }
44
32
  })
@@ -48,6 +36,11 @@ class MeshApolloLink extends ApolloLink {
48
36
  }
49
37
  });
50
38
  });
39
+ };
40
+ }
41
+ class MeshApolloLink extends ApolloLink {
42
+ constructor(options) {
43
+ super(createMeshApolloRequestHandler(options));
51
44
  }
52
45
  }
53
46
 
package/package.json CHANGED
@@ -1,14 +1,20 @@
1
1
  {
2
2
  "name": "@graphql-mesh/apollo-link",
3
- "version": "2.0.0-alpha-e65eb8d69.0",
3
+ "version": "2.0.0-alpha-a952bb598.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@apollo/client": "^3.5.9",
7
- "@graphql-mesh/runtime": "0.33.0-alpha-e65eb8d69.0",
7
+ "@graphql-mesh/runtime": "0.38.0-alpha-a952bb598.0",
8
8
  "graphql": "^15.2.0 || ^16.0.0"
9
9
  },
10
10
  "dependencies": {
11
- "tslib": "2.3.1"
11
+ "@graphql-tools/utils": "8.6.13",
12
+ "tslib": "^2.4.0"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "Urigo/graphql-mesh",
17
+ "directory": "packages/apollo-link"
12
18
  },
13
19
  "license": "MIT",
14
20
  "main": "index.js",