graphql-request
graphql-request copied to clipboard
Typescript error after updating typescript to 4.7.2
const abortController = new AbortController(); // the dom one, not the npm one
client.request({
// ...
signal: abortController.signal,
});
Type 'AbortSignal' is not assignable to type 'AbortSignal'. Two different types with this name exist, but they are unrelated.
Types of property 'onabort' are incompatible.
Type '((this: AbortSignal, ev: Event) => any) | null' is not assignable to type '((this: AbortSignal, ev: Event) => any) | null'. Two different types with this name exist, but they are unrelated.
Type '(this: AbortSignal, ev: Event) => any' is not assignable to type '(this: AbortSignal, ev: Event) => any'. Two different types with this name exist, but they are unrelated.
The 'this' types of each signature are incompatible.
Type 'AbortSignal' is missing the following properties from type 'AbortSignal': reason, throwIfAborted
node_modules/graphql-request/dist/types.d.ts:47:5
47 signal?: Dom.RequestInit['signal'];
~~~~~~
The expected type comes from property 'signal' which is declared here on type 'RequestOptions<Exact<{ /* ... */ }>>'
I managed to work around the issue:
import { RequestInit } from "graphql-request/dist/types.dom";
client.request({
// ...
signal: abortController.signal as NonNullable<RequestInit["signal"]>,
});
I don't know why the lib had to redefine its own AbortSignal
but it feels like an anti-pattern. We should rely on the standard object otherwise it makes the usage weird and brittle. When typing as you suggested above, it doesn't accept signal from global AbortController
and you have to explicitly cast it.
It's a bug that this library ships with standard DOM types (from Typescript). It shouldn't import them explicitly as then those end up in compiled bundle. Or it should use import type
statement but I think it should just rely on those being available without importing.
It does that on purpose but I'm not convinced it's a proper solution:
https://github.com/prisma-labs/graphql-request/blob/2f221a4c5470c22dad7749b53cb1ed560727fd42/src/types.dom.ts#L1-L8
Any news on this ? Casting doesn't seems like the "proper solution" even tho it works..
Currently working on a MR that should fix this issue (#411)
closed by https://github.com/jasonkuhrt/graphql-request/commit/702ef92ae382455ede353ac34300b25d31749e1c