WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c78be5e

Browse files
committed
feat: use dynamic validation rules
Closes #1
1 parent 3f6793a commit c78be5e

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

src/index.ts

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import type {
55
execute as graphqlExecute,
66
subscribe as graphqlSubscribe,
77
specifiedRules as graphqlSpecifiedRules,
8+
ExecutionArgs,
9+
ValidationRule,
10+
DocumentNode,
811
} from 'graphql';
912
import type {
1013
CreateRequestHandlerOptions,
@@ -88,38 +91,24 @@ const GraphQLSSEPlugin: PostGraphilePlugin = {
8891
},
8992
);
9093

94+
// some values necessary for dynamic validation are not available in `validate` callback
95+
const dynamicValidationRulesForDocument = new Map<
96+
DocumentNode,
97+
ValidationRule[]
98+
>();
99+
91100
handler = createHandler<IncomingMessage, ServerResponse>({
92101
execute,
93102
subscribe,
94103
validate(schema, document) {
95-
const validationErrors = validate(
96-
schema,
97-
document,
98-
staticValidationRules,
99-
);
100-
if (validationErrors.length) {
101-
return validationErrors;
104+
try {
105+
return validate(schema, document, [
106+
...staticValidationRules,
107+
...(dynamicValidationRulesForDocument.get(document) || []),
108+
]);
109+
} finally {
110+
dynamicValidationRulesForDocument.delete(document);
102111
}
103-
104-
// TODO: implement if necessary
105-
// // You are strongly encouraged to use
106-
// // `postgraphile:validationRules:static` if possible - you should
107-
// // only use this one if you need access to variables.
108-
// const moreValidationRules = pluginHook('postgraphile:validationRules', [], {
109-
// options,
110-
// req,
111-
// res,
112-
// variables: variableValues,
113-
// operationName: operationName,
114-
// });
115-
// if (moreValidationRules.length) {
116-
// const moreValidationErrors = validate(schema, document, moreValidationRules);
117-
// if (moreValidationErrors.length) {
118-
// return moreValidationErrors;
119-
// }
120-
// }
121-
122-
return [];
123112
},
124113
async onSubscribe(req, res, params) {
125114
const context = await withPostGraphileContextFromReqRes(
@@ -129,10 +118,8 @@ const GraphQLSSEPlugin: PostGraphilePlugin = {
129118
(context) => context,
130119
);
131120

132-
const schema = await getGraphQLSchema();
133-
134-
return {
135-
schema,
121+
const args: ExecutionArgs = {
122+
schema: await getGraphQLSchema(),
136123
contextValue: context,
137124
operationName: params.operationName,
138125
document:
@@ -141,6 +128,29 @@ const GraphQLSSEPlugin: PostGraphilePlugin = {
141128
: params.query,
142129
variableValues: params.variables,
143130
};
131+
132+
// You are strongly encouraged to use
133+
// `postgraphile:validationRules:static` if possible - you should
134+
// only use this one if you need access to variables.
135+
const dynamicValidationRules = pluginHook(
136+
'postgraphile:validationRules',
137+
[],
138+
{
139+
options,
140+
req,
141+
res,
142+
variables: args.variableValues,
143+
operationName: args.operationName,
144+
},
145+
);
146+
if (dynamicValidationRules.length) {
147+
dynamicValidationRulesForDocument.set(
148+
args.document,
149+
dynamicValidationRules,
150+
);
151+
}
152+
153+
return args;
144154
},
145155
async onNext(req, _args, result) {
146156
if (result.errors) {

0 commit comments

Comments
 (0)