jparise / vim-graphql

A Vim plugin that provides GraphQL file detection, syntax highlighting, and indentation.
MIT License
487 stars 25 forks source link

Support for magic comments or functions #96

Closed Axxxx0n closed 1 month ago

Axxxx0n commented 1 year ago

New graphql codegen client preset is not using template strings for graphql queries. It uses graphql() function with string arguments. Using this we don't get syntax highlighting.

const query = graphql(`
    query ExampleQuery {
        example {
           id
        }
    }
`)

Possible solutions:

  1. Similar to g:graphql_javascript_tags option we could use something as g:graphql_javascript_function=["graphql", "gql"] and mark the string inside function for syntax highlighting

  2. Use magic comment as many graphql tools and extensions do:

const query = graphql(/* GraphQL */`
    query ExampleQuery {
        example {
           id
        }
    }
`)
  1. Add prefixes and document let g:graphql_javascript_tags = ["gql", "graphql", "Relay.QL", "gql(", "graphql("]

    Vim Version

NVIM v0.8.3
Build type: Release
LuaJIT 2.1.0-beta3
jparise commented 1 year ago

I think the g:graphql_javascript_functions = ["graphql"] approach would be the best way to handle this.

I just want to be sensitive to the number of optimistic matches we apply to JavaScript buffers in attempts to find embedded GraphQL strings.

jparise commented 10 months ago

We also already support a "magic" comment within the GraphQL template string:

const query = graphql(`# graphql
    query ExampleQuery {
        example {
           id
        }
    }
`)

I'm going to close this because I think it's a sufficiently good solution that doesn't involve additional code and runtime pattern matching, but please reopen if anyone feels otherwise.

felamaslen commented 4 months ago

We also already support a "magic" comment within the GraphQL template string

This works as long as everybody on your team uses this plugin and/or is OK with updating documents to add this spurious comment.

What if you contribute to a project where you don't have the option of making these refactors?

dickfickling commented 2 months ago

@jparise would you be open to a PR adding g:graphql_javascript_functions = ["graphql"]? Adding the comment is suboptimal for the reasons @felamaslen gave above. i'd be happy to contribute to the implementation, just don't want to waste my time if you don't want it.

jparise commented 2 months ago

Yes, I'm happy to review a change that implements something like that. I think if the default is just ["graphql"] that it shouldn't introduce much overhead beyond the current implementation.

I somewhat regret that g:graphql_javascript_tags defaults to all of ['gql', 'graphql', 'Relay.QL'] because it makes the pattern match slightly more expensive for everyone.

jparise commented 1 month ago

@dickfickling @felamaslen I added initial support for the graqhql() function in #109. If you have a chance to check it out, I'd be glad to know if it addressed your use cases.

dickfickling commented 1 month ago

Looks good! I saw an error in the vim log related to vim-graphql once right after updating the plugin, but I skipped it and haven't seen it since. I'll report back here if I see it again!

jparise commented 1 month ago

Looks good! I saw an error in the vim log related to vim-graphql once right after updating the plugin, but I skipped it and haven't seen it since. I'll report back here if I see it again!

Thanks! If it flashes by again, you should be able to see it in :messages.