Skip to content

Commit

Permalink
feat(gql): implements gql parser for express
Browse files Browse the repository at this point in the history
The GqlParser now is working for express. There are updates to the main module packages to handle
whenever subscriptions come in, as there is not enough information in them to properly add them to
the request logging. This may change in the future.

fix #14
  • Loading branch information
jmcdo29 committed Apr 13, 2020
1 parent 4b1b8fa commit 9290504
Show file tree
Hide file tree
Showing 24 changed files with 8,920 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ coverage/

*.tgz

*.txt
*.txt
*.gql
30 changes: 30 additions & 0 deletions integration/graphql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@ogma/platform-graphql-integration",
"version": "0.0.0",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"start": "nest start",
"visual": "node testScript"
},
"private": true,
"dependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/graphql": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"apollo-server-express": "^2.11.0",
"graphql": "^14.0.0",
"graphql-subscriptions": "^1.1.0",
"graphql-tools": "^4.0.7"
},
"devDependencies": {
"@nestjs/cli": "^7.1.1",
"@nestjs/testing": "^7.0.0",
"jest": "^25.2.3",
"reflect-metadata": "0.1.13",
"rimraf": "^3.0.2",
"rxjs": "6.5.4",
"ts-jest": "^25.2.1"
}
}
26 changes: 26 additions & 0 deletions integration/graphql/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { OgmaModule } from '@ogma/nestjs-module';
import { GraphQLParser } from '@ogma/platform-graphql';
import { PubSub } from 'graphql-subscriptions';
import { AppResolver } from './app.resolver';
import { AppService } from './app.service';

@Module({
imports: [
OgmaModule.forRoot({
interceptor: {
gql: GraphQLParser,
},
}),
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
context: ({ req, res }) => {
return { req, res };
},
installSubscriptionHandlers: true,
}),
],
providers: [AppResolver, AppService, PubSub],
})
export class AppModule {}
42 changes: 42 additions & 0 deletions integration/graphql/src/app.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { BadRequestException } from '@nestjs/common';
import { Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { OgmaSkip } from '@ogma/nestjs-module';
import { PubSub } from 'graphql-subscriptions';
import { AppService } from './app.service';
import { SimpleObject } from './models';

@Resolver(() => SimpleObject)
export class AppResolver {
constructor(
private readonly appService: AppService,
private readonly pubSub: PubSub,
) {}

@Query(() => SimpleObject)
getQuery(): SimpleObject {
const hello = this.appService.getHello();
this.pubSub.publish('saidHello', hello);
return hello;
}

@Mutation(() => SimpleObject)
getMutation(): SimpleObject {
return this.appService.getHello();
}

@Query(() => SimpleObject)
getError(): never {
throw new BadRequestException('Borked');
}

@OgmaSkip()
@Query(() => SimpleObject)
getSkip(): SimpleObject {
return this.appService.getHello();
}

@Subscription(() => SimpleObject)
saidHello() {
return this.pubSub.asyncIterator<SimpleObject>('saidHello');
}
}
9 changes: 9 additions & 0 deletions integration/graphql/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Injectable } from '@nestjs/common';
import { SimpleObject } from './models';

@Injectable()
export class AppService {
getHello(): SimpleObject {
return { key: 'value' };
}
}
12 changes: 12 additions & 0 deletions integration/graphql/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { OgmaService } from '@ogma/nestjs-module';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = app.get(OgmaService);
await app.listen(3000);
logger.log('Graphql Listening at ' + (await app.getUrl()) + '/graphql');
}

bootstrap();
1 change: 1 addition & 0 deletions integration/graphql/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './simple-object.model';
7 changes: 7 additions & 0 deletions integration/graphql/src/models/simple-object.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql';

@ObjectType()
export class SimpleObject {
@Field()
key: string;
}
28 changes: 28 additions & 0 deletions integration/graphql/testScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { request } = require('http');

const baseUrl = 'http://localhost:3000/graphql';

function cb(res) {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => console.log(data));
}

function makeCall(gqlQuery) {
const req = request(
baseUrl,
{ method: 'POST', headers: { 'Content-Type': 'application/json' } },
cb,
);
req.write(gqlQuery);
req.end();
}

function makeQueryString(type, name) {
return `{"query": "${type} ${name}{ ${name} { key }}"}`;
}

makeCall(makeQueryString('query', 'getQuery'));
makeCall(makeQueryString('query', 'getError'));
makeCall(makeQueryString('mutation', 'getMutation'));
makeCall(makeQueryString('query', 'getSkip'));
3 changes: 3 additions & 0 deletions integration/graphql/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
9 changes: 9 additions & 0 deletions integration/graphql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["./src"]
}
Loading

0 comments on commit 9290504

Please sign in to comment.