graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Prevent StrawberryShake client throwing ArgumentNullException when @include directive is false.

Open grounzero opened this issue 8 months ago • 0 comments

When an operation contains an @include(if: false) the generated code throws an ArgumentNullException at runtime.

  • https://github.com/ChilliCream/graphql-platform/issues/6616

In the GraphQL Api I tried extracting @include directives using a middleware like this in order to return something other than null. Though this only works when @include(if: true) since the directive itself is not included in this scenario.

It would be good to apply this conditionally based on whether a directive exists but it seems directives are ignored by the StarwberryShake.CodeGeneration.Analyzers

    internal static bool HasInclude(this IMiddlewareContext context)
    {
        foreach (var directiveNode in context.Selection.SyntaxNode.Directives)
        {
            if (string.Compare(directiveNode.Name.Value, "include", StringComparison.CurrentCultureIgnoreCase) == 0 && directiveNode.Kind == SyntaxKind.Directive)
            {
                foreach (var argumentNode in directiveNode.Arguments)
                {
                    if (argumentNode is { Name.Value: "if", Kind: SyntaxKind.BooleanValue, Value.Value: false })
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    }

grounzero avatar Jun 14 '24 16:06 grounzero