Skip to content

Commit 3434aeb

Browse files
sample(): add plugin and directive example (graphql)
1 parent cd20f41 commit 3434aeb

8 files changed

Lines changed: 423 additions & 43 deletions

File tree

sample/12-graphql-schema-first/package-lock.json

Lines changed: 212 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/12-graphql-schema-first/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"dependencies": {
2222
"@nestjs/common": "6.11.8",
2323
"@nestjs/core": "6.11.8",
24-
"@nestjs/graphql": "6.6.2",
24+
"@nestjs/graphql": "^7.0.0-next.5",
2525
"@nestjs/platform-express": "6.11.8",
26+
"apollo-server": "2.11.0",
2627
"apollo-server-express": "2.10.1",
2728
"class-transformer": "0.2.3",
2829
"class-validator": "0.11.0",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { SchemaDirectiveVisitor } from 'apollo-server';
2+
import { defaultFieldResolver, GraphQLField } from 'graphql';
3+
4+
export class UpperCaseDirective extends SchemaDirectiveVisitor {
5+
visitFieldDefinition(field: GraphQLField<any, any>) {
6+
const { resolve = defaultFieldResolver } = field;
7+
field.resolve = async function(...args) {
8+
const result = await resolve.apply(this, args);
9+
if (typeof result === 'string') {
10+
return result.toUpperCase();
11+
}
12+
return result;
13+
};
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Plugin } from '@nestjs/graphql';
2+
import {
3+
ApolloServerPlugin,
4+
GraphQLRequestListener,
5+
} from 'apollo-server-plugin-base';
6+
7+
@Plugin()
8+
export class LoggingPlugin implements ApolloServerPlugin {
9+
requestDidStart(): GraphQLRequestListener {
10+
console.log('Request started');
11+
return {
12+
willSendResponse() {
13+
console.log('Will send response');
14+
},
15+
};
16+
}
17+
}

0 commit comments

Comments
 (0)