@envelop/apollo-datasources 1.0.0-alpha-58625a2.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.
- package/README.md +51 -0
- package/index.d.ts +10 -0
- package/index.js +32 -0
- package/index.mjs +28 -0
- package/package.json +35 -0
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
## `@envelop/apollo-datasources`
|
|
2
|
+
|
|
3
|
+
This plugin integrates Apollo DataSources into Envelop.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
yarn add @envelop/apollo-datasources
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage Example
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { envelop } from '@envelop/core';
|
|
15
|
+
import { useApolloDataSources } from '@envelop/apollo-datasources';
|
|
16
|
+
import { RESTDataSource } from 'apollo-datasource-rest';
|
|
17
|
+
|
|
18
|
+
class MoviesAPI extends RESTDataSource {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.baseURL = 'https://movies-api.example.com/';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async getMovie(id) {
|
|
25
|
+
return this.get(`movies/${encodeURIComponent(id)}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async getMostViewedMovies(limit = 10) {
|
|
29
|
+
const data = await this.get('movies', {
|
|
30
|
+
per_page: limit,
|
|
31
|
+
order_by: 'most_viewed',
|
|
32
|
+
});
|
|
33
|
+
return data.results;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const getEnveloped = envelop({
|
|
38
|
+
plugins: [
|
|
39
|
+
// ... other plugins ...
|
|
40
|
+
useApolloDataSources({
|
|
41
|
+
dataSources() {
|
|
42
|
+
return {
|
|
43
|
+
moviesAPI: new MoviesAPI(),
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
// To provide a custom cache, you can use the following code (InMemoryLRUCache is used by default):
|
|
47
|
+
// cache: new YourCustomCache()
|
|
48
|
+
}),
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from '@envelop/core';
|
|
2
|
+
import type { KeyValueCache } from 'apollo-server-caching';
|
|
3
|
+
import type { DataSource } from 'apollo-datasource';
|
|
4
|
+
export interface ApolloDataSourcesConfig {
|
|
5
|
+
dataSources(): {
|
|
6
|
+
[name: string]: DataSource;
|
|
7
|
+
};
|
|
8
|
+
cache?: KeyValueCache;
|
|
9
|
+
}
|
|
10
|
+
export declare function useApolloDataSources(config: ApolloDataSourcesConfig): Plugin;
|
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const apolloServerCaching = require('apollo-server-caching');
|
|
6
|
+
|
|
7
|
+
function useApolloDataSources(config) {
|
|
8
|
+
const cache = config.cache || new apolloServerCaching.InMemoryLRUCache();
|
|
9
|
+
return {
|
|
10
|
+
async onExecute({ extendContext, args }) {
|
|
11
|
+
const dataSources = config.dataSources();
|
|
12
|
+
const initializers = [];
|
|
13
|
+
for (const dataSource of Object.values(dataSources)) {
|
|
14
|
+
if (dataSource.initialize) {
|
|
15
|
+
initializers.push(dataSource.initialize({
|
|
16
|
+
context: args.contextValue,
|
|
17
|
+
cache,
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
await Promise.all(initializers);
|
|
22
|
+
if ('dataSources' in args.contextValue) {
|
|
23
|
+
throw new Error('Please use the dataSources config option instead of putting dataSources on the context yourself.');
|
|
24
|
+
}
|
|
25
|
+
extendContext({
|
|
26
|
+
dataSources,
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.useApolloDataSources = useApolloDataSources;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { InMemoryLRUCache } from 'apollo-server-caching';
|
|
2
|
+
|
|
3
|
+
function useApolloDataSources(config) {
|
|
4
|
+
const cache = config.cache || new InMemoryLRUCache();
|
|
5
|
+
return {
|
|
6
|
+
async onExecute({ extendContext, args }) {
|
|
7
|
+
const dataSources = config.dataSources();
|
|
8
|
+
const initializers = [];
|
|
9
|
+
for (const dataSource of Object.values(dataSources)) {
|
|
10
|
+
if (dataSource.initialize) {
|
|
11
|
+
initializers.push(dataSource.initialize({
|
|
12
|
+
context: args.contextValue,
|
|
13
|
+
cache,
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
await Promise.all(initializers);
|
|
18
|
+
if ('dataSources' in args.contextValue) {
|
|
19
|
+
throw new Error('Please use the dataSources config option instead of putting dataSources on the context yourself.');
|
|
20
|
+
}
|
|
21
|
+
extendContext({
|
|
22
|
+
dataSources,
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { useApolloDataSources };
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@envelop/apollo-datasources",
|
|
3
|
+
"version": "1.0.0-alpha-58625a2.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"@envelop/core": "^2.3.1",
|
|
7
|
+
"apollo-datasource": "^3",
|
|
8
|
+
"apollo-server-caching": "^3",
|
|
9
|
+
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/dotansimha/envelop.git",
|
|
14
|
+
"directory": "packages/plugins/apollo-datasources"
|
|
15
|
+
},
|
|
16
|
+
"author": "Kamil Kisiela <kamil.kisiela@gmail.com>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"main": "index.js",
|
|
19
|
+
"module": "index.mjs",
|
|
20
|
+
"typings": "index.d.ts",
|
|
21
|
+
"typescript": {
|
|
22
|
+
"definition": "index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"require": "./index.js",
|
|
27
|
+
"import": "./index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./*": {
|
|
30
|
+
"require": "./*.js",
|
|
31
|
+
"import": "./*.mjs"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
}
|
|
35
|
+
}
|