JetBrains / js-graphql-intellij-plugin

GraphQL language support for WebStorm, IntelliJ IDEA and other IDEs based on the IntelliJ Platform.
https://jimkyndemeyer.github.io/js-graphql-intellij-plugin/
MIT License
880 stars 97 forks source link

Intellij Idea graphql plugin tagged template literals intellisense cannot resolve String.formatted() variables #723

Closed ksdc-vaughann closed 2 weeks ago

ksdc-vaughann commented 1 month ago

Describe the bug When using a text block (annotated as GraphQL) as a template for a GraphQL mutation, intellisense flags unquoted string variables (like %s & %d) as unresolved values.

To Reproduce Create the following text block in Java with the IntelliJ GraphQL plugin installed: ` java

    String lukeName = "Luke Skywalker";
    String LeiaName = "Leia Organa";
    // language=GraphQL
    String document2 = """
            mutation {
                addHero(hero: {
                    name: "%s",
                    born: %s,
                    damage: %d
                }) {
                    name
                    born
                    age
                    damage
                }
            }
            """;
    String lukeMutation = document2.formatted(lukeName, LocalDate.of(1979, 12, 1), 85.5);
    String LeiaMutation = document2.formatted(LeiaName, LocalDate.of(1979, 12, 1), 90.75);

`

GQL Schema

input HeroInput { name: String, born: LocalDate, damage: Float }

type Hero { name: String, born: LocalDate, age: Int, damage: Float }

Expected behavior The expectation is that there should not be an inspection warning of " expected, got '%' Unknown field "d" on input type "HeroInput"

Version and Environment Details Operation system: Windows 11 Build 22631.4169 IDE name and version: IntelliJ IDEA 2024.2.1 (Ultimate Edition) Plugin version: 242.21829.3

vepanimas commented 1 month ago

@ksdc-vaughann hi! Thank you for bringing up the issue. Unfortunately, I don't think it will ever be fixed. GraphQL parser is not and can't be aware of any string-building methods in every possible language. For instance, Java also has MessageFormat API, e.g.

MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     planet, new Date(), event);

Should the GraphQL parser be able to handle that too?

It’s generally much better to use GraphQL variables for this purpose, and that’s how most users approach it. There are several discussions on StackOverflow that cover this topic. For example, this answer mentions:

If your query is dynamically generated, there's no way to do static analysis on it using tools like eslint-plugin-graphql, so you can't check if your query is valid without actually running your code.

This is one of the key reasons for using variables instead of interpolation - and it’s likely the issue you’re currently encountering.