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

Feature: Extend GraphQL Client Package Types #1927

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-scissors-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/graphql-client': minor
---

Extends GraphQL Client Types for better DevX
41 changes: 32 additions & 9 deletions packages/api-clients/graphql-client/src/graphql-client/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface CustomRequestInit {
method?: string;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
headers?: HeadersInit;
body?: string;
signal?: AbortSignal;
Expand All @@ -10,7 +10,7 @@ export type CustomFetchApi = (
init?: CustomRequestInit,
) => Promise<Response>;

type OperationVariables = Record<string, any>;
type OperationVariables<TVariables = Record<string, any>> = TVariables;

export type DataChunk = Buffer | Uint8Array;

Expand All @@ -21,11 +21,28 @@ export type {HeadersObject as Headers};
export interface ResponseErrors {
networkStatusCode?: number;
message?: string;
graphQLErrors?: any[];
graphQLErrors?: {
message: string;
extensions?: GQLExtensions;
}[];
response?: Response;
}

export type GQLExtensions = Record<string, any>;
export interface GQLExtensions {
cost?: {
requestedQueryCost: number;
actualQueryCost: number;
throttleStatus: {
maximumAvailable: number;
currentlyAvailable: number;
restoreRate: number;
};
code?: string;
maxCost?: number;
documentation?: string;
[key: string]: unknown;
};
}

export interface FetchResponseBody<TData = any> {
data?: TData;
Expand All @@ -37,6 +54,12 @@ export interface ClientResponse<TData = any> extends FetchResponseBody<TData> {
errors?: ResponseErrors;
}

export type SuccessClientResponse<TData = any> = Omit<
ClientResponse<TData>,
'errors'
>;
export type ErrorClientResponse = Omit<ClientResponse, 'data' | 'extensions'>;

export interface ClientStreamResponse<TData = any>
extends ClientResponse<TData> {
hasNext: boolean;
Expand Down Expand Up @@ -90,11 +113,11 @@ export interface ClientConfig {
}

export interface RequestOptions {
variables?: OperationVariables;
url?: string;
headers?: HeadersObject;
retries?: number;
signal?: AbortSignal;
readonly variables?: OperationVariables;
readonly url?: string;
readonly headers?: HeadersObject;
readonly retries?: number;
readonly signal?: AbortSignal;
}

export type RequestParams = [operation: string, options?: RequestOptions];
Expand Down
Loading