Skip to content

False positive from unused variables validator when query variable is only used in object literal passed to @arguments #4932

Open

Description

@trey-miller

If I have a query like this:

query TestQuery($foo: String!) {
    ...TestFragment @arguments(input: { foo: $foo })
}

I get an error like: "Variable $foo is never used in operation TestQuery"

However if I use that variable in another fragment without the object literal, I get no error and it compiles happily.

query TestQuery($foo: String!) {
    ...TestFragment @arguments(input: { foo: $foo })
    ...TestTwoFragment @arguments(foo: $foo)
}

Is this a bug in the unused variables validator?

Activity

  1. tyao1 commented on Mar 7, 2025

    @tyao1
    Contributor

    I'm not sure if Relay support complex object in @arguments. A snapshot test in the compiler would be helpful for further debugging this.
    To take a step back, can TestFragment reference the global variable $foo directly?

  2. trey-miller commented on Mar 13, 2025

    @trey-miller
    Author

    Thanks. I don't have the Relay project running locally, but a snapshot graphql using the test schema might look like:

    fragment StorySearchFragment on User @argumentDefinitions(query: { type: "StorySearchInput!" }) {  
      storySearch(query: $query) {
        id
      }
    }
    
    query StorySearchQuery($userID: ID!, $text: String!) {
      node($userID) {
        ...StorySearchFragment @arguments(query: { text: $text, type: DIRECTED }) {
          id
        }
      }
    }

    (I didn't run the compiler against that, so hopefully it's proper syntax)

    You can see what I was trying to achieve which was to hardcode the type and only require the $text as a variable. The workaround is to just have StorySearchQuery argument be the full $query: StorySearchInput! object, but that requires the caller to have to specify the extra arguments that I want to hardcode into this query.

    To take a step back, can TestFragment reference the global variable $foo directly?

    Possibly, but that doesn't achieve my intent which is for the query to be using a specific type, but the fragment itself is not tied to that specific type, or any other hardcoded fields on the input object.

    If complex objects passed to arguments is not supported, then that's that, but in my original example, the second code snippet shows that the compiler passes if I also use the same variable in another simple argument. So I assume that it's only a special case that the unused variables validator does not account for, rather than a limitation in the compiler itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions