Interface Client

The Client is the central hub for your GraphQL operations and holds urql's state.

Remarks

The Client manages your active GraphQL operations and their state, and contains the Exchange pipeline to execute your GraphQL operations.

It contains methods that allow you to execute GraphQL operations manually, but the Client is also interacted with by bindings (for React, Preact, Vue, Svelte, etc) to execute GraphQL operations.

While Exchanges are ultimately responsible for the control flow of operations, sending API requests, and caching, the Client still has the important responsibility for creating operations, managing consumers of active operations, sharing results for operations, and more tasks as a “central hub”.

See

https://urql.dev/goto/docs/architecture/#requests-and-operations-on-the-client for more information on what the Client is and does.

Hierarchy

  • Client

Constructors

  • Parameters

    Returns Client

Properties

operations$: Source<Operation<any, AnyVariables>>

Exposes the stream of Operations that is passed to the Exchange pipeline.

Remarks

This is a Wonka Source that issues the Operations going into the exchange pipeline.

suspense: boolean

Flag indicating whether support for “Suspense” is activated.

Remarks

This flag indicates whether support for “Suspense” has been activated via the suspense flag.

When this is enabled, the Client itself doesn’t function any differently, and the flag only serves as an instructions for the React/Preact bindings to change their behaviour.

See

suspense for more information.

