Comments (6)
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.
from ts-graphql-plugin.
hi, thanks for reporting issue.
It’s not possible. The <mouseQuery>
is wrong TypeScript generics syntax because mouseQuery is not a type but value.
And with <typeof mouseQuery>
form, which has no compilation error, TypeFromQuery
wouldn’t work. We need to parse body of query to get return type of the query. But TypeScript type system does not allow this.
from ts-graphql-plugin.
Hi, that was not really an issue 😄
And there is no shenanigans a typescript plugin can do about this?
Having the type from the query felt so reachable!
PS: Yeah, used wrong syntax sorry, was wavering between a generic and a function and mixed both --'
from ts-graphql-plugin.
Hey @Quramy,
Seems like it will be soon doable: https://github.com/dotansimha/graphql-typed-ast (using an alpha TS version)
How do you feel about it, is that functionality something you would like for this plugin?
Cheers
from ts-graphql-plugin.
How do you feel about it, is that functionality something you would like for this plugin?
Template literal types is very interesting feature. 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.
from ts-graphql-plugin.
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!
from ts-graphql-plugin.
Related Issues (20)
- "No GraphQL schema" error (GitHub API)
- Document how to import graphql files (`typed-query-document`)
- Is it possible to use it in tsconfig.json with ttypescript?
- ts-graphql-plugin does not understand directive @connection (relay) HOT 3
- [typegen] Nullable params should be optional HOT 2
- List of list support HOT 2
- Empty variables, consider using `never` type? HOT 2
- Allow to customize type generation
- RangeError: Maximum call stack size exceeded HOT 3
- Action Required: Fix Renovate Configuration
- Feature Request: Cleanup generated files HOT 1
- External/Global fragment sources HOT 3
- Add more customizable pattern for `tag` HOT 2
- cli extract can't identify fragment which export by default.
- Dependency Dashboard
- graphql rename causes problems for webpack parser (Error: Cannot find module 'graphql/validation/rules/ExecutableDefinitions') HOT 3
- Typescript 4.8.2 breaking changes HOT 2
- Ignore/disable/turn off rules (for a certain line?)
- should depend on/use graphql-language-service instead of graphql-language-service-interface (deprecated) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-graphql-plugin.