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

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

Open
ksdc-vaughann opened this issue Sep 13, 2024 · 1 comment

Comments

@ksdc-vaughann
Copy link

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
Copy link
Collaborator

@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.

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

No branches or pull requests

2 participants