Skip to content
This repository has been archived by the owner on Mar 13, 2020. It is now read-only.

Add irrelevant entities query #2

Open
wants to merge 4 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
28 changes: 28 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/graphql-server-for-polaris.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions context-builder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {GraphQLLogger, PolarisGraphQLLogger} from "@enigmatis/polaris-graphql-logger";
import {DeltaMiddlewareContext} from '@enigmatis/polaris-delta-middleware';
import { PolarisConnection } from "@enigmatis/polaris-typeorm/src/connections/connection";

export interface PolarisContext extends DeltaMiddlewareContext {
import {PolarisBaseContext} from "@enigmatis/polaris-common"
import { Connection } from "@enigmatis/polaris-typeorm";

export interface PolarisContext extends PolarisBaseContext{
}

export class ContextBuilder {
private _logger: GraphQLLogger;
private _res: any;
private _dataVersion: number;
private _realityId: number;
private _includeLinkedOper: boolean;
Expand Down Expand Up @@ -40,21 +40,25 @@ export class ContextBuilder {
this._includeLinkedOper = includeLinkedOper == "false"? false: true;
}

res(res){
this._res = res;
}

build(): PolarisContext {
return {logger: this._logger, dataVersion: this._dataVersion};
return {logger: this._logger, dataVersion: this._dataVersion, res: this._res};
}
}

export class ContextInitializer {
private readonly logger: GraphQLLogger;
private readonly polarisConnection: PolarisConnection;
private readonly polarisConnection: Connection;

constructor(logger: GraphQLLogger, polarisConnection: PolarisConnection) {
constructor(logger: GraphQLLogger, polarisConnection: Connection) {
this.logger = logger;
this.polarisConnection = polarisConnection;
}

async initializeContextForRequest({req}) {
async initializeContextForRequest({req, res}) {
const contextBuilder = new ContextBuilder();
let dataVersionHeader = req.headers['data-version'];
let realityIdHeader = req.headers['reality-id'];
Expand All @@ -69,8 +73,10 @@ export class ContextInitializer {
contextBuilder.includeLinkedOper(includeLinkedOper);
}
contextBuilder.realityId(realityIdHeader);
contextBuilder.res(res);
let context: PolarisContext = contextBuilder.build();
res.locals.irrelevantEntities = {};
this.polarisConnection.manager.queryRunner.data.context = context;
return context;
}
}
}
46 changes: 30 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import {applyMiddleware} from 'graphql-middleware';
import {
dataVersionMiddleware,
softDeletedMiddleware,
ExtensionsPlugin
} from '../polaris-delta-middleware';
import {realitiesMiddleware} from '../polaris-realities-middleware'
ExtensionsPlugin,
realitiesMiddleware,
irrelevantEntitiesMiddleware
} from '@enigmatis/polaris-middlewares';
import {
repositoryEntityTypeDefs,
scalarsResolvers,
scalarsTypeDefs
} from '../polaris-schema';
import {ContextInitializer} from "./context-builder";
} from '@enigmatis/polaris-schema';
import {ContextInitializer, PolarisContext} from "./context-builder";
import {Book} from "./dal/book";
import {
CommonModel,
DataVersion,
createPolarisConnection,
ConnectionOptions
} from '../polaris-typeorm';
import {Like} from '@enigmatis/polaris-typeorm'
import {PolarisGraphQLLogger} from '@enigmatis/polaris-graphql-logger';
import "reflect-metadata";

Expand All @@ -41,11 +43,7 @@ const books = [

let connectionOptions: ConnectionOptions = {
type: "postgres",
host: "localhost",
port: 5432,
username: "postgres",
password: "Aa123456",
database: "postgres",
url:"postgres://dbgazwwm:[email protected]:5432/dbgazwwm",
entities: [
__dirname + '/dal/*.ts',
CommonModel,
Expand All @@ -63,22 +61,32 @@ const applicationLogProperties = {
version: '1'
};

const polarisGraphQLLogger = new PolarisGraphQLLogger(applicationLogProperties, {
const polarisGraphQLLogger = new PolarisGraphQLLogger<PolarisContext>(applicationLogProperties, {
loggerLevel: 'debug',
writeToConsole: true,
writeFullMessageToConsole: false
});

const play = async () => {
const connection = await createPolarisConnection(connectionOptions,{});
// @ts-ignore
const connection = await createPolarisConnection(connectionOptions, polarisGraphQLLogger);

let initDb = async () => {
await connection.dropDatabase();
const tables = ['user', 'profile', 'book', 'author', 'library', 'dataVersion'];
for (const table of tables) {
if (connection.manager) {
try {
await connection.manager.getRepository(table).query('DELETE FROM "' + table + '";');
}catch (e) {

}
}
}
await connection.synchronize();
let bookRepo = connection.getRepository(Book);
let book1 = new Book('Harry Potter and the Chamber of Secrets', 'J.K. Rowling');
let book2 = new Book('Jurassic Park', 'Michael Crichton');
// await bookRepo.save([book1, book2]);
await bookRepo.save([book1, book2]);
};

const typeDefs = gql`
Expand All @@ -103,6 +111,7 @@ const play = async () => {
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
booksStartWith(startWith: String!): [Book]
bla: String
}
type Mutation {
Expand All @@ -118,6 +127,11 @@ const play = async () => {
let books = await bookRepo.find();
return books;
},
booksStartWith: async (root, args, context, info) => {
const bookRepo = connection.getRepository(Book);
let books = await bookRepo.find({where:{title: Like(`${args.startWith}%`)}});
return books;
},
bla: () => "bla"
},
Mutation: {
Expand All @@ -141,10 +155,10 @@ const play = async () => {
resolvers: [resolvers, scalarsResolvers]
});

const executableSchema = applyMiddleware(schema, dataVersionMiddleware, softDeletedMiddleware, realitiesMiddleware);
const executableSchema = applyMiddleware(schema, dataVersionMiddleware, softDeletedMiddleware, realitiesMiddleware, irrelevantEntitiesMiddleware);
const config: ApolloServerExpressConfig = {
schema: executableSchema,
context: ({req}) => new ContextInitializer(polarisGraphQLLogger, connection).initializeContextForRequest({req}),
context: ({req,res}) => new ContextInitializer(polarisGraphQLLogger, connection).initializeContextForRequest({req, res}),
plugins: [() => new ExtensionsPlugin(connection.getRepository(DataVersion))],
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"link": "npm link @enigmatis/polaris-schema && npm link @enigmatis/polaris-delta-middleware && npm link @enigmatis/polaris-realities-middleware && npm link @enigmatis/polaris-graphql-logger && npm link @enigmatis/polaris-typeorm",
"link": "npm link @enigmatis/polaris-schema && npm link @enigmatis/polaris-middlewares && npm link @enigmatis/polaris-graphql-logger && npm link @enigmatis/polaris-typeorm && npm link @enigmatis/polaris-common",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc & ts-node index.ts"
},
Expand Down