ts-graphql-plugin icon indicating copy to clipboard operation
ts-graphql-plugin copied to clipboard

Add more customizable pattern for `tag`

Open aspirisen opened this issue 3 years ago • 2 comments

Thanks to TS new feature Template Literal Types it becomes possible to parse the query on type level, at least not complex one. However, it is not possible to get type of tagged literal because it is always TemplateStringsArray, but it is possible to use plain functions instead gql('query { data }'). So the correct types will be inferred in TS itself, and the autocomplete will be provided by this plugin.

Also, it would be good to add possibility to provide autocomplete in types just as bonus for edge cases, i.e. type Data = Query<'query { data }'>

It would be good to specify more complex pattern like start with gql(' and ends with ') for functions and to cover case with types you could set start with Query<' and ends with '>, or array of patterns. So we need just to specify the pattern which includes graphql content.

I think it could close this issues as well https://github.com/Quramy/ts-graphql-plugin/issues/117 https://github.com/Quramy/ts-graphql-plugin/issues/53

aspirisen avatar Apr 12 '21 20:04 aspirisen

I agree "adding more complex tag pattern" .

Because some GraphQL clients don't use template literal tag function.

For example, with octokit/graphql.js :

const { repository } = await graphql(
  `
    {
      repository(owner: "octokit", name: "graphql.js") {
        issues(last: 3) {
          edges {
            node {
              title
            }
          }
        }
      }
    }
  `,
  {
    headers: {
      authorization: `token secret123`,
    },
  }
);

But I will not care TypeScript Template Literal Types pattern (e.g. Query<"query { data }"> ).

But I will not use this in my plugin because I think lexing / parsing queries should be responsible for graphql-js . And recursive descent parser with string type literal can exceed TypeScript's recursive type limitation.

https://github.com/Quramy/ts-graphql-plugin/issues/117#issuecomment-704352869

Quramy avatar Apr 13 '21 05:04 Quramy

@Quramy about

But I will not care TypeScript Template Literal Types pattern (e.g. Query<"query { data }"> ).

I don't mean to actually infer type or whatever, just when you specify the pattern do not ignore ts types if they are matched by the pattern. I.e. here type Data = Query<'query { data }'> there is no some predefined Query type that cause some special behavior of ts-graphql-plugin. So as i understand the task of the plugin is match part of the file text, parse it as GraphQL and provide autocomplete.

So yes, i agree that this plugin should not provide some types inference out of the box, but delegate it to some other third party libraries. However, it would be good to have autocomplete in such cases

aspirisen avatar Apr 13 '21 08:04 aspirisen