-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (30 loc) · 963 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const {
DirectiveLocation,
GraphQLDirective,
defaultFieldResolver,
} = require('graphql');
const { SchemaDirectiveVisitor } = require('graphql-tools');
class convertToUpperCase extends SchemaDirectiveVisitor {
static getDirectiveDeclaration(directiveName = 'upper') {
return new GraphQLDirective({
// register directive name
name: directiveName,
// register a description
description: 'Converts (string) field to upper case form',
// register where this directive can be used
locations: [DirectiveLocation.FIELD_DEFINITION],
});
}
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
// eslint-disable-next-line
field.resolve = async function(...args) {
const result = await resolve.apply(this, args);
if (typeof result === 'string') {
return result.toUpperCase();
}
return result;
};
}
}
module.exports = convertToUpperCase;