Skip to content

Commit

Permalink
Working first version
Browse files Browse the repository at this point in the history
  • Loading branch information
MRVDH committed Aug 31, 2023
1 parent caaea10 commit 82777ca
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/apps/graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
"main": "index.js",
"scripts": {
"build": "pnpm prisma:generate && heft build --clean",
"devnet": "docker run -it -p 8080:8080 -p 5432:5432 -p 9999:9999 -v $HOME/.kda-devnet/data:/data -v ./cwd-extra-migrations:/cwd-extra-migrations enof/devnet:l2-latest-arm64",
"devnet:update": "docker pull enof/devnet:l2-latest",
"format": "pnpm run /^format:.*/",
"format:src": "prettier config src --write",
"lint": "eslint package.json src --ext .js,.ts --fix",
"prisma:generate": "prisma generate",
"prisma:pull": "prisma db pull",
"prisma:studio": "prisma studio",
"devnet": "docker run -it -p 8080:8080 -p 5432:5432 -p 9999:9999 -v $HOME/.kda-devnet/data:/data -v ./cwd-extra-migrations:/cwd-extra-migrations enof/devnet:l2-latest-arm64",
"devnet:update": "docker pull enof/devnet:l2-latest",
"start": "pnpm run prisma:generate && npx ts-node-dev --respawn --no-notify --exit-child src/index.ts",
"test": "echo \"no test specified\""
},
Expand Down
18 changes: 8 additions & 10 deletions packages/apps/graph/src/graph/Subscription/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prismaClient } from '../../db/prismaClient';
import { builder } from '../builder';
import { builder, Context } from '../builder';

import { Transaction } from '@prisma/client';
import _debug, { Debugger } from 'debug';
Expand All @@ -13,32 +13,30 @@ builder.subscriptionField('transaction', (t) => {
},
type: 'Transaction',
nullable: true,
subscribe: (parent, args, context, info) => iteratorFn(args.requestKey),
subscribe: (parent, args, context, info) =>
iteratorFn(args.requestKey, context),
resolve: (__, transaction) => transaction,
});
});

async function* iteratorFn(
requestKey: string,
): AsyncGenerator<Transaction, void, unknown> {
console.log('iteratorFn', requestKey);
log('iteratorFn', requestKey);
while (true) {
context: Context,
): AsyncGenerator<Transaction | null, void, unknown> {
while (!context.req.socket.destroyed) {
const transaction = await prismaClient.transaction.findFirst({
where: {
requestkey: requestKey,
},
});

if (transaction) {
console.log('transaction found', transaction);
log('transaction found', transaction);
yield transaction;
return;
}

console.log('waiting for transaction ' + requestKey);
log('waiting for transaction ' + requestKey);
log(`waiting for transaction ${requestKey}`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
6 changes: 6 additions & 0 deletions packages/apps/graph/src/graph/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DateTimeResolver,
PositiveFloatResolver,
} from 'graphql-scalars';
import { IncomingMessage } from 'http';

interface IDefaultTypesExtension {
Scalars: {
Expand All @@ -28,10 +29,15 @@ interface IDefaultTypesExtension {
};
}

export interface Context {
req: IncomingMessage;
}

// eslint-disable-next-line @rushstack/typedef-var
export const builder = new SchemaBuilder<
IDefaultTypesExtension & {
PrismaTypes: PrismaTypes;
Context: Context;
}
>({
plugins: [RelayPlugin, PrismaPlugin],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export async function startDevelopmentWorker(): Promise<void> {
)
.catch(console.error);

if (i > 38 && statements.length > 0) {
await new Promise((resolve) => setTimeout(resolve, 5000));
if (i > 5 && statements.length > 0) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
i++;
}
Expand Down

0 comments on commit 82777ca

Please sign in to comment.