-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add backwards compatible generics support for fql statements #277
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type for Client.paginate()
needs a tweek. I've a change coming for this.
Also, do we want to include Client.stream()
in this PR, too?
ptpaterson is a contributor on the PR and resolved the open issues.
@@ -269,9 +270,11 @@ export class Client { | |||
|
|||
/** | |||
* Initialize a streaming request to Fauna | |||
* @typeParam T - The expected type of the response from Fauna. T can be inferred | |||
* if theprovided query used a type parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/s/theprovided/the provided/
// @ts-expect-error Argument of type 'Query<string | number>' is not assignable to parameter of type 'Query<string>'. | ||
await client.query<string>(query2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow cool assertion here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding how to use this to the development section of the README?
As we tweak type arguments in future work capturing that we can sanity check our types using this and proper linter settings will help out future devs.
For example, we got some work we'd like to do to make less boilerplate for users when definining their own types and when using .query
calls that return pages (as opposed to using the clients pagination utils - there's cases you might do that in an API for example). We have some of these type improvements documented internally here: https://faunadb.atlassian.net/wiki/spaces/DX/pages/3852697604/Developer+Tooling+Pain#API-Pagination
So this test pattern is something we will likely need aagain.
Just linking here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html with some text about the linter settings needed would suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I like the solution. Found one small doc typoe.
Ticket(s): FE-5389
Problem
Today, fql statements return a
Query
type. We'd like for fql statements to carry generic types that describe the resulting value once it's evaluated. This would allow users to "type" a fql statement once and have it be inferred in client methods. This would eliminate the need to type client call methods.We want to do this in a backwards compatible way.
Solution
By adding a type parameter that extends
QueryValue
to fql statements andQuery
, we can enable types to be inferred in client method calls with minimal changes. So something like this "just works":Result
The following PR adds support for generics in fql statements and
Query
in a backwards compatible way. The following usages work:Similarly, you can still type client calls directly:
Thanks to the efforts of @ptpaterson, users are protected from conflicting types when they attempt to type both fql and client methods. See the following test for this in action:
Out of scope
Query
is publicly exportedTesting
To test, the existing query-typing tests were expanded to demonstrate fql types.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.