Socket
Socket
Sign inDemoInstall

@apollo/client

Package Overview
Dependencies
28
Maintainers
1
Versions
551
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @apollo/client

A fully-featured caching GraphQL client.


Version published
Weekly downloads
3.2M
increased by3.81%
Maintainers
1
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

Apollo Client 3.3.20

Bug fixes

  • Fix policy merging bug when calling cache.policies.addTypePolicies multiple times for the same type policy. <br/> @Banou26 in #8361

Readme

Source

Apollo Client

Apollo Client

npm version Build Status Join the community

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.

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

Who is Apollo?

Apollo Graph, Inc. creates industry-leading tools for building applications with GraphQL:

  • Apollo Client – The most popular GraphQL client for the web. Apollo also builds and maintains Apollo iOS and Apollo Android.
  • Apollo Server – Build a production-ready JavaScript GraphQL server with a schema-first approach.
  • Apollo Studio – A turnkey portal for GraphQL developers, featuring a powerful GraphQL IDE (the Apollo Explorer), metrics reporting, schema search, and documentation.
  • Apollo Federation – Create and manage a single data graph composed of subgraphs that can be developed independently.

We are fully committed to advancing the frontier of graph development with open-source libraries, hosted software tooling, developer extensions, and community contributions.

Keywords

FAQs

Last updated on 08 Jun 2021

Did you know?

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc