graphiql icon indicating copy to clipboard operation
graphiql copied to clipboard

Feature: Typescript generic argument on graphql template function breaks highlighting

Open Mike-Logit opened this issue 3 years ago • 4 comments

I noticed this (press play to view the GIF):

graphql with generic typescript

As you can see, highlighting is not supported when using a generic argument to add Typescript features. I can use #graphql to fix this but sometimes prettier will format it onto a new line and break it which isn't ideal.

I'd prefer to just have Typescript support using the generic argument if possible.

Thank you.

Mike-Logit avatar Jul 21 '22 12:07 Mike-Logit

@Mike-Logit we just added the regex for highlighting graphql( so this must be missing from that. Try this for now, which we know is working:

graphql<MyType>` 
  query myQuery{
  
  }
`

acao avatar Jul 21 '22 14:07 acao

This is where the fix would be implemented https://github.com/graphql/graphiql/blob/b5feda28c61848d62df2615d85fe417e5e355298/packages/vscode-graphql/grammars/graphql.js.json#L47

acao avatar Jul 24 '22 10:07 acao

@Mike-Logit Try this for now, which we know is working:

graphql<MyType>` 
  query myQuery{
  
  }
`

Thanks. This doesn't work for me because I believe the graphql function I'm using isn't typed to support that syntax. I'm using Gatsby.js so it might not be the same type of graphql function you're used to seeing:

Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'string'.ts(234

Mike-Logit avatar Jul 27 '22 11:07 Mike-Logit

However, I solved it another way by splitting the query string from where it was being passed into the function so prettier wouldn't think my line was too long and move the #graphql part to another line and break the highlighting:

const sourcesQuery = `#graphql
  query {
    allSourcesYaml {
      edges {
        node {
           ... more fields
        }
      }
    }
  }`;

const sourcesQueryResult = await graphql<Queries.Query>(sourcesQuery);

Mike-Logit avatar Jul 27 '22 11:07 Mike-Logit

Any update on this? I dislike having to refactor my code just to get the syntax highlighting to work.

DennisRosen avatar Dec 20 '22 08:12 DennisRosen

@DennisRosen @Mike-Logit please re-open if this is still an issue.

we have snapshot tests confirming this case now. enjoy!

acao avatar Mar 01 '24 20:03 acao

@acao Thank you all very much! I've just tested this out and yes it works perfectly. I've put the query back to where it's supposed to be now that I no longer need the solution I mentioned above using #graphql. Thanks for letting us know :)

Mike-Logit avatar Mar 21 '24 10:03 Mike-Logit