Directive Middleware
See original GitHub issueI need to validate fields before process query/mutation. For example, I need to check is email have correct format, and is password strong enough. Seems that is not possible for now, I can’t find any examples in documentation and source code.
I suggest to create generic method Validate
in ArgumentDescriptor
that accepts validation function. This function must return validation result. Validation result contains flag, that indicates validation success, and optionally contains error message, if validation was failed.
Here is an example of Validate
function usage:
descriptor
.Field(t => t.RegisterUser(default, default))
.Type<NonNullType<UserType>>()
.Argument("email", a => a
.Type<NonNullType<StringType>>())
.Validate<string>(x => ValidationUtility.IsValidEmail(x))
.Argument("password", a => a
.Type<NonNullType<StringType>>())
.Validate<string>(x => ValidationUtility.IsPasswordStrong(x));
Also, I think this method can have more wide application, we can create it in FieldDescriptor
and use for check user permissions on mutation or query.
Issue Analytics
- State:
- Created 5 years ago
- Comments:50 (50 by maintainers)
Top Results From Across the Web
Treating GraphQL directives as middleware
Directives are among GraphQL's most powerful features, but that's only if the GraphQL server provides a good support for them.
Read more >Directives - Apollo GraphQL Docs
A directive decorates part of a GraphQL schema or operation with additional configuration. Tools like Apollo Server (and Apollo Client) can read a...
Read more >Directives - Hot Chocolate - ChilliCream GraphQL Platform
Directives consist of a name and zero or more arguments. ... A directive middleware is only added to a field middleware pipeline when...
Read more >Directives | RedwoodJS Docs
You can think of directives like "middleware" that let you run reusable code during GraphQL execution to perform tasks like authentication and formatting....
Read more >Using schema directives to implement permission checks
Directives act a bit like annotations, decorators, or HTTP middleware. They give you a way to specify some behaviour based on a field...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I will close this issue and create follow up issues for interface support and so on…
Once we have implemented these for custom use you could use the descriptors in code-first and annotate these: