facebook / facebook/relay

[Feature Request]: automatic type resolvers from graphql tags

Open
#4,388 0 comments 8 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

AFAIK Typescript doesn't have the possibilities to `infer` the `TemplateStringsArray` that prevents automatic type resolving.

It seems like an hard issue for TypeScript to solve. The request has been around since 2019:
https://github.com/microsoft/TypeScript/issues/33304

I was thinking if we could create an alternative to:
```ts
type CurrentGraphQLFn = (texts: TemplateStringsArray): GraphQLTaggedNode;

graphql`
query something {
// ....
}
`
```

To something like this:
```ts
type GraphQLQuery = T extends keyof RelayPersistentQueries
? RelayPersistentQueries[T]
: GraphQLTaggedNode;

type FeatureRequestGraphQLFn = (query: T): GraphQLQuery;

// $graphql would be a new function from relay and keep the other one untouched for backward compatibility.
const query = $graphql(`
query something {
// ...
}
`)
```

If the compiler creates a type definition file with all queries like `Record` as the `RelayPersistentQueries` we would be able to automatically type queries.

This is good either way because if they manage to add the inferring feature to Typescript than we already have this functionality and would be easy to add to our `graphql` function as well. Then we only deprecate that `$graphql` function because I can agree that template literals looks better.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.