question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Examine the possibility to use type from queries without generation

See original GitHub issue

name: Feature request about: Examine the possibility to use type from queries without generation title: ‘Create a function/generic to return the expected result of a query’ labels: ‘question, feature request’ assignees: ‘’

Describe the feature Since typescript knows, thanks to this plugin, what are the expected key name and type of any query, could it be possible to have some sort of function or generic that would return the data from a query, without having to generate them?

It would replace:

const mouseQuery = gql`
query Mouse($mouseId: String!) {
  mouse(mouseId: $mouseId) {
    name
    bio {
      full
    }
  }
}
`
// generated types
type Mouse = {
    mouse: ({
        name: string;
        bio: ({
            full: string;
        }) | null;
    }) | null;
};
type MouseVariables = {
    mouseId: string;
};

with

// names may vary
import {
    TypeFromQuery,
    VariableTypeFromQuery
} from 'ts-graphql-plugin';

const mouseQuery = gql`
query Mouse($mouseId: String!) {
  mouse(mouseId: $mouseId) {
    name
    bio {
      full
    }
  }
}
`
type Mouse = TypeFromQuery<mouseQuery>;
type MouseVariables = VariableTypeFromQuery<mouseQuery>;

Since we can have auto-completion for queries and query type generation, what would we need to make this happen?

Thanks a lot for this plugin, love it!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hallettjcommented, Oct 30, 2020

So I’ve learned that, no, TypeScript plugins cannot produce the effect of adding a type assertion to an expression. So instead, inspired by @ts-gql I made an ESLint plugin with an autofix that automatically appends type assertions to gql template tag expressions.

The plugin: @hallettj/eslint-plugin-ts-graphql

The autofix avoids the step of importing generated types myself. The @hallettj scope on the name is probably temporary; I may move the package to a more permanent name in the near future. I’m also planning a pull request for this repo to export TypedDocumentNode types from generated files to make the type assertions more compact.

0reactions
hallettjcommented, Oct 19, 2020

Is it possible for the plugin to override the type of a tagged template expression? Or to insert a type assertion? @apollo/client (and possibly other libraries) can consume a generic type called TypedDocumentNode from the package @graphql-typed-document-node/core that is defined like this:

import { DocumentNode } from 'graphql';
export interface TypedDocumentNode<Result = {
    [key: string]: any;
}, Variables = {
    [key: string]: any;
}> extends DocumentNode {
    __resultType?: Result;
    __variablesType?: Variables;
}

It would be super helpful if the plugin could replace the inferred type of mouseQuery from the example above with this type:

import { TypedDocumentNode } from "@graphql-typed-document-node/core"

const mouseQuery = gql`
  query Mouse($mouseId: String!) {
    mouse(mouseId: $mouseId) {
      name
      bio {
        full
      }
    }
  }
` as TypedDocumentNode<
  {
    mouse: {
      name: string
      bio: {
        full: string
      } | null
    } | null
  },
  {
    mouseId: string
  }
>

Passing a value with that type to, for example, useQuery would automatically hook up the correct type inference for response data, and for query variables.

Edit: I realized an as would probably be better than adding a type annotation higher in the tree.

By the way, this plugin makes me very happy! I love it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to queries - Microsoft Support
Using a query makes it easier to view, add, delete, or change data in your Access database. ... Action queries are not available...
Read more >
What is a database query? - TechTarget
A query gives meaning to the lines of code used in every query language. As such, both the user and the database exchange...
Read more >
Chapter 4. Query Performance Optimization - O'Reilly
In the previous chapter, we explained how to optimize a schema, which is one of the necessary conditions for high performance.
Read more >
MySQL 8.0 Reference Manual :: 8.8.2 EXPLAIN Output Format
If key is NULL , MySQL found no index to use for executing the query more ... Other than the system and const...
Read more >
Learn SQL: Create a report manually using SQL queries
As always, we'll have to take a look at the data model we're using. If you're a data analyst, some of the expected...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found