mirror of
https://github.com/vale981/apollo-server
synced 2025-03-05 09:41:40 -05:00
Extract abstract DataSource superclass into apollo-datasource package
This commit is contained in:
parent
299018fd5c
commit
f055d282e0
9 changed files with 98 additions and 12 deletions
|
@ -23,6 +23,7 @@
|
|||
"node": ">=6"
|
||||
},
|
||||
"dependencies": {
|
||||
"apollo-datasource": "^2.0.0-rc.7",
|
||||
"apollo-server-caching": "^2.0.0-rc.7",
|
||||
"apollo-server-env": "^2.0.0-rc.7",
|
||||
"apollo-server-errors": "^2.0.0-rc.7",
|
||||
|
|
|
@ -9,6 +9,9 @@ import {
|
|||
URLSearchParamsInit,
|
||||
} from 'apollo-server-env';
|
||||
|
||||
import { DataSource } from 'apollo-datasource';
|
||||
|
||||
import { KeyValueCache } from 'apollo-server-caching';
|
||||
import { HTTPCache } from './HTTPCache';
|
||||
|
||||
import {
|
||||
|
@ -29,10 +32,15 @@ export { Request };
|
|||
|
||||
type ValueOrPromise<T> = T | Promise<T>;
|
||||
|
||||
export abstract class RESTDataSource<TContext = any> {
|
||||
export abstract class RESTDataSource<TContext = any> extends DataSource {
|
||||
httpCache!: HTTPCache;
|
||||
context!: TContext;
|
||||
|
||||
initialize(context: TContext, cache: KeyValueCache): void {
|
||||
this.context = context;
|
||||
this.httpCache = new HTTPCache(cache);
|
||||
}
|
||||
|
||||
baseURL?: string;
|
||||
|
||||
protected willSendRequest?(request: RequestOptions): ValueOrPromise<void>;
|
||||
|
|
6
packages/apollo-datasource/.npmignore
Normal file
6
packages/apollo-datasource/.npmignore
Normal file
|
@ -0,0 +1,6 @@
|
|||
*
|
||||
!src/**/*
|
||||
!dist/**/*
|
||||
dist/**/*.test.*
|
||||
!package.json
|
||||
!README.md
|
57
packages/apollo-datasource/package.json
Normal file
57
packages/apollo-datasource/package.json
Normal file
|
@ -0,0 +1,57 @@
|
|||
{
|
||||
"name": "apollo-datasource",
|
||||
"version": "2.0.0-rc.7",
|
||||
"author": "opensource@apollographql.com",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/apollographql/apollo-server/tree/master/packages/apollo-datasource"
|
||||
},
|
||||
"homepage": "https://github.com/apollographql/apollo-server#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/apollographql/apollo-server/issues"
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rm -rf dist",
|
||||
"compile": "tsc",
|
||||
"prepublish": "npm run clean && npm run compile",
|
||||
"test": "jest --verbose"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"dependencies": {
|
||||
"apollo-server-caching": "^2.0.0-rc.7",
|
||||
"apollo-server-env": "^2.0.0-rc.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^23.1.2",
|
||||
"jest": "^23.2.0",
|
||||
"ts-jest": "^22.4.6"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node",
|
||||
"setupFiles": [
|
||||
"<rootDir>/node_modules/apollo-server-env/dist/index.js"
|
||||
],
|
||||
"transform": {
|
||||
"^.+\\.(ts|js)$": "ts-jest"
|
||||
},
|
||||
"moduleFileExtensions": [
|
||||
"ts",
|
||||
"js",
|
||||
"json"
|
||||
],
|
||||
"testRegex": "apollo-datasource-rest/src/__tests__/.*$",
|
||||
"roots": [
|
||||
"../../"
|
||||
],
|
||||
"globals": {
|
||||
"ts-jest": {
|
||||
"skipBabel": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
5
packages/apollo-datasource/src/index.ts
Normal file
5
packages/apollo-datasource/src/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { KeyValueCache } from 'apollo-server-caching';
|
||||
|
||||
export abstract class DataSource<TContext = any> {
|
||||
abstract initialize(context: TContext, cache: KeyValueCache): void;
|
||||
}
|
17
packages/apollo-datasource/tsconfig.json
Normal file
17
packages/apollo-datasource/tsconfig.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"extends": "../../tsconfig",
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
"removeComments": true,
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUnusedParameters": true,
|
||||
"noUnusedLocals": true,
|
||||
"types": ["node", "jest"]
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "**/__tests__/*", "**/__mocks__/*"]
|
||||
}
|
|
@ -30,7 +30,7 @@
|
|||
"dependencies": {
|
||||
"@types/ws": "^5.1.2",
|
||||
"apollo-cache-control": "^0.2.0-rc.0",
|
||||
"apollo-datasource-rest": "^2.0.0-rc.7",
|
||||
"apollo-datasource": "^2.0.0-rc.7",
|
||||
"apollo-engine-reporting": "^0.0.0-rc.1",
|
||||
"apollo-server-caching": "^2.0.0-rc.7",
|
||||
"apollo-server-env": "^2.0.0-rc.7",
|
||||
|
|
|
@ -7,6 +7,7 @@ import { HttpHeaderCalculation } from './caching';
|
|||
import { GraphQLExtension } from 'graphql-extensions';
|
||||
import { CacheControlExtensionOptions } from 'apollo-cache-control';
|
||||
import { KeyValueCache } from 'apollo-server-caching';
|
||||
import { DataSource } from 'apollo-datasource';
|
||||
|
||||
/*
|
||||
* GraphQLServerOptions
|
||||
|
@ -50,10 +51,6 @@ export interface GraphQLServerOptions<
|
|||
persistedQueries?: PersistedQueryOptions;
|
||||
}
|
||||
|
||||
export interface DataSource<TContext> {
|
||||
context: TContext;
|
||||
}
|
||||
|
||||
export type DataSources<TContext> = {
|
||||
[name: string]: DataSource<TContext>;
|
||||
};
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import { ExecutionResult } from 'graphql';
|
||||
const sha256 = require('hash.js/lib/hash/sha/256');
|
||||
|
||||
import { HTTPCache } from 'apollo-datasource-rest';
|
||||
import { CacheControlExtensionOptions } from 'apollo-cache-control';
|
||||
|
||||
import { omit } from 'lodash';
|
||||
|
@ -338,12 +337,8 @@ export async function runHttpQuery(
|
|||
if (optionsObject.dataSources) {
|
||||
const dataSources = optionsObject.dataSources() || {};
|
||||
|
||||
// we use the cache provided to the request and add the Http semantics on top
|
||||
const httpCache = new HTTPCache(optionsObject.cache);
|
||||
|
||||
for (const dataSource of Object.values(dataSources)) {
|
||||
dataSource.context = context;
|
||||
(dataSource as any).httpCache = httpCache;
|
||||
dataSource.initialize(context, optionsObject.cache!);
|
||||
}
|
||||
|
||||
if ('dataSources' in context) {
|
||||
|
|
Loading…
Add table
Reference in a new issue