UNPKG

graphql-typed-apollo-client

Version:

A tool that generates a strongly typed client library for any GraphQL endpoint. The client allows writing GraphQL queries as plain JS objects (with type safety, awesome code completion experience, custom scalar type mapping, type guards and more)

41 lines (40 loc) 1.96 kB
import 'isomorphic-fetch'; import qs from 'qs'; import { ExecutionResult, GraphQLError } from 'graphql'; import { Observable } from 'rxjs'; import { QueryHookOptions, MutationHookOptions } from '@apollo/client'; import { TypeMapper } from './applyTypeMapperToResponse'; import { LinkedType } from './linkTypeMap'; import { Fields, Gql } from './requestToGql'; import { SubscriptionCreatorOptions } from './getSubscriptionCreator'; export declare class ClientError extends Error { errors?: readonly GraphQLError[] | undefined; constructor(message?: string, errors?: readonly GraphQLError[] | undefined); } export interface Fetcher { (gql: Gql, fetchImpl: typeof fetch, qsImpl: typeof qs): Promise<ExecutionResult<any>>; } export interface Client<QR, QC, Q, MR, MC, M, SR, SC, S> { apolloQuery(apolloContext: any, request: QR, options?: QueryHookOptions<any, Record<string, any>> | undefined): any; useQuery(request: QR, options?: QueryHookOptions<any, Record<string, any>> | undefined): any; useMutation(request: Function, options?: MutationHookOptions<any, Record<string, any>> | undefined): any; query(request: QR): Promise<ExecutionResult<Q>>; mutation(request: MR): Promise<ExecutionResult<M>>; subscription(request: SR): Observable<ExecutionResult<S>>; chain: { query: QC; mutation: MC; subscription: SC; }; } export interface ClientOptions { fetcher?: Fetcher; subscriptionCreatorOptions?: SubscriptionCreatorOptions; } export interface ClientEmbeddedOptions { queryRoot?: LinkedType; mutationRoot?: LinkedType; subscriptionRoot?: LinkedType; typeMapper?: TypeMapper; } export declare const createClient: <QR extends Fields, QC, Q, MR extends Fields, MC, M, SR extends Fields, SC, S>({ fetcher, subscriptionCreatorOptions, queryRoot, mutationRoot, subscriptionRoot, typeMapper, }: ClientOptions & ClientEmbeddedOptions) => Client<QR, QC, Q, MR, MC, M, SR, SC, S>;