Hotchocolate: How to modify field resolver result from directive

Created on 23 Nov 2018  路  6Comments  路  Source: ChilliCream/hotchocolate

Hi!

I'm trying to implement UpperCase directive using HotChocolate

Here is example how it could be implemented in ApolloServer: https://www.apollographql.com/docs/graphql-tools/schema-directives.html#Uppercasing-strings

I feel that it could be done in Directive Middleware but can't figured it out

bug

Most helpful comment

So, that is a bug.... but you can use a workaround.

You can also shortcircut the middleware and force a resolve like the following:

descriptor.Middleware(next => async context =>
{
    context.Result = (await context.ResolveAsync<string>()).ToUpperInvariant();
});

But, it should have worked like you did it. I will issue a fix tonight.

All 6 comments

That's how I try to solve this task:

public class UpperCaseDirectiveType : DirectiveType<UpperCaseDirective> 
{
    protected override void Configure(IDirectiveTypeDescriptor<UpperCaseDirective> descriptor)
    {
        descriptor
            .Name("uppercase")
            .Location(DirectiveLocation.Field | DirectiveLocation.FieldDefinition)
            .Middleware(next => context => Do(context, next));
    }

    private static async Task Do(IDirectiveContext context, DirectiveDelegate next)
    {
        await next(context);

        var result = context.Result as string;
        if (result != null)
        {
            result = result.ToUpper();
            context.Result = result;
        }
    }
}

but after calling await next(context) I get context.Result = null.

I think it's key point to problem

The same behavior I have in StarWars example from https://github.com/ChilliCream/hotchocolate-aspnetcore

If I add required claims and query

{
  search(text: "Han") {
    __typename
  }
}

I'm getting null as result

Looks like it's related to my issue

It should work.... just give me a second, I will try it with the aspnet example.

So, that is a bug.... but you can use a workaround.

You can also shortcircut the middleware and force a resolve like the following:

descriptor.Middleware(next => async context =>
{
    context.Result = (await context.ResolveAsync<string>()).ToUpperInvariant();
});

But, it should have worked like you did it. I will issue a fix tonight.

Thank you for quick response, I'll wait for fix.

Fixed with #358

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nigel-sampson picture nigel-sampson  路  5Comments

tunurgitr picture tunurgitr  路  4Comments

marcin-janiak picture marcin-janiak  路  4Comments

benmccallum picture benmccallum  路  5Comments

sascha-andres picture sascha-andres  路  4Comments