Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to data version and execution metadata #64

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15,309 changes: 0 additions & 15,309 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"homepage": "https://github.com/yarinvak/polaris#readme",
"dependencies": {
"@bitjourney/uuid-v4": "^1.0.1",
"@enigmatis/polaris-logs": "^2.5.3",
"@enigmatis/polaris-logs": "^2.5.4",
"@enigmatis/utills": "^1.3.3",
"@types/graphql": "^14.2.0",
"@types/graphql-fields": "^1.3.0",
Expand Down
13 changes: 13 additions & 0 deletions src/common/execution-result-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as joi from 'joi';

const executionResultSchema = joi.object().keys({
result: joi.any(),
executionMetadata: joi.any(),
});

export function isExecutionResultResponse(candidate: object): boolean {
if (!candidate) {
return false;
}
return !joi.validate(candidate, executionResultSchema, { stripUnknown: true }).error;
}
2 changes: 2 additions & 0 deletions src/inversion-of-control/container-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PolarisLogger } from '@enigmatis/polaris-logs';
import { Container, decorate, injectable } from 'inversify';
import 'reflect-metadata';
import { PolarisGraphQLLogger } from '../logging/polaris-graphql-logger';
import { ExecutionResultMiddleware } from '../middlewares/execution-result-middleware';
import { IrrelevantEntitiesMiddleware } from '../middlewares/irrelevant-entities-middleware';
import { PolarisMiddleware } from '../middlewares/polaris-middleware';
import { DefaultRealitiesHolder } from '../realities-holder/default-realities-holder';
Expand All @@ -13,6 +14,7 @@ decorate(injectable(), PolarisLogger);
export const polarisContainer = new Container();
polarisContainer.bind(POLARIS_TYPES.GraphQLServer).to(PolarisGraphQLServer);
polarisContainer.bind(POLARIS_TYPES.GraphQLLogger).to(PolarisGraphQLLogger);
polarisContainer.bind(POLARIS_TYPES.Middleware).to(ExecutionResultMiddleware);
polarisContainer.bind(POLARIS_TYPES.Middleware).to(PolarisMiddleware);
polarisContainer.bind(POLARIS_TYPES.Middleware).to(IrrelevantEntitiesMiddleware);
polarisContainer.bind(POLARIS_TYPES.RealitiesHolderValidator).to(RealitiesHolderValidator);
Expand Down
19 changes: 19 additions & 0 deletions src/middlewares/execution-result-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { injectable } from 'inversify';
import { isExecutionResultResponse } from '../common/execution-result-validator';
import { Middleware, RequestMiddlewareParams, ResponseMiddlewareParams } from './middleware';

@injectable()
export class ExecutionResultMiddleware implements Middleware {
preResolve({ args }: RequestMiddlewareParams) {
return;
}

postResolve(params: ResponseMiddlewareParams): any {
if (isExecutionResultResponse(params.result)) {
params.context.executionMetadata = params.result.executionMetadata;
return params.result.result;
}

return params.result;
}
}
18 changes: 18 additions & 0 deletions src/server/data-version-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
import { PolarisContext } from './polaris-context';

export class DataVersionExtension extends GraphQLExtension {
willSendResponse(responseContext: {
graphqlResponse: GraphQLResponse;
context: PolarisContext;
}) {
const { context, graphqlResponse } = responseContext;
if (context.body.operationName !== 'IntrospectionQuery') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you checking the operation name?

graphqlResponse.extensions = {
...graphqlResponse.extensions,
dataVersion: context.executionMetadata && context.executionMetadata.dataVersion,
};
}
return responseContext;
}
}
9 changes: 5 additions & 4 deletions src/server/graphql-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SoftDeleteConfiguration } from '@enigmatis/utills';
import { ExecutionMetadata, SoftDeleteConfiguration } from '@enigmatis/utills';
import { ApolloServer, Config, PubSub } from 'apollo-server-koa';
import { GraphQLError, GraphQLFormattedError, GraphQLSchema } from 'graphql';
import { applyMiddleware } from 'graphql-middleware';
Expand All @@ -16,6 +16,7 @@ import { Middleware } from '../middlewares/middleware';
import { createMiddleware } from '../middlewares/polaris-middleware-creator';
import { PolarisProperties } from '../properties/polaris-properties';
import { RealitiesHolderValidator } from '../realities-holder/realities-holder-validator';
import { DataVersionExtension } from './data-version-extension';
import { IrrelevantEntitiesExtension } from './irrelevant-entities-extension';
import { PolarisContext } from './polaris-context';

