question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Directive Middleware

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:50 (50 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelstaibcommented, Oct 9, 2018

I will close this issue and create follow up issues for interface support and so on…

1reaction
michaelstaibcommented, Jul 22, 2018

Once we have implemented these for custom use you could use the descriptors in code-first and annotate these:

public static class Extensions
    {
        public static IObjectFieldDescriptor AllowUnauthorized(
            this IObjectFieldDescriptor descriptor)
        {
            descriptor.Directive(new AuthDirective());
            return descriptor;
        }
    }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found