Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

grounzero
Copy link
Contributor

@grounzero grounzero commented Jun 14, 2024

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

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 grounzero changed the base branch from main-version-13 to main June 14, 2024 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant