How to validate the whole input object? #3397
-
Hi Reading about graphql validation and want to build custom directive to validate I've done the following: auth.directive.ts (Ignore the implementation since it is not the issue here) import { mapSchema, MapperKind } from "@graphql-tools/utils";
import { GraphQLSchema } from "graphql";
const validateDirective = (directiveName: string) => {
return {
validateDirectiveTypeDefs: `directive @${directiveName} on INPUT_OBJECT_FIELD`,
validateDirectiveTransformer: (schema: GraphQLSchema) =>
mapSchema(schema, {
[MapperKind.INPUT_OBJECT_FIELD]: (
fieldConfig,
_fieldName,
typeName
) => {
console.log(fieldConfig, _fieldName, typeName);
return fieldConfig;
},
}),
};
};
export const { validateDirectiveTypeDefs, validateDirectiveTransformer } =
validateDirective("validateAuthInput"); auth.typedef.ts import { gql } from "apollo-server-core";
import { validateDirectiveTypeDefs } from "./auth.directive";
const AUTH_TYPE = gql`
${validateDirectiveTypeDefs}
"""
Authentication input model
"""
input AuthInput @validateAuthInput {
email: String
password: String
}
"""
Authentication data model
"""
type Auth {
accessToken: String!
refreshToken: String!
userRole: String
}
`;
export default AUTH_TYPE; Schema import { gql } from "apollo-server-express";
import {
AuthResolvers,
AUTH_TYPE,
UserResolvers,
USER_TYPE,
} from "./resolvers";
import { validateDirectiveTransformer } from "./resolvers/auth/auth.directive";
import { makeExecutableSchema } from "@graphql-tools/schema";
const typeDefs = gql`
"""
\`ISO 8601\` date format. E.g: 2021-08-09T09:45:16.696Z
"""
scalar Date
${USER_TYPE}
${AUTH_TYPE}
type Message {
message: String
string: String @deprecated(reason: "Use message field instead.")
date: Date
}
type Query {
"""
Generate tokens.
"""
tokens(userData: AuthInput!): Auth
"""
Simple query for demonstration purposes.
"""
sayHi: Message
"""
Fetch all articles
"""
getAllUsers: [User]
}
# MUTATIONS
# type Mutation {}
`;
const executableSchema = makeExecutableSchema({
typeDefs,
resolvers: [UserResolvers, AuthResolvers],
});
const schemaWithAuthValidation = validateDirectiveTransformer(executableSchema);
export default schemaWithAuthValidation; I'm getting the following error error: GraphQLError [Object]: Syntax Error: Unexpected Name "INPUT_OBJECT_FIELD Any idea if this is possbile? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
It seems you are using directive Other than that, I'd use scalars for this kind of validation instead of directives which would be waay easier to implement and maintain. |
Beta Was this translation helpful? Give feedback.
-
@ardatan thank you for the rapid support.
I'm sure the issue is lack of information so hopefully you can lighten me. |
Beta Was this translation helpful? Give feedback.
-
Fair enough! @ardatan Thank you for your kind support. |
Beta Was this translation helpful? Give feedback.
-
See also #3337 (reply in thread) |
Beta Was this translation helpful? Give feedback.
It seems you are using directive
validateAuthInput
for input types not fields. So it should be something likeINPUT_OBJECT
. And this error seems related to GraphQLJS parser that says there is no identifier likeINPUT_OBJECT_FIELD
.Other than that, I'd use scalars for this kind of validation instead of directives which would be waay easier to implement and maintain.