npm @apollo/client 4.0.0-alpha.10
v4.0.0-alpha.10

29 days ago

Major Changes

  • #12559 49ace0e Thanks @jerelmiller! - ObservableQuery.variables can now be reset back to empty when calling reobserve with variables: undefined. Previously the variables key would be ignored so variables would remain unchanged.

  • #12559 49ace0e Thanks @jerelmiller! - never is no longer supported as a valid TVariables generic argument for APIs that require variables as part of its type. Use Record<string, never> instead.

  • #12559 49ace0e Thanks @jerelmiller! - When passing a variables key with the value undefined, the value will be replaced by the default value in the query, if it is provided, rather than leave it as undefined.

    // given this query
    const query = gql`
      query PaginatedQuery($limit: Int! = 10, $offset: Int) {
        list(limit: $limit, offset: $offset) {
          id
        }
      }
    `;
    
    const observable = client.query({
      query,
      variables: { limit: 5, offset: 0 },
    });
    console.log(observable.variables); // => { limit: 5, offset: 0 }
    
    observable.reobserve({ variables: { limit: undefined, offset: 10 } });
    // limit is now `10`. This would previously be `undefined`
    console.log(observable.variables); // => { limit: 10, offset: 10 }
  • #12562 90bf0e6 Thanks @jerelmiller! - client.query no longer supports a fetchPolicy of standby. standby does not fetch and did not return data. standby is meant for watched queries where fetching should be on hold.

Minor Changes

  • #12557 51d26ae Thanks @jerelmiller! - Add ability to specify message formatter for CombinedGraphQLErrors and CombinedProtocolErrors. To provide your own message formatter, override the static formatMessage property on these classes.

    CombinedGraphQLErrors.formatMessage = (
      errors,
      { result, defaultFormatMessage }
    ) => {
      return "Some formatted message";
    };
    
    CombinedProtocolErrors.formatMessage = (errors, { defaultFormatMessage }) => {
      return "Some formatted message";
    };
  • #12546 5dffbbe Thanks @jerelmiller! - Add a static is method to error types defined by Apollo Client. is makes it simpler to determine whether an error is a specific type, which can be helpful in cases where you'd like to narrow the error type in order to use specific properties from that error.

    This change applies to the following error types:

    • CombinedGraphQLErrors
    • CombinedProtocolErrors
    • ServerError
    • ServerParseError
    • UnconventionalError

    Example

    import { CombinedGraphQLErrors } from "@apollo/client";
    
    if (CombinedGraphQLErrors.is(error)) {
      console.log(error.message);
      error.errors.forEach((graphQLError) => console.log(graphQLError.message));
    }
  • #12561 99d72bf Thanks @jerelmiller! - Add the ability to detect if an error was an error was emitted from the link chain. This is useful if your application throws custom errors in other areas of the application and you'd like to differentiate them from errors emitted by the link chain itself.

    To detect if an error was emitted from the link chain, use LinkError.is.

    import { LinkError } from "@apollo/client";
    
    client.query({ query }).catch((error) => {
      if (LinkError.is(error)) {
        // This error originated from the link chain
      }
    });

Patch Changes

  • #12559 49ace0e Thanks @jerelmiller! - The variables option used with various APIs are now enforced more consistently across the client when TVariables contains required variables. If required variables are not provided, TypeScript will now complain that it requires a variables option.

    This change affects the following APIs:

    • client.query
    • client.mutate
    • client.subscribe
    • client.watchQuery
    • useBackgroundQuery
    • useQuery
    • useSubscription
    • useSuspenseQuery
  • #12559 49ace0e Thanks @jerelmiller! - Fix type of variables returned from useLazyQuery. When called is false, variables is now Partial<TVariables> instead of TVariables.

  • #12562 90bf0e6 Thanks @jerelmiller! - client.query no longer supports notifyOnNetworkStatusChange in options. An error will be thrown if this option is set. The effects of this option were not observable by client.query since client.query emits a single result.

  • #12557 51d26ae Thanks @jerelmiller! - Update format of the error message for CombinedGraphQLErrors and CombinedProtocolErrors to be more like v3.x.

    console.log(error.message);
    - `The GraphQL server returned with errors:
    - - Email not found
    - - Username already in use`
    + `Email not found
    + Username already in use`
  • #12559 49ace0e Thanks @jerelmiller! - ObservableQuery.variables has been updated to return TVariables rather than TVariables | undefined. This is more consistent with the runtime value where an empty object ({}) will be returned when the variables option is not provided.

Don't miss a new client release

NewReleases is sending notifications on new releases.