You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@apollo/client

Package Overview
Dependencies
Maintainers
4
Versions
559
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apollo/client

A fully-featured caching GraphQL client.


Version published
Weekly downloads
3.3M
increased by0.14%
Maintainers
4
Created
Weekly downloads
 

Package description

What is @apollo/client?

The @apollo/client npm package is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. It is designed to help you develop faster and more secure apps with less boilerplate. It integrates seamlessly with any JavaScript front-end, allowing for reactive data fetching, caching, and data management.

What are @apollo/client's main functionalities?

Fetching data with useQuery

This feature allows you to fetch data from a GraphQL server. The useQuery hook is used to execute a GraphQL query and handle loading, error, and result states.

import { useQuery, gql } from '@apollo/client';

const GET_LAUNCHES = gql`
  query GetLaunches {
    launches {
      id
      mission_name
    }
  }
`;

function Launches() {
  const { loading, error, data } = useQuery(GET_LAUNCHES);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;

  return data.launches.map(({ id, mission_name }) => (
    <div key={id}>
      <p>{mission_name}</p>
    </div>
  ));
}

Updating data with useMutation

This feature enables you to update data on a GraphQL server. The useMutation hook is used to execute a mutation, allowing you to create, update, or delete data.

import { useMutation, gql } from '@apollo/client';

const ADD_TODO = gql`
  mutation AddTodo($type: String!) {
    addTodo(type: $type) {
      id
      type
    }
  }
`;

function AddTodo() {
  let input;
  const [addTodo, { data }] = useMutation(ADD_TODO);

  return (
    <div>
      <form
        onSubmit={e => {
          e.preventDefault();
          addTodo({ variables: { type: input.value } });
          input.value = '';
        }}
      >
        <input
          ref={node => {
            input = node;
          }}
        />
        <button type="submit">Add Todo</button>
      </form>
    </div>
  );
}

Managing local state

This feature allows you to manage local state using Apollo Client. You can read and write to the cache as if it were a local database, enabling you to manage client-side state alongside remote data.

import { gql, useApolloClient } from '@apollo/client';

const GET_LOCAL_STATE = gql`
  query GetLocalState {
    isLoggedIn @client
  }
`;

function LoginButton() {
  const client = useApolloClient();
  const data = client.readQuery({ query: GET_LOCAL_STATE });
  const isLoggedIn = data.isLoggedIn;

  return (
    <button onClick={() => {
      client.writeQuery({
        query: GET_LOCAL_STATE,
        data: { isLoggedIn: !isLoggedIn }
      });
    }}>
      {isLoggedIn ? 'Log out' : 'Log in'}
    </button>
  );
}

Other packages similar to @apollo/client

Changelog

Source

3.11.0-rc.0

Minor Changes

  • #11923 d88c7f8 Thanks @jerelmiller! - Add support for subscribeToMore function to useQueryRefHandlers.

  • #11854 3812800 Thanks @jcostello-atlassian! - Support extensions in useSubscription

  • #11923 d88c7f8 Thanks @jerelmiller! - Add support for subscribeToMore function to useLoadableQuery.

  • #11863 98e44f7 Thanks @phryneas! - Reimplement useSubscription to fix rules of React violations.

  • #11869 a69327c Thanks @phryneas! - Rewrite big parts of useQuery and useLazyQuery to be more compliant with the Rules of React and React Compiler

  • #11936 1b23337 Thanks @jerelmiller! - Add the ability to specify a name for the client instance for use with Apollo Client Devtools. This is useful when instantiating multiple clients to identify the client instance more easily. This deprecates the connectToDevtools option in favor of a new devtools configuration.

    new ApolloClient({
      devtools: {
        enabled: true,
        name: "Test Client",
      },
    });
    

    This option is backwards-compatible with connectToDevtools and will be used in the absense of a devtools option.

  • #11923 d88c7f8 Thanks @jerelmiller! - Add support for subscribeToMore function to useBackgroundQuery.

  • #11789 5793301 Thanks @phryneas! - Changes usages of the GraphQLError type to GraphQLFormattedError.

    This was a type bug - these errors were never GraphQLError instances to begin with, and the GraphQLError class has additional properties that can never be correctly rehydrated from a GraphQL result. The correct type to use here is GraphQLFormattedError.

    Similarly, please ensure to use the type FormattedExecutionResult instead of ExecutionResult - the non-"Formatted" versions of these types are for use on the server only, but don't get transported over the network.

  • #11930 a768575 Thanks @jerelmiller! - Deprecates experimental schema testing utilities introduced in 3.10 in favor of recommending @apollo/graphql-testing-library.

Patch Changes

Readme

Source

Apollo Client

Apollo Client

npm version Build Status Join the community Join our Discord server

Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to easily build UI components that fetch data via GraphQL.

☑️ Apollo Client User Survey
What do you like best about Apollo Client? What needs to be improved? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Documentation

All Apollo Client documentation, including React integration articles and helpful recipes, can be found at:
https://www.apollographql.com/docs/react/

The Apollo Client API reference can be found at:
https://www.apollographql.com/docs/react/api/apollo-client/

Learn how to use Apollo Client with self-paced hands-on training on Odyssey, Apollo's official learning platform:
https://odyssey.apollographql.com/

Maintainers

NameUsername
Ben Newman@benjamn
Alessia Bellisario@alessbell
Jeff Auriemma@bignimbus
Hugh Willson@hwillson
Jerel Miller@jerelmiller
Lenz Weber-Tronic@phryneas

Who is Apollo?

Apollo builds open-source software and a graph platform to unify GraphQL across your apps and services. We help you ship faster with:

  • Apollo Studio – A free, end-to-end platform for managing your GraphQL lifecycle. Track your GraphQL schemas in a hosted registry to create a source of truth for everything in your graph. Studio provides an IDE (Apollo Explorer) so you can explore data, collaborate on queries, observe usage, and safely make schema changes.
  • Apollo Federation – The industry-standard open architecture for building a distributed graph. Use Apollo’s gateway to compose a unified graph from multiple subgraphs, determine a query plan, and route requests across your services.
  • Apollo Client – The most popular GraphQL client for the web. Apollo also builds and maintains Apollo iOS and Apollo Kotlin.
  • Apollo Server – A production-ready JavaScript GraphQL server that connects to any microservice, API, or database. Compatible with all popular JavaScript frameworks and deployable in serverless environments.

Learn how to build with Apollo

Check out the Odyssey learning platform, the perfect place to start your GraphQL journey with videos and interactive code challenges. Join the Apollo Community to interact with and get technical help from the GraphQL community.

Keywords

FAQs

Package last updated on 09 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc