-
-
Notifications
You must be signed in to change notification settings - Fork 455
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
RFC: Graphcache strong typing #3538
Comments
This is definitely something we intend to include in So, if we write up a version that's targeting the current output in There's also some old issues to revisit with the typings and how they're being consumed by Graphcache, but that's likely TBD until we explore this again. But yea, I'd currently favour absorbing the complexity here into a |
Including it in gql.tada would definitely be a step in the right direction. |
I figured I'd add what I've been patching into my current project as it might be useful in further implementing this feature. When using Cache Normalizationimport type { DataField, Entity, FieldArgs } from '@urql/exchange-graphcache';
/**
* This utility type represents a reference returned from `Cache.resolve`,
* typically looking like `Book:123` but with the full type being referenced
* stored alongside it in branded-type fashion
*/
type Reference<T> = string & { __def: T };
/**
* This utility type is used to "normalize" an object type and return it
* as either a reference type or a scalar.
*/
type Normalize<T> =
T extends Array<infer U>
? Array<Normalize<U>>
: T extends object // Check if T can be normalized
? Reference<T> // If it can, return the normalized reference type
: T; // If it doesn't, return T
// Example
type Book = {
__typename: 'Book';
id: string;
title: string;
publicationDate: string;
author: {
__typename: 'Author';
id: string;
name: string;
};
};
type BookReference = Reference<Book>;
// ▲ string & { __def: Book; } Helpers/**
* This helper casts any `Entity` to a reference type, essentially prepping it
* for use in `Cache.resolve` without actually modifying its underlying type.
*/
const toReference = <T>(entity: Entity): Reference<T> => entity as Reference<T>;
/**
* This utility type is used to resolve a reference type to its full type
*/
type Resolved<T extends Reference<any>> = T extends Reference<infer U> ? U : T;
|
Summary
Currently all the operations on the graphcache are completely untyped. There is a old graphql-codegen project to support typing but that is apparently unmaintained: #2323
Taking the lessons learned from https://gql-tada.0no.co/ it should be possible to add typings to graphcache operations when an introspection schema is provided.
Proposed Solution
I have already played around and come up with a proof of concept that works using the instrospection created by 0no-co/graphqlsp
The text was updated successfully, but these errors were encountered: