// @flow import type { DocumentNode, ExecutionResult, GraphQLError, SelectionSetNode, FieldNode, InlineFragmentNode, ValueNode, SelectionNode, NameNode, } from "graphql"; import type { Middleware } from "redux"; export interface Request { // causes flow errors // property `$key` of Request. Indexable signature not found in // property `$value` of Request. Indexable signature not found in // [additionalKey: string]: any, debugName?: string, query?: DocumentNode, variables?: Object, operationName?: string | null, } export interface NetworkInterface { query(request: Request): Promise, } export interface BatchedNetworkInterface { batchQuery(requests: Request[]): Promise, } export interface MiddlewareRequest { request: Request, options: RequestOptions, } export interface MiddlewareInterface { applyMiddleware( request: MiddlewareRequest, next: Function, ): void, } export interface BatchMiddlewareRequest { requests: Request[], options: RequestOptions, } export interface BatchMiddlewareInterface { applyBatchMiddleware( request: BatchMiddlewareRequest, next: Function, ): void, } export interface AfterwareResponse { response: Response, options: RequestOptions, } export interface AfterwareInterface { applyAfterware( response: AfterwareResponse, next: Function, ): any, } export interface BatchAfterwareResponse { responses: Response[], options: RequestOptions, } export interface BatchAfterwareInterface { applyBatchAfterware( response: BatchAfterwareResponse, next: Function, ): any, } export interface PrintedRequest { debugName?: string, query?: string, variables?: Object, operationName?: string | null, } export interface SubscriptionNetworkInterface { subscribe( request: Request, handler: (error: any, result: any) => void, ): number, unsubscribe(id: Number): void, } export type NetworkMiddleware = Array | Array; export type NetworkAfterware = Array | Array; declare export class HTTPNetworkInterface { _uri: string, _opts: RequestOptions, _middlewares: NetworkMiddleware, _afterwares: NetworkAfterware, use(middlewares: NetworkMiddleware): HTTPNetworkInterface, useAfter(afterwares: NetworkAfterware): HTTPNetworkInterface, query(request: Request): Promise } export interface BatchRequestAndOptions { requests: Request[], options: RequestOptions, } export interface BatchResponseAndOptions { responses: Response[], options: RequestOptions, } export interface BatchingNetworkInterfaceOptions { uri: string, batchInterval: number, // compatibility with NetworkInterfaceOptions opts?: ?RequestOptions, } declare export function createBatchingNetworkInterface( options: BatchingNetworkInterfaceOptions, ): HTTPNetworkInterface; declare export class HTTPBatchedNetworkInterface extends BaseNetworkInterface { _middlewares: NetworkMiddleware, _afterwares: NetworkAfterware, constructor( uri: string, batchInterval: number, fetchOpts: RequestOptions, ): this, query(request: Request): Promise, batchQuery(requests: Request[]): Promise, applyBatchMiddlewares( opts: BatchRequestAndOptions, ): Promise, applyBatchAfterwares( opts: BatchResponseAndOptions, ): Promise, use(middlewares: BatchMiddlewareInterface[]): HTTPNetworkInterface, useAfter(afterwares: BatchAfterwareInterface[]): HTTPNetworkInterface, } export interface RequestAndOptions { request: Request, options: RequestOptions, } export interface ResponseAndOptions { response: Response, options: RequestOptions, } export interface NetworkInterfaceOptions { // compatibility with BatchNetworkInrfaceOptions // also network interfaces throws an error in case you haven't provide an uri // in this case is right to fail typecheck too uri: string, opts?: ?RequestOptions, } declare export function printRequest(request: Request): PrintedRequest; declare export function createNetworkInterface( uriOrInterfaceOpts: string | NetworkInterfaceOptions, secondArgOpts?: NetworkInterfaceOptions, ): HTTPNetworkInterface; declare export class BaseNetworkInterface { _middlewares: NetworkMiddleware, _afterwares: NetworkAfterware, _uri: string, _opts: RequestOptions, constructor(uri: string | void, opts?: RequestOptions): this, query(request: Request): Promise, } declare export class HTTPFetchNetworkInterface extends BaseNetworkInterface { _middlewares: NetworkMiddleware, _afterwares: NetworkAfterware, applyMiddlewares( requestAndOptions: RequestAndOptions, ): Promise, applyAfterwares(opts: ResponseAndOptions): Promise, fetchFromRemoteEndpoint(opts: RequestAndOptions): Promise, query(request: Request): Promise, use(middlewares: MiddlewareInterface[]): HTTPNetworkInterface, useAfter(afterwares: AfterwareInterface[]): HTTPNetworkInterface, } export type FetchPolicy = | "cache-first" | "cache-and-network" | "network-only" | "cache-only" | "standby"; export type SubscribeToMoreOptions = { document: DocumentNode, variables?: { [key: string]: any, }, updateQuery?: ( previousQueryResult: Object, options: { subscriptionData: { data: any, }, variables: { [key: string]: any, }, }, ) => Object, onError?: (error: Error) => void, }; export type MutationUpdaterFn = ( proxy: DataProxy, mutationResult: ApolloExecutionResult, ) => void; export interface ModifiableWatchQueryOptions { variables?: { [key: string]: any, }, pollInterval?: number, fetchPolicy?: FetchPolicy, fetchResults?: boolean, notifyOnNetworkStatusChange?: boolean, reducer?: OperationResultReducer, } export interface WatchQueryOptions { query: DocumentNode, metadata?: any, } export interface FetchMoreQueryOptions { query?: DocumentNode, variables?: { [key: string]: any, }, } export interface SubscriptionOptions { query: DocumentNode, variables?: { [key: string]: any, }, } export interface MutationOptions { mutation: DocumentNode, variables?: Object, optimisticResponse?: Object, updateQueries?: MutationQueryReducersMap, refetchQueries?: string[] | PureQueryOptions[], update?: MutationUpdaterFn, } export type QueryListener = ( queryStoreValue: QueryStoreValue, ) => void; export type FetchType = number; export type PureQueryOptions = { query: DocumentNode, variables?: { [key: string]: any, }, }; export type ApolloQueryResult = { data: T, loading: boolean, networkStatus: NetworkStatus, stale: boolean, }; export type ApolloExecutionResult = { data: T; }; export type IdGetter = (value: Object) => string | null | void; export type ListValue = Array; export type StoreValue = | number | string | string[] | IdValue | ListValue | JsonValue | null | void; export interface NormalizedCache { [dataId: string]: StoreObject, } export interface StoreObject { [storeFieldKey: string]: StoreValue, ___typename?: string, } export interface IdValue { type: "id", id: string, generated: boolean, } export interface JsonValue { type: "json", json: any, } declare export function valueToObjectRepresentation( argObj: any, name: NameNode, value: ValueNode, variables?: Object, ): void; declare export function storeKeyNameFromField( field: FieldNode, variables?: Object, ): string; declare export function storeKeyNameFromFieldNameAndArgs( fieldName: string, args?: Object, ): string; declare export function resultKeyNameFromField(field: FieldNode): string; declare export function isField(selection: SelectionNode): FieldNode; declare export function isInlineFragment( selection: SelectionNode, ): InlineFragmentNode; declare export function graphQLResultHasError(result: ExecutionResult): | number | void; declare export function isIdValue(idObject: StoreValue): IdValue; declare export function toIdValue(id: string, generated?: boolean): IdValue; declare export function isJsonValue(jsonObject: StoreValue): JsonValue; export type MutationQueryReducer = ( previousResult: Object, options: { mutationResult: ApolloExecutionResult, queryName: string | null, queryVariables: Object, }, ) => Object; export type MutationQueryReducersMap = { [queryName: string]: MutationQueryReducer, }; export type OperationResultReducer = ( previousResult: Object, action: ApolloAction, variables: Object, ) => Object; export type OperationResultReducerMap = { [queryId: string]: OperationResultReducer, }; export type ApolloCurrentResult = { data: T | {}, loading: boolean, networkStatus: NetworkStatus, error?: ApolloError, partial?: boolean, }; export interface FetchMoreOptions { updateQuery: ( previousQueryResult: Object, options: { fetchMoreResult: Object, queryVariables: Object, }, ) => Object, } export interface UpdateQueryOptions { variables?: Object, } declare export class ObservableQuery extends Observable> { options: WatchQueryOptions, queryId: string, variables: { [key: string]: any, }, constructor( data: { scheduler: QueryScheduler, options: WatchQueryOptions, shouldSubscribe?: boolean, }, ): this, result(): Promise>, currentResult(): ApolloCurrentResult, getLastResult(): ApolloQueryResult, refetch(variables?: any): Promise>, fetchMore( fetchMoreOptions: FetchMoreQueryOptions & FetchMoreOptions, ): Promise>, subscribeToMore(options: SubscribeToMoreOptions): () => void, setOptions( opts: ModifiableWatchQueryOptions, ): Promise>, setVariables( variables: any, tryFetch?: boolean, ): Promise>, updateQuery( mapFn: (previousQueryResult: any, options: UpdateQueryOptions) => any, ): void, stopPolling(): void, startPolling(pollInterval: number): void, } export type CleanupFunction = () => void; export type SubscriberFunction = (observer: Observer) => | Subscription | CleanupFunction; export interface Observer { next?: (value: T) => void, error?: (error: Error) => void, complete?: () => void, } export interface Subscription { unsubscribe: CleanupFunction, } declare export class Observable { constructor(subscriberFunction: SubscriberFunction): this, subscribe(observer: Observer): Subscription, } export type NetworkStatus = number; declare export function isNetworkRequestInFlight( networkStatus: NetworkStatus, ): boolean; export interface MutationStore { [mutationId: string]: MutationStoreValue, } export interface MutationStoreValue { mutationString: string, variables: Object, loading: boolean, error: Error | null, } export type IntrospectionResultData = { ___schema: { types: [{ kind: string, name: string, possibleTypes: [{ name: string, }], }], }, }; declare export class FragmentMatcherInterface { match( idValue: IdValue, typeCondition: string, context: ReadStoreContext, ): boolean, } declare export class IntrospectionFragmentMatcher extends FragmentMatcherInterface { constructor( options?: { introspectionQueryResultData?: IntrospectionResultData, }, ): this, match( idValue: IdValue, typeCondition: string, context: ReadStoreContext, ): boolean, } declare export class HeuristicFragmentMatcher extends FragmentMatcherInterface { constructor(): this, ensureReady(): Promise, canBypassInit(): boolean, match( idValue: IdValue, typeCondition: string, context: ReadStoreContext, ): boolean, } export interface DataProxyReadQueryOptions { query: DocumentNode, variables?: Object, } export interface DataProxyReadFragmentOptions { id: string, fragment: DocumentNode, fragmentName?: string, variables?: Object, } export interface DataProxyWriteQueryOptions { data: any, query: DocumentNode, variables?: Object, } export interface DataProxyWriteFragmentOptions { data: any, id: string, fragment: DocumentNode, fragmentName?: string, variables?: Object, } declare export class DataProxy { readQuery(options: DataProxyReadQueryOptions): QueryType, readFragment(options: DataProxyReadFragmentOptions): | FragmentType | null, writeQuery(options: DataProxyWriteQueryOptions): void, writeFragment(options: DataProxyWriteFragmentOptions): void, } declare export class ReduxDataProxy extends DataProxy { constructor( store: ApolloStore, reduxRootSelector: (state: any) => Store, fragmentMatcher: FragmentMatcherInterface, reducerConfig: ApolloReducerConfig, ): this, readQuery(options: DataProxyReadQueryOptions): QueryType, readFragment(options: DataProxyReadFragmentOptions): | FragmentType | null, writeQuery(options: DataProxyWriteQueryOptions): void, writeFragment(options: DataProxyWriteFragmentOptions): void, } declare export class TransactionDataProxy extends DataProxy { constructor( data: NormalizedCache, reducerConfig: ApolloReducerConfig, ): this, finish(): Array, readQuery(options: DataProxyReadQueryOptions): QueryType, readFragment(options: DataProxyReadFragmentOptions): | FragmentType | null, writeQuery(options: DataProxyWriteQueryOptions): void, writeFragment(options: DataProxyWriteFragmentOptions): void, } export type QueryResultAction = { type: "APOLLO_QUERY_RESULT", result: ExecutionResult, queryId: string, document: DocumentNode, operationName: string | null, requestId: number, fetchMoreForQueryId?: string, extraReducers?: ApolloReducer[], }; export type ApolloAction = | QueryResultAction | QueryErrorAction | QueryInitAction | QueryResultClientAction | QueryStopAction | MutationInitAction | MutationResultAction | MutationErrorAction | UpdateQueryResultAction | StoreResetAction | SubscriptionResultAction | WriteAction; declare export function mutations( previousState: MutationStore | void, action: ApolloAction, ): MutationStore; export interface QueryErrorAction { type: "APOLLO_QUERY_ERROR", error: Error, queryId: string, requestId: number, fetchMoreForQueryId?: string, } export interface QueryInitAction { type: "APOLLO_QUERY_INIT", queryString: string, document: DocumentNode, variables: Object, fetchPolicy: FetchPolicy, queryId: string, requestId: number, storePreviousVariables: boolean, isRefetch: boolean, isPoll: boolean, fetchMoreForQueryId?: string, metadata: any, } export interface QueryResultClientAction { type: "APOLLO_QUERY_RESULT_CLIENT", result: ExecutionResult, complete: boolean, queryId: string, requestId: number, } export interface QueryStopAction { type: "APOLLO_QUERY_STOP", queryId: string, } export type QueryWithUpdater = { reducer: MutationQueryReducer, query: QueryStoreValue, }; export interface MutationInitAction { type: "APOLLO_MUTATION_INIT", mutationString: string, mutation: DocumentNode, variables: Object, operationName: string | null, mutationId: string, optimisticResponse: Object | void, extraReducers?: ApolloReducer[], updateQueries?: { [queryId: string]: QueryWithUpdater, }, update?: (proxy: DataProxy, mutationResult: Object) => void, } export interface MutationResultAction { type: "APOLLO_MUTATION_RESULT", result: ExecutionResult, document: DocumentNode, operationName: string | null, variables: Object, mutationId: string, extraReducers?: ApolloReducer[], updateQueries?: { [queryId: string]: QueryWithUpdater, }, update?: (proxy: DataProxy, mutationResult: Object) => void, } export interface MutationErrorAction { type: "APOLLO_MUTATION_ERROR", error: Error, mutationId: string, } export interface UpdateQueryResultAction { type: "APOLLO_UPDATE_QUERY_RESULT", variables: any, document: DocumentNode, newResult: Object, } export interface StoreResetAction { type: "APOLLO_STORE_RESET", observableQueryIds: string[], } export interface SubscriptionResultAction { type: "APOLLO_SUBSCRIPTION_RESULT", result: ExecutionResult, subscriptionId: number, variables: Object, document: DocumentNode, operationName: string | null, extraReducers?: ApolloReducer[], } export interface DataWrite { rootId: string, result: any, document: DocumentNode, variables: Object, } export interface WriteAction { type: "APOLLO_WRITE", writes: Array, } declare export function isQueryResultAction( action: ApolloAction, ): QueryResultAction; declare export function isQueryErrorAction( action: ApolloAction, ): QueryErrorAction; declare export function isQueryInitAction( action: ApolloAction, ): QueryInitAction; declare export function isQueryResultClientAction( action: ApolloAction, ): QueryResultClientAction; declare export function isQueryStopAction( action: ApolloAction, ): QueryStopAction; declare export function isMutationInitAction( action: ApolloAction, ): MutationInitAction; declare export function isMutationResultAction( action: ApolloAction, ): MutationResultAction; declare export function isMutationErrorAction( action: ApolloAction, ): MutationErrorAction; declare export function isUpdateQueryResultAction( action: ApolloAction, ): UpdateQueryResultAction; declare export function isStoreResetAction( action: ApolloAction, ): StoreResetAction; declare export function isSubscriptionResultAction( action: ApolloAction, ): SubscriptionResultAction; declare export function isWriteAction(action: ApolloAction): WriteAction; export type ApolloStateSelector = (state: any) => Store; declare export class ApolloError extends Error { message: string, graphQLErrors: GraphQLError[], networkError: Error | null, extraInfo: any, constructor(info: ErrorConstructor): this, } declare export function isApolloError(err: Error): ApolloError; interface ErrorConstructor { graphQLErrors?: GraphQLError[], networkError?: Error | null, errorMessage?: string, extraInfo?: any, } export type OptimisticStoreItem = { mutationId: string, data: NormalizedCache, }; export type OptimisticStore = OptimisticStoreItem[]; export interface Store { data: NormalizedCache, queries: QueryStore, mutations: MutationStore, optimistic: OptimisticStore, reducerError: ReducerError | null, } export interface ApolloStore { dispatch: (action: ApolloAction) => void, getState: () => any, } export type ApolloReducer = ( store: NormalizedCache, action: ApolloAction, ) => NormalizedCache; export type ApolloReducerConfig = { dataIdFromObject?: IdGetter, customResolvers?: CustomResolverMap, fragmentMatcher?: FragmentMatcher, addTypename?: boolean, }; export interface ReducerError { error: Error, queryId?: string, mutationId?: string, subscriptionId?: number, } declare export function getDataWithOptimisticResults( store: Store, ): NormalizedCache; declare export function optimistic( previousState: any[] | void, action: any, store: any, config: any, ): OptimisticStore; export type QueryStoreValue = { queryString: string, document: DocumentNode, variables: Object, previousVariables: Object | null, networkStatus: NetworkStatus, networkError: Error | null, graphQLErrors: GraphQLError[], lastRequestId: number, metadata: any, }; export interface QueryStore { [queryId: string]: QueryStoreValue, } export interface SelectionSetWithRoot { id: string, typeName: string, selectionSet: SelectionSetNode, } declare export function queries( previousState: QueryStore | void, action: ApolloAction, ): QueryStore; type FragmentMatcher = ( rootValue: any, typeCondition: string, context: any, ) => boolean; export type DiffResult = { result?: any, isMissing?: boolean, }; export type ReadQueryOptions = { store: NormalizedCache, query: DocumentNode, fragmentMatcherFunction?: FragmentMatcher, variables?: Object, previousResult?: any, rootId?: string, config?: ApolloReducerConfig, }; export type DiffQueryAgainstStoreOptions = & ReadQueryOptions & { returnPartialData?: boolean, }; export type CustomResolver = ( rootValue: any, args: { [argName: string]: any, }, ) => any; export type CustomResolverMap = { [typeName: string]: { [fieldName: string]: CustomResolver, }, }; export type ReadStoreContext = { store: NormalizedCache, returnPartialData: boolean, hasMissingField: boolean, customResolvers: CustomResolverMap, }; declare export function readQueryFromStore( options: ReadQueryOptions, ): QueryType; declare export function diffQueryAgainstStore( result: DiffQueryAgainstStoreOptions, ): DiffResult; declare export function assertIdValue(idValue: IdValue): void; declare export class QueryScheduler { inFlightQueries: { [queryId: string]: WatchQueryOptions, }, registeredQueries: { [queryId: string]: WatchQueryOptions, }, intervalQueries: { [interval: number]: string[], }, queryManager: QueryManager, constructor( queryManager: { queryManager: QueryManager, }, ): this, checkInFlight(queryId: string): boolean, fetchQuery( queryId: string, options: WatchQueryOptions, fetchType: FetchType, ): Promise<{}>, startPollingQuery( options: WatchQueryOptions, queryId: string, listener?: QueryListener, ): string, stopPollingQuery(queryId: string): void, fetchQueriesOnInterval(interval: number): void, addQueryOnInterval( queryId: string, queryOptions: WatchQueryOptions, ): void, registerPollingQuery( queryOptions: WatchQueryOptions, ): ObservableQuery, } declare export function createApolloReducer(config: ApolloReducerConfig): ( state: Store, action: ApolloAction, ) => Store; declare export function createApolloStore( init?: { reduxRootKey?: string, initialState?: any, config?: ApolloReducerConfig, reportCrashes?: boolean, logger?: Middleware<*, *>, }, ): ApolloStore; declare export function createApolloReducer(config: ApolloReducerConfig): ( state: Store, action: ApolloAction, ) => Store; declare export function createApolloStore( store?: { reduxRootKey?: string, initialState?: any, config?: ApolloReducerConfig, reportCrashes?: boolean, logger?: Middleware<*, *>, }, ): ApolloStore; declare export class QueryManager { pollingTimers: { [queryId: string]: any, }, scheduler: QueryScheduler, store: ApolloStore, networkInterface: NetworkInterface, ssrMode: boolean, constructor( setup: { networkInterface: NetworkInterface, store: ApolloStore, reduxRootSelector: ApolloStateSelector, fragmentMatcher?: FragmentMatcherInterface, reducerConfig?: ApolloReducerConfig, addTypename?: boolean, queryDeduplication?: boolean, ssrMode?: boolean, }, ): this, broadcastNewStore(store: any): void, mutate( mutationOpts: { mutation: DocumentNode, variables?: Object, optimisticResponse?: Object, updateQueries?: MutationQueryReducersMap, refetchQueries?: string[] | PureQueryOptions[], update?: (proxy: DataProxy, mutationResult: Object) => void, }, ): Promise>, fetchQuery( queryId: string, options: WatchQueryOptions, fetchType?: FetchType, fetchMoreForQueryId?: string, ): Promise>, queryListenerForObserver( queryId: string, options: WatchQueryOptions, observer: Observer>, ): QueryListener, watchQuery( options: WatchQueryOptions, shouldSubscribe?: boolean, ): ObservableQuery, query(options: WatchQueryOptions): Promise>, generateQueryId(): string, stopQueryInStore(queryId: string): void, getApolloState(): Store, selectApolloState(store: any): Store, getInitialState(): { data: Object, }, getDataWithOptimisticResults(): NormalizedCache, addQueryListener(queryId: string, listener: QueryListener): void, addFetchQueryPromise( requestId: number, promise: Promise>, resolve: (result: ApolloQueryResult) => void, reject: (error: Error) => void, ): void, removeFetchQueryPromise(requestId: number): void, addObservableQuery( queryId: string, observableQuery: ObservableQuery, ): void, removeObservableQuery(queryId: string): void, resetStore(): void, startQuery( queryId: string, options: WatchQueryOptions, listener: QueryListener, ): string, startGraphQLSubscription(options: SubscriptionOptions): Observable, removeQuery(queryId: string): void, stopQuery(queryId: string): void, getCurrentQueryResult( observableQuery: ObservableQuery, isOptimistic?: boolean, ): any, getQueryWithPreviousResult( queryIdOrObservable: string | ObservableQuery, isOptimistic?: boolean, ): { previousResult: any, variables: | { [key: string]: any, } | void, document: DocumentNode, }, } declare export class ApolloClient extends DataProxy { networkInterface: NetworkInterface, store: ApolloStore, reduxRootSelector: ApolloStateSelector | null, initialState: any, queryManager: QueryManager, reducerConfig: ApolloReducerConfig, addTypename: boolean, disableNetworkFetches: boolean, dataId: IdGetter | void, dataIdFromObject: IdGetter | void, fieldWithArgs: (fieldName: string, args?: Object) => string, version: string, queryDeduplication: boolean, constructor( options?: { networkInterface?: NetworkInterface, reduxRootSelector?: string | ApolloStateSelector, initialState?: any, dataIdFromObject?: IdGetter, ssrMode?: boolean, ssrForceFetchDelay?: number, addTypename?: boolean, customResolvers?: CustomResolverMap, connectToDevTools?: boolean, queryDeduplication?: boolean, fragmentMatcher?: FragmentMatcherInterface, }, ): this, watchQuery(options: WatchQueryOptions): ObservableQuery, query(options: WatchQueryOptions): Promise>, mutate(options: MutationOptions): Promise>, subscribe(options: SubscriptionOptions): Observable, readQuery(options: DataProxyReadQueryOptions): T, readFragment(options: DataProxyReadFragmentOptions): T | null, writeQuery(options: DataProxyWriteQueryOptions): void, writeFragment(options: DataProxyWriteFragmentOptions): void, reducer(): (state: Store, action: ApolloAction) => Store, ___actionHookForDevTools(cb: Function): void, middleware: () => Middleware, initStore(): void, resetStore(): void, getInitialState(): { data: Object, }, } declare export default typeof ApolloClient;