import { Observable } from 'rxjs'; import { NetworkStatus, WatchQueryOptions, MutationOptions, } from 'apollo-client'; import { Operation, RequestHandler } from 'apollo-link'; import { ExecutionResult, GraphQLError } from 'graphql'; import { ErrorResponse } from 'apollo-link-error'; export type GraphqlWatchQueryOptions = WatchQueryOptions; export type GraphqlMutationOptions = MutationOptions; export interface IGraphqlClient { events$: Observable; network$: Observable; query: ( options: GraphqlWatchQueryOptions, ) => Promise>; query$: ( options: GraphqlWatchQueryOptions, ) => Observable>; mutate: ( options: GraphqlMutationOptions, ) => Promise>; } /** * Initialization options. */ export interface IGraphqlInitOptions { middleware?: GraphqlMiddlewareHandler | GraphqlMiddlewareHandler[]; headers?: IGraphqlHeaders | GraphqlHeadersFactory; } /** * Graphql [Query] response. */ export type GraphqlQueryResult = { data: T; errors?: GraphQLError[] | undefined; loading: boolean; networkStatus: NetworkStatus; stale: boolean; }; /* * Graphql [Mutation] response. */ export type GraphqlMutationResult = ExecutionResult & { context?: T | undefined; extensions?: Record | undefined; }; /** * Middleware. */ export type GraphqlOperation = Operation; export type GraphqlMiddlewareHandler = RequestHandler; export type GraphqlHeadersFactory = () => IGraphqlHeaders; export type GraphqlHeaderRequest = ( req: GraphqlOperation, ) => string | number | undefined; export type GraphqlHeaderValue = GraphqlHeaderRequest | string | number; export interface IGraphqlHeaders { [key: string]: GraphqlHeaderValue; } /** * Events. */ export type GraphqlEvent = { id: string; type: GraphqlEventType; payload: GraphqlEventPayload; }; export type GraphqlNetworkEvent = { count: number; isActive: boolean; }; export type GraphqlEventType = | 'ERROR' | 'QUERY_START' | 'QUERY_COMPLETE' | 'MUTATION_START' | 'MUTATION_COMPLETE'; export type GraphqlEventPayload = | GraphqlErrorEvent | GraphqlQueryEvent | GraphqlMutationEvent; export type GraphqlErrorEvent = ErrorResponse; export type GraphqlQueryEvent = { options: GraphqlWatchQueryOptions; result?: GraphqlQueryResult; }; export type GraphqlMutationEvent = { options: GraphqlMutationOptions; result?: GraphqlMutationResult; };