Expand Down Expand Up @@ -62,6 +63,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
extensions: [
() => new IrrelevantEntitiesExtension(),
() => new ResponseHeadersExtension(),
() => new DataVersionExtension(),
],
...userApolloConfig,
};
Expand All @@ -83,9 +85,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
`🚀 Server ready at http://localhost:${port}${this.server.graphqlPath}`,
);
this.polarisLogger.info(
`🚀 Subscriptions ready at ws://localhost:${port}${
this.server.subscriptionsPath
}`,
`🚀 Subscriptions ready at ws://localhost:${port}${this.server.subscriptionsPath}`,
);
resolve();
});
Expand Down Expand Up @@ -148,6 +148,7 @@ export class PolarisGraphQLServer implements GraphQLServer {
headers,
body: ctx.request.body,
softDeleteConfiguration: this.softDeleteConfiguration,
executionMetadata: { debugDate: new Date() },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove debug date

irrelevantEntities: new IrrelevantEntitiesContainer(),
...this.getCustomContext(ctx),
};
Expand Down
1 change: 1 addition & 0 deletions src/server/irrelevant-entities-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class IrrelevantEntitiesExtension extends GraphQLExtension {

if (context.headers.dataVersion) {
graphqlResponse.extensions = {
...graphqlResponse.extensions,
irrelevantEntities: context.irrelevantEntities.irrelevantContainer,
};
}
Expand Down
4 changes: 1 addition & 3 deletions test/integration-tests/test-server/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { GraphQLClient } from 'graphql-request';
import * as polarisPropertiesPath from './config/properties.json';

export const url = `http://localhost:${polarisPropertiesPath.port}${
polarisPropertiesPath.endpoint
}`;
export const url = `http://localhost:${polarisPropertiesPath.port}${polarisPropertiesPath.endpoint}`;

export const graphQLRequest = async (data: string, headers: any, variables: any = undefined) => {
const graphQLClient = new GraphQLClient(url, { headers });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { PolarisRequestHeaders } from '@enigmatis/utills';
import { UserInputError } from 'apollo-server-koa';
import { PolarisContext } from '../../../../../src/server/polaris-context';
import { Author } from '../definitions/author';
import { Book } from '../definitions/book';
import { BOOK_UPDATED } from './subscription-event-names';

import { getModelCreator, QueryWithIrrelevant } from '@enigmatis/mongo-driver';
import { getModelCreator, getModelExecutor, QueryWithIrrelevant } from '@enigmatis/mongo-driver';
import { authorSchema } from '../../dal/author-model';
import { bookSchema } from '../../dal/book-model';

Expand Down Expand Up @@ -109,13 +108,17 @@ export const bookByIdResolver = async (
if (!Number.isInteger(realityId as any)) {
throw new UserInputError('please provide reality-id header');
} else {
return getModelCreator<Book>('book', bookSchema)(context)
.findOne({ testId: bookId })
.populate({
path: 'author',
model: getModelCreator<Author>('author', authorSchema)(context),
})
.lean();
return getModelExecutor<Book>('book', bookSchema).execute(
m =>
m
.findOne({ testId: bookId })
.populate({
path: 'author',
model: getModelCreator<Author>('author', authorSchema)(context),
})
.lean(),
context,
);
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/integration-tests/tests/irrelevant-entities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const titles = ['first', 'second', 'third', 'fourth', 'fifth'];
const prepareDb = async (headers: PolarisRequestHeaders) => {
const books = [];
for (let i = 0; i < titles.length; i++) {
books.push({ title: titles[i], testId: i, dataVersion: i + 1 });
books.push({ title: titles[i], testId: i });
}
await getModelCreator<Book>('book', bookSchema)({ headers }).create(books);
};
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('irrelevant entities tests', () => {
}`;
const { extensions }: any = await graphqlRawRequest(queryBook, { 'reality-id': 1 });

expect(extensions).not.toBeDefined();
expect(extensions.irrelevantEntities).not.toBeDefined();
});

test('deleted entity is in irrelevant entities', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration-tests/tests/response-headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TestServer } from '../test-server/server';
const titles = ['first', 'second', 'third', 'fourth', 'fifth'];

const prepareDb = async (headers: PolarisRequestHeaders) => {
const context: PolarisBaseContext = { headers };
const context: PolarisBaseContext = { headers, executionMetadata: { dataVersion: 0 } };

const books = [];
for (let i = 0; i < titles.length; i++) {
Expand Down
6 changes: 5 additions & 1 deletion test/integration-tests/tests/soft-delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const defaultAuthor = {
const bookId = '0';

const prepareDb = async (softDeleteConfiguration?: SoftDeleteConfiguration) => {
const context: PolarisBaseContext = { headers, softDeleteConfiguration };
const context: PolarisBaseContext = {
headers,
softDeleteConfiguration,
executionMetadata: { dataVersion: 0 },
};
const author = await getModelCreator<Author>('author', authorSchema)(context).create(
defaultAuthor,
);
Expand Down
6 changes: 6 additions & 0 deletions test/middlewares/filter-executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('filter resolver tests', () => {
headers: { dataVersion: 3, realityId: 0 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -60,6 +61,7 @@ describe('filter resolver tests', () => {
headers: { realityId: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -81,6 +83,7 @@ describe('filter resolver tests', () => {
headers: { realityId: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -102,6 +105,7 @@ describe('filter resolver tests', () => {
headers: { realityId: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -125,6 +129,7 @@ describe('filter resolver tests', () => {
headers: { realityId: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -144,6 +149,7 @@ describe('filter resolver tests', () => {
headers: { realityId: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand Down
7 changes: 7 additions & 0 deletions test/middlewares/irrelevant-entities-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('split relevantEntities and irrelevant', () => {
headers: { dataVersion: 3 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -51,6 +52,7 @@ describe('split relevantEntities and irrelevant', () => {
headers: { dataVersion: 3 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -70,6 +72,7 @@ describe('split relevantEntities and irrelevant', () => {
headers: { dataVersion: 0 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -94,6 +97,7 @@ describe('not modifying result if not needed', () => {
headers: {},
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -112,6 +116,7 @@ describe('not modifying result if not needed', () => {
headers: {},
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -133,6 +138,7 @@ describe('not modifying result if not needed', () => {
headers: {},
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand All @@ -151,6 +157,7 @@ describe('not modifying result if not needed', () => {
headers: {},
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('data version tests', () => {
headers: {},
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root: undefined,
Expand All @@ -30,6 +31,7 @@ describe('data version tests', () => {
headers: { dataVersion: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root: undefined,
Expand All @@ -45,6 +47,7 @@ describe('data version tests', () => {
headers: { dataVersion: 1 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root: undefined,
Expand All @@ -60,6 +63,7 @@ describe('data version tests', () => {
headers: { dataVersion: 2 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root: undefined,
Expand All @@ -75,6 +79,7 @@ describe('data version tests', () => {
headers: { dataVersion: 2 },
body: {},
irrelevantEntities: new IrrelevantEntitiesContainer(),
executionMetadata: { dataVersion: 0 },
};
const middlewareParams: ResponseMiddlewareParams = {
root: undefined,
Expand Down
Loading