Files
graphql-api-js/src/context.ts
T
Arwid ThornströmandGitHub c68c182b64 3010: Apollo 4 major update and complete security overhaul to prepare for public api support
* 2919: rewrite for update to apollo 4 (#131)

* rewrite for update to apollo 4

* removed container tests, edited make to publish any branch to staging

* revert makefile

* cleanup

* update 1

* more fixes to get it working on aws

* enabled csrf prevention protection, testing if build works

* switched to express version

* changed the healthcheck url

* added cache to restdatasources

* 3011: create a login mutation on graphql server that responds with a access token (#133)

* 3011 added login rest endpoint and general scope fixes

* changed login auth to basic auth username and password

* minor changes from CR feedback

* force update

* added tighter timeout for idle knex connection

* Refactor authentication and scopes (#134)

* big refactor of scopes in graphql

* cr fixes

* some security fixes (#135)

* some security fixes

* cleanup

* update staging

* minor readme change
2022-12-12 12:58:24 +01:00

104 lines
3.2 KiB
TypeScript

import { ApolloServer } from '@apollo/server';
import knexStringcase from 'knex-stringcase';
import {
ClaimVerifyResult,
verifyAuthentication,
} from './cognito/cognito-client';
import { dbConfig } from './config';
import { CategoryAPI } from './datasources/category-api';
import { DesignerAPI } from './datasources/designer-api';
import { ImageServerApi } from './datasources/imageserver-api';
import { InteriorAPI } from './datasources/interior-api';
import { InteriorsLambdaAPI } from './datasources/interiors-lambda-api';
import { KeywordAPI } from './datasources/keyword-api';
import { MarketLocaleAPI } from './datasources/market-locale-api';
import { OrderAPI } from './datasources/order-api';
import { PrintProductDefaultsAPI } from './datasources/printproduct-defaults-api';
import { ProductAPI } from './datasources/product-api';
import { TextsAPI } from './datasources/texts-api';
import { IncomingMessage } from 'http';
import { DataSourceOptions, IAuthObject } from './types/types';
const knexConfig = knexStringcase(dbConfig);
export class ContextValue {
public req: IncomingMessage;
public server: ApolloServer<ContextValue>;
public user: IAuthObject | ClaimVerifyResult;
public dataSources: {
marketLocaleApi: MarketLocaleAPI;
textsApi: TextsAPI;
categoryApi: CategoryAPI;
designerApi: DesignerAPI;
imageServerApi: ImageServerApi;
interiorsLambdaApi: InteriorsLambdaAPI;
interiorApi: InteriorAPI;
printProductDefaultsApi: PrintProductDefaultsAPI;
keywordApi: KeywordAPI;
orderApi: OrderAPI;
productApi: ProductAPI;
};
constructor({
req,
server,
}: {
req: IncomingMessage;
server: ApolloServer<ContextValue>;
}) {
this.req = req;
this.server = server;
}
async process() {
const { cache } = this.server;
// -------------------------------
// Authentication
// -------------------------------
this.user = await verifyAuthentication(this.req);
// -------------------------------
// DataSources
// -------------------------------
const options: DataSourceOptions = { cache, user: this.user };
const marketLocaleApi = new MarketLocaleAPI(options, knexConfig);
const textsApi = new TextsAPI(options, knexConfig, marketLocaleApi);
const categoryApi = new CategoryAPI(options, knexConfig);
const designerApi = new DesignerAPI(options, knexConfig);
const imageServerApi = new ImageServerApi(options);
const interiorsLambdaApi = new InteriorsLambdaAPI(options);
const interiorApi = new InteriorAPI(options, knexConfig, imageServerApi);
const printProductDefaultsApi = new PrintProductDefaultsAPI(
options,
knexConfig,
);
const keywordApi = new KeywordAPI(options, knexConfig, textsApi);
const orderApi = new OrderAPI(options, knexConfig);
const productApi = new ProductAPI(
options,
knexConfig,
textsApi,
printProductDefaultsApi,
);
this.dataSources = {
productApi,
marketLocaleApi,
textsApi,
categoryApi,
designerApi,
imageServerApi,
interiorsLambdaApi,
interiorApi,
printProductDefaultsApi,
keywordApi,
orderApi,
};
return this;
}
}