-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage-example.ts
108 lines (90 loc) · 2.88 KB
/
usage-example.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { CoercibleGraphQLInputField, defaultCoercer, CoercibleGraphQLArgument, coerceFieldArgumentsValues, pathToArray } from './src';
import { assert } from 'chai';
import { SchemaDirectiveVisitor, makeExecutableSchema, visitSchema, SchemaVisitor } from 'graphql-tools';
import { GraphQLField, defaultFieldResolver, graphql } from 'graphql';
const typeDefs = `
directive @length(max: Int!) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
type Query {
books: [Book]
}
type Book {
title: String
}
type Mutation {
createBook(book: BookInput): Book
}
input BookInput {
title: String! @length(max: 50)
}`;
class LengthDirective<TContext> extends SchemaDirectiveVisitor<{ max: number }, TContext> {
visitInputFieldDefinition(field: CoercibleGraphQLInputField<string, TContext>) {
const { coerce } = field;
field.coerce = async (value, ...args) => {
// called other coercers
if (coerce) value = await coerce(value, ...args);
const { path } = args[1]; // inputCoerceInfo
const { max } = this.args;
assert.isAtMost(value.length, max, `${pathToArray(path).join('.')} length`);
return value;
}
}
visitArgumentDefinition(argument: CoercibleGraphQLArgument<string, TContext>) {
const { coerce = defaultCoercer } = argument;
argument.coerce = async (value, ...args) => {
// called other coercers
if (coerce) value = await coerce(value, ...args);
assert.isAtMost(value.length, this.args.max);
return value;
}
}
visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async (...args) => {
const value = await resolve(...args);
value && assert.isAtMost(value.length, this.args.max);
return value;
}
}
}
const schema = makeExecutableSchema({
typeDefs,
resolvers: {
Mutation: {
createBook: (_, { book }) => book,
}
},
schemaDirectives: {
// @ts-ignore
length: LengthDirective
}
});
class FieldResoverWrapperVisitor<TContext> extends SchemaVisitor {
visitFieldDefinition(field: GraphQLField<any, TContext>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async (parent, argumentValues, context, info) => {
const coercionErrors: Error[] = [];
const coercedArgumentValues = await coerceFieldArgumentsValues(
field,
argumentValues,
context,
info,
e => coercionErrors.push(e)
);
if (coercionErrors.length > 0) {
throw new Error(`Arguments are incorrect: ${coercionErrors.join(',')}`);
}
return resolve(parent, coercedArgumentValues, context, info);
}
}
}
visitSchema(schema, new FieldResoverWrapperVisitor);
const query = `
mutation {
createBook(book: {
title: "sfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdjdjjdjddjdfsdfsdfsdfsdfdsfds"
}) {
title
}
}
`
graphql(schema, query).then(res => console.log(res));