Skip to content
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

fix(core/bindings): Fix conditional/optional variables type based on TypedDocumentNode generic #3022

Merged
merged 3 commits into from
Mar 9, 2023

Conversation

kitten
Copy link
Member

@kitten kitten commented Mar 9, 2023

Resolves #2668

Summary

Previously, we had a couple of cases that wouldn't allow us to correctly identify when the variables input should be optional, e.g. allowing for:

import { useQuery, TypedDocumentNode } from 'urql';
const x: TypedDocumentNode<{ data: null }, { test: string | null }> = {} as any;
useQuery({ query: x });

Additionally, it wasn't always easily possible to proxy generic inputs to our hooks and bindings, e.g. allowing for:

import { useQuery, TypedDocumentNode, AnyVariables } from 'urql';

export function wrapper<Data, Variables extends AnyVariables = AnyVariables>(
  query: TypedDocumentNode<Data, Variables>,
  variables: Variables,
) {
  return useQuery({ query, variables });
}

This is now resolved and to prevent future mistakes a GraphQLRequestParams utility type has been exposed by @urql/core which will be maintained/exposed as a public type, but is also reused in all of our bindings.

NOTE on patch: This is a patch and not a major/breaking change since we can never break types by making a property optional rather than required. Since we're a) widening the type with the union, and b) fixing cases where variables should be optional, this shouldn't break any TS types.

Set of changes

  • Add GraphQLRequestParams utility fixing optional variables
  • Update bindings to use GraphQLRequestParams where query, variables inputs are accepted

@kitten kitten requested a review from JoviDeCroock March 9, 2023 04:27
@kitten kitten merged commit aa183e2 into main Mar 9, 2023
@kitten kitten deleted the fix/ts-conditional-variables branch March 9, 2023 12:35
@github-actions github-actions bot mentioned this pull request Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QueryArgs variables type seems to be broken with TypedDocumentNode
2 participants