Examine the possibility to use type from queries without generation
See original GitHub issuename: 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:
- Created 3 years ago
- Reactions:9
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 exportTypedDocumentNode
types from generated files to make the type assertions more compact.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 calledTypedDocumentNode
from the package@graphql-typed-document-node/core
that is defined like this:It would be super helpful if the plugin could replace the inferred type of
mouseQuery
from the example above with this type: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!