Methods

  • Creates an Operation from a GraphQLRequest and optionally, overriding OperationContext options.

    Remarks

    This method is expected to be called with a kind set to the OperationType of the GraphQL operation. In development, this is enforced by checking that the GraphQL document's operation matches this kind.

    Hint: While bindings will use this method combined with executeRequestOperation, if you’re executing operations manually, you can use one of the other convenience methods instead.

    See

    • executeRequestOperation for the method used to execute operations.
    • createRequest which creates a GraphQLRequest from a DocumentNode and variables.

    Type Parameters

    Parameters

    Returns Operation<Data, Variables>

    An Operation created from the parameters.

  • Creates a Source that executes the Operation and issues OperationResults for this Operation.

    Remarks

    The Operation will be dispatched to the pipeline of Exchanges when subscribing to the returned Source, which issues OperationResults belonging to this Operation.

    Internally, OperationResults are filtered and deliverd to this source by comparing the key on the operation and the operation. For mutations, the OperationContext._instance will additionally be compared, since two mutations with, even given the same variables, will have two distinct results and will be executed separately.

    The Client dispatches the Operation when we subscribe to the returned Source and will from then on consider the Operation as “active” until we unsubscribe. When all consumers unsubscribe from an Operation and it becomes “inactive” a teardown signal will be dispatched to the Exchanges.

    Hint: While bindings will use this method, if you’re executing operations manually, you can use one of the other convenience methods instead, like executeQuery et al.

    Type Parameters

    Parameters

    Returns OperationResultSource<OperationResult<Data, Variables>>

    A Wonka Source of OperationResults for the passed Operation.

  • Creates a Source that executes the GraphQL mutation operation created from the passed parameters.

    Remarks

    The Client.mutation method is useful to programmatically create and issue a GraphQL mutation operation. It automatically calls createRequest, client.createRequestOperation, and client.executeRequestOperation for you, and is a convenience method.

    Since it returns a PromisifiedSource it may be chained with a toPromise() call to only await a single result in an async function. Since mutations will only typically issue one result, using this method is recommended.

    Hint: This is the recommended way to create mutations programmatically when not using the bindings, or when you’re trying to get a single, promisified result.

    Example

    const createPostMutation = gql`
    mutation CreatePost($text: String!) {
    createPost(text: $text) {
    id
    text
    }
    }
    `;

    async function createPost(text) {
    const result = await client.mutation(createPostMutation, {
    text,
    }).toPromise();
    if (result.error) {
    throw result.error;
    }

    return result.data.createPost;
    }

    Type Parameters

    Parameters

    • query: DocumentInput<Data, Variables>

      a GraphQL document containing the mutation operation that will be executed.

    • variables: Variables

      the variables used to execute the operation.

    • Optional context: Partial<OperationContext>

    Returns OperationResultSource<OperationResult<Data, Variables>>

    A PromisifiedSource issuing the OperationResults for the GraphQL operation.

  • Creates a Source that executes the GraphQL query operation created from the passed parameters.

    Remarks

    The Client.query method is useful to programmatically create and issue a GraphQL query operation. It automatically calls createRequest, client.createRequestOperation, and client.executeRequestOperation for you, and is a convenience method.

    Since it returns a OperationResultSource it may be chained with a toPromise() call to only await a single result in an async function.

    Hint: This is the recommended way to create queries programmatically when not using the bindings, or when you’re trying to get a single, promisified result.

    Example

    const getBookQuery = gql`
    query GetBook($id: ID!) {
    book(id: $id) {
    id
    name
    author {
    name
    }
    }
    }
    `;

    async function getBook(id) {
    const result = await client.query(getBookQuery, { id }).toPromise();
    if (result.error) {
    throw result.error;
    }

    return result.data.book;
    }

    Type Parameters

    Parameters

    • query: DocumentInput<Data, Variables>

      a GraphQL document containing the query operation that will be executed.

    • variables: Variables

      the variables used to execute the operation.

    • Optional context: Partial<OperationContext>

    Returns OperationResultSource<OperationResult<Data, Variables>>

    A OperationResultSource issuing the OperationResults for the GraphQL operation.

  • Returns the first synchronous result a Client provides for a given operation.

    Remarks

    The Client.readQuery method returns a result synchronously or defaults to null. This is useful as it limits the result for a query operation to whatever the cache Exchange of a Client had stored and available at that moment.

    In urql, it's expected that cache exchanges return their results synchronously. The bindings and this method exploit this by using synchronous results, like these, to check what data is already in the cache.

    This method is similar to what all bindings do to synchronously provide the initial state for queries, regardless of whether effects afterwards that subscribe to the query operation update this state synchronously or asynchronously.

    Type Parameters

    Parameters

    • query: DocumentInput<Data, Variables>

      a GraphQL document containing the query operation that will be executed.

    • variables: Variables

      the variables used to execute the operation.

    • Optional context: Partial<OperationContext>

    Returns OperationResult<Data, Variables>

    An OperationResult if one became available synchronously or null.

  • Dispatches an Operation to the Exchange pipeline, if this Operation is active.

    Remarks

    This method is frequently used in Exchanges, for instance caches, to reexecute an operation. It’s often either called because an Operation will need to be queried against the cache again, if a cache result has changed or been invalidated, or it’s called with an Operation's RequestPolicy set to network-only to issue a network request.

    This method will only dispatch an Operation if it has active consumers, meaning, active subscribers to the sources of OperationResult. For instance, if no bindings (e.g. useQuery) is subscribed to the Operation, then reexecuteOperation will do nothing.

    All operations are put onto a queue and executed after a micro-tick. The queue of operations is emptied eagerly and synchronously, similar to a trampoline scheduler.

    Parameters

    Returns void

  • Internal

    Subscribe method to add an event listener to debug events.

    Remarks

    This is a method that's only available in development, and allows the urql-devtools to receive to debug events that are issued by exchanges, giving the devtools more information about the flow and execution of Operations.

    See

    DebugEventTypes for a description of all debug events.

    Parameters

    • onEvent: ((event) => void)

      A callback called with new debug events, each time an Exchange issues them.

        • (event): void
        • Parameters

          Returns void

    Returns Subscription

    A Wonka Subscription which is used to optionally terminate the event listener.

  • Creates a Source that executes the GraphQL subscription operation created from the passed parameters.

    Remarks

    The Client.subscription method is useful to programmatically create and issue a GraphQL subscription operation. It automatically calls createRequest, client.createRequestOperation, and client.executeRequestOperation for you, and is a convenience method.

    Hint: This is the recommended way to create subscriptions programmatically when not using the bindings.

    Example

    import { pipe, subscribe } from 'wonka';

    const getNewsSubscription = gql`
    subscription GetNews {
    breakingNews {
    id
    text
    createdAt
    }
    }
    `;

    function subscribeToBreakingNews() {
    const subscription = pipe(
    client.subscription(getNewsSubscription, {}),
    subscribe(result => {
    if (result.data) {
    console.log(result.data.breakingNews.text);
    }
    })
    );

    return subscription.unsubscribe;
    }

    Type Parameters

    Parameters

    • query: DocumentInput<Data, Variables>

      a GraphQL document containing the subscription operation that will be executed.

    • variables: Variables

      the variables used to execute the operation.

    • Optional context: Partial<OperationContext>

    Returns OperationResultSource<OperationResult<Data, Variables>>

    A Wonka Source issuing the OperationResults for the GraphQL operation.

Generated using TypeDoc