@envelop/extended-validation 1.4.0 → 1.4.1-alpha-c145a12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/common.d.ts +1 -2
- package/index.js +14 -16
- package/index.mjs +16 -17
- package/package.json +1 -1
package/common.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { ASTVisitor, DirectiveNode, ExecutionArgs,
|
|
1
|
+
import { ASTVisitor, DirectiveNode, ExecutionArgs, ValidationContext } from 'graphql';
|
|
2
2
|
export declare type ExtendedValidationRule = (context: ValidationContext, executionArgs: ExecutionArgs) => ASTVisitor;
|
|
3
3
|
export declare function getDirectiveFromAstNode(astNode: {
|
|
4
4
|
directives?: ReadonlyArray<DirectiveNode>;
|
|
5
5
|
}, names: string | string[]): null | DirectiveNode;
|
|
6
|
-
export declare function unwrapType(type: GraphQLType): GraphQLNamedType;
|
package/index.js
CHANGED
|
@@ -78,12 +78,6 @@ function getDirectiveFromAstNode(astNode, names) {
|
|
|
78
78
|
const authDirective = directives.find(d => namesArr.includes(d.name.value));
|
|
79
79
|
return authDirective || null;
|
|
80
80
|
}
|
|
81
|
-
function unwrapType(type) {
|
|
82
|
-
if (graphql.isNonNullType(type) || graphql.isListType(type)) {
|
|
83
|
-
return unwrapType(type.ofType);
|
|
84
|
-
}
|
|
85
|
-
return type;
|
|
86
|
-
}
|
|
87
81
|
|
|
88
82
|
const ONE_OF_DIRECTIVE_SDL = /* GraphQL */ `
|
|
89
83
|
directive @oneOf on INPUT_OBJECT | FIELD_DEFINITION
|
|
@@ -98,12 +92,10 @@ const OneOfInputObjectsRule = (validationContext, executionArgs) => {
|
|
|
98
92
|
return;
|
|
99
93
|
}
|
|
100
94
|
const values = utils.getArgumentValues(fieldType, node, executionArgs.variableValues || undefined);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
validationContext.reportError(new graphql.GraphQLError(`Exactly one key must be specified for input for field "${fieldType.type.toString()}.${node.name.value}"`, [node]));
|
|
106
|
-
}
|
|
95
|
+
const isOneOfFieldType = ((_b = fieldType.extensions) === null || _b === void 0 ? void 0 : _b.oneOf) || (fieldType.astNode && getDirectiveFromAstNode(fieldType.astNode, 'oneOf'));
|
|
96
|
+
if (isOneOfFieldType) {
|
|
97
|
+
if (Object.keys(values).length !== 1) {
|
|
98
|
+
validationContext.reportError(new graphql.GraphQLError(`Exactly one key must be specified for input for field "${fieldType.type.toString()}.${node.name.value}"`, [node]));
|
|
107
99
|
}
|
|
108
100
|
}
|
|
109
101
|
for (const arg of node.arguments) {
|
|
@@ -116,6 +108,12 @@ const OneOfInputObjectsRule = (validationContext, executionArgs) => {
|
|
|
116
108
|
},
|
|
117
109
|
};
|
|
118
110
|
};
|
|
111
|
+
function getNonNullType(ttype) {
|
|
112
|
+
if (ttype instanceof graphql.GraphQLNonNull) {
|
|
113
|
+
return ttype.ofType;
|
|
114
|
+
}
|
|
115
|
+
return ttype;
|
|
116
|
+
}
|
|
119
117
|
function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
120
118
|
var _a;
|
|
121
119
|
// if the current value is empty we don't need to traverse deeper
|
|
@@ -123,13 +121,14 @@ function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
|
123
121
|
if (currentValue == null) {
|
|
124
122
|
return;
|
|
125
123
|
}
|
|
126
|
-
|
|
124
|
+
const unwrappedType = getNonNullType(graphqlType);
|
|
125
|
+
if (graphql.isListType(unwrappedType)) {
|
|
127
126
|
if (!Array.isArray(currentValue)) {
|
|
128
127
|
// because of graphql type coercion a single object should be treated as an array of one object
|
|
129
128
|
currentValue = [currentValue];
|
|
130
129
|
}
|
|
131
130
|
currentValue.forEach(value => {
|
|
132
|
-
traverseVariables(validationContext, arg,
|
|
131
|
+
traverseVariables(validationContext, arg, unwrappedType.ofType, value);
|
|
133
132
|
});
|
|
134
133
|
return;
|
|
135
134
|
}
|
|
@@ -137,7 +136,7 @@ function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
|
137
136
|
// in case the value is not an object, the "original" validation phase should complain.
|
|
138
137
|
return;
|
|
139
138
|
}
|
|
140
|
-
const inputType =
|
|
139
|
+
const inputType = graphql.getNamedType(graphqlType);
|
|
141
140
|
const isOneOfInputType = ((_a = inputType.extensions) === null || _a === void 0 ? void 0 : _a.oneOf) || (inputType.astNode && getDirectiveFromAstNode(inputType.astNode, 'oneOf'));
|
|
142
141
|
if (isOneOfInputType) {
|
|
143
142
|
if (Object.keys(currentValue).length !== 1) {
|
|
@@ -154,5 +153,4 @@ function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
|
154
153
|
exports.ONE_OF_DIRECTIVE_SDL = ONE_OF_DIRECTIVE_SDL;
|
|
155
154
|
exports.OneOfInputObjectsRule = OneOfInputObjectsRule;
|
|
156
155
|
exports.getDirectiveFromAstNode = getDirectiveFromAstNode;
|
|
157
|
-
exports.unwrapType = unwrapType;
|
|
158
156
|
exports.useExtendedValidation = useExtendedValidation;
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypeInfo, ValidationContext, visitInParallel, visit, visitWithTypeInfo,
|
|
1
|
+
import { TypeInfo, ValidationContext, visitInParallel, visit, visitWithTypeInfo, GraphQLError, isListType, getNamedType, GraphQLInputObjectType, GraphQLNonNull } from 'graphql';
|
|
2
2
|
import { getArgumentValues } from '@graphql-tools/utils';
|
|
3
3
|
|
|
4
4
|
const symbolExtendedValidationRules = Symbol('extendedValidationContext');
|
|
@@ -74,12 +74,6 @@ function getDirectiveFromAstNode(astNode, names) {
|
|
|
74
74
|
const authDirective = directives.find(d => namesArr.includes(d.name.value));
|
|
75
75
|
return authDirective || null;
|
|
76
76
|
}
|
|
77
|
-
function unwrapType(type) {
|
|
78
|
-
if (isNonNullType(type) || isListType(type)) {
|
|
79
|
-
return unwrapType(type.ofType);
|
|
80
|
-
}
|
|
81
|
-
return type;
|
|
82
|
-
}
|
|
83
77
|
|
|
84
78
|
const ONE_OF_DIRECTIVE_SDL = /* GraphQL */ `
|
|
85
79
|
directive @oneOf on INPUT_OBJECT | FIELD_DEFINITION
|
|
@@ -94,12 +88,10 @@ const OneOfInputObjectsRule = (validationContext, executionArgs) => {
|
|
|
94
88
|
return;
|
|
95
89
|
}
|
|
96
90
|
const values = getArgumentValues(fieldType, node, executionArgs.variableValues || undefined);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
validationContext.reportError(new GraphQLError(`Exactly one key must be specified for input for field "${fieldType.type.toString()}.${node.name.value}"`, [node]));
|
|
102
|
-
}
|
|
91
|
+
const isOneOfFieldType = ((_b = fieldType.extensions) === null || _b === void 0 ? void 0 : _b.oneOf) || (fieldType.astNode && getDirectiveFromAstNode(fieldType.astNode, 'oneOf'));
|
|
92
|
+
if (isOneOfFieldType) {
|
|
93
|
+
if (Object.keys(values).length !== 1) {
|
|
94
|
+
validationContext.reportError(new GraphQLError(`Exactly one key must be specified for input for field "${fieldType.type.toString()}.${node.name.value}"`, [node]));
|
|
103
95
|
}
|
|
104
96
|
}
|
|
105
97
|
for (const arg of node.arguments) {
|
|
@@ -112,6 +104,12 @@ const OneOfInputObjectsRule = (validationContext, executionArgs) => {
|
|
|
112
104
|
},
|
|
113
105
|
};
|
|
114
106
|
};
|
|
107
|
+
function getNonNullType(ttype) {
|
|
108
|
+
if (ttype instanceof GraphQLNonNull) {
|
|
109
|
+
return ttype.ofType;
|
|
110
|
+
}
|
|
111
|
+
return ttype;
|
|
112
|
+
}
|
|
115
113
|
function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
116
114
|
var _a;
|
|
117
115
|
// if the current value is empty we don't need to traverse deeper
|
|
@@ -119,13 +117,14 @@ function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
|
119
117
|
if (currentValue == null) {
|
|
120
118
|
return;
|
|
121
119
|
}
|
|
122
|
-
|
|
120
|
+
const unwrappedType = getNonNullType(graphqlType);
|
|
121
|
+
if (isListType(unwrappedType)) {
|
|
123
122
|
if (!Array.isArray(currentValue)) {
|
|
124
123
|
// because of graphql type coercion a single object should be treated as an array of one object
|
|
125
124
|
currentValue = [currentValue];
|
|
126
125
|
}
|
|
127
126
|
currentValue.forEach(value => {
|
|
128
|
-
traverseVariables(validationContext, arg,
|
|
127
|
+
traverseVariables(validationContext, arg, unwrappedType.ofType, value);
|
|
129
128
|
});
|
|
130
129
|
return;
|
|
131
130
|
}
|
|
@@ -133,7 +132,7 @@ function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
|
133
132
|
// in case the value is not an object, the "original" validation phase should complain.
|
|
134
133
|
return;
|
|
135
134
|
}
|
|
136
|
-
const inputType =
|
|
135
|
+
const inputType = getNamedType(graphqlType);
|
|
137
136
|
const isOneOfInputType = ((_a = inputType.extensions) === null || _a === void 0 ? void 0 : _a.oneOf) || (inputType.astNode && getDirectiveFromAstNode(inputType.astNode, 'oneOf'));
|
|
138
137
|
if (isOneOfInputType) {
|
|
139
138
|
if (Object.keys(currentValue).length !== 1) {
|
|
@@ -147,4 +146,4 @@ function traverseVariables(validationContext, arg, graphqlType, currentValue) {
|
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
148
|
|
|
150
|
-
export { ONE_OF_DIRECTIVE_SDL, OneOfInputObjectsRule, getDirectiveFromAstNode,
|
|
149
|
+
export { ONE_OF_DIRECTIVE_SDL, OneOfInputObjectsRule, getDirectiveFromAstNode, useExtendedValidation };
|