Skip to content

Commit

Permalink
Add type definitions for graphqurl by @hasura
Browse files Browse the repository at this point in the history
Type definitions by Peter Boyer as per hasura/graphqurl#55
  • Loading branch information
josephcagle committed Nov 15, 2021
1 parent 7c69555 commit 23395cf
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
17 changes: 17 additions & 0 deletions types/graphqurl/graphqurl-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createClient } from 'graphqurl';

// $ExpectType Client
createClient({
endpoint: 'https://my-graphql-endpoint/graphql',
headers: {
Authorization: 'Bearer <token>'
},
websocket: {
endpoint: 'https://my-graphql-endpoint/graphql',
shouldRetry: false,
parameters: { someData: 'abc123' },
onConnectionSuccess: () => { },
onConnectionError: err => { },
onConnectionKeepAlive: () => { }
}
});
132 changes: 132 additions & 0 deletions types/graphqurl/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Type definitions for graphqurl 1.0
// Project: https://github.com/hasura/graphqurl
// Definitions by: Peter Boyer <https://github.com/ptboyer/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

export function createClient(options: Options): Client;

export interface Client {
query: (
queryOptions: QueryOptions,
successCallback?: (response: Response) => void,
errorCallback?: (error: any) => void,
) => Promise<Response>;
subscribe: (
subscriptionOptions: SubscriptionOptions,
eventCallback?: (response: Response) => void,
errorCallback?: (error: any) => void,
) => Disposer;
}

export interface Options {
/**
* GraphQL endpoint
*/
endpoint: string;

/**
* Request header, defaults to {}. These headers will be added along with
* all the GraphQL queries, mutations and subscriptions made through the
* client.
*/
headers?: Record<string, string>;

/**
* Options for configuring subscriptions over websocket. Subscriptions are
* not supported if this field is empty.
*/
websocket?: {
/**
* WebSocket endpoint to run GraphQL subscriptions.
*/
endpoint?: string;

/**
* Boolean value whether to retry closed websocket connection. Defaults to
* false.
*/
shouldRetry?: boolean;

/**
* Payload to send the connection init message with
*/
parameters?: Record<string, any>;

/**
* Callback function called when the GraphQL connection is successful.
* Please not that this is different from the websocket connection being
* open.
*/
onConnectionSuccess?: () => void;

/**
* Callback function called if the GraphQL connection over websocket is
* unsuccessful
*/
onConnectionError?: (error: any) => void;

/**
* Callback function called when the GraphQL server sends
* GRAPHQL_CONNECTION_KEEP_ALIVE messages to keep the connection alive.
*/
onConnectionKeepAlive?: () => void;
};
}

export interface QueryOptions {
/**
* The GraphQL query or mutation to be executed over HTTP
*/
query: string;

/**
* GraphQL query variables. Defaults to {}
*/
variables?: Record<string, any>;

/**
* Header overrides. If you wish to make a GraphQL query while adding to or
* overriding the headers provided during initalisations, you can pass the
* headers here.
*/
headers?: Record<string, string>;
}

export interface SubscriptionOptions {
/**
* The GraphQL subscription to be started over WebSocket
*/
subscription: string;

/**
* GraphQL query variables. Defaults to {}
*/
variables?: Record<string, any>;

/**
* You can optionally pass this function as an event callback
*/
onGraphQLData?: (response: Response) => void;

/**
* You can optionally pass this function as an error callback
*/
onGraphQLError?: (error: any) => void;

/**
* Callback function called when the GraphQL subscription is declared as
* complete by the server and no more events will be received
*/
onGraphQLComplete?: () => void;
}

export interface Response {
data: any;
loading: boolean;
networkStatus: number;
stale: boolean;
}

export interface Disposer {
(): void;
}
23 changes: 23 additions & 0 deletions types/graphqurl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"graphqurl-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/graphqurl/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

0 comments on commit 23395cf

Please sign in to comment.