npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

graphql-codegen-typescript-react-apollo-4

v1.0.2

Published

Fork of @graphql-codegen/typescript-react-apollo with support for Apollo Client 4

Readme

graphql-codegen-typescript-react-apollo-4

Fork of @graphql-codegen/typescript-react-apollo with (basic) Apollo Client v4 compatibility. Focus: keep existing DX while you migrate to @apollo/client v4. Not a drop‑in replacement for new v4 type features (notably dataState).

Why this fork?

@apollo/client v4 introduced a large TypeScript surface change:

  • New dataState property ("empty" | "partial" | "streaming" | "complete") for queries / fragments
  • Result type narrowing based on dataState
  • Overridable DataValue / TypeOverrides patterns (e.g. DeepPartial<T> for partial / streaming states)

The upstream @graphql-codegen/typescript-react-apollo (as of this fork) targets Apollo Client 3 style result types and does not emit code aware of the dataState discriminant.

This fork updates peer/runtime expectations so your generated React hooks compile & run against @apollo/client v4, while deliberately not attempting to model the new dataState-based narrowing yet (to keep migration friction low and avoid leaking unstable upstream internals).

What is (and is not) supported

| Area | Status | | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | | Basic hook generation (useQuery, useLazyQuery, useMutation, useSubscription, fragments) | Supported | | Apollo Client v4 package structure ("framework-agnostic" core + @apollo/client/react) | Supported (imports resolved via @apollo/client) | | React 18+ Suspense hooks you already use (useSuspenseQuery, etc.) | Generated only if you have documents using them upstream (same as original plugin behavior) | | dataState strict narrowing (empty / partial / streaming / complete) | NOT supported (see below) | | Deep partial typing for partial / streaming states | NOT supported | | Custom TypeOverrides / DataValue.* integration | NOT supported | | Data masking / unmasking types beyond upstream v2 behavior | As upstream (no new v4 overrides) |

About dataState

Apollo Client v4 adds:

const { data, dataState } = useQuery(MyQuery);
if (dataState === "complete") {
  /* data is fully typed */
}

In official v4-aware tooling, TypeScript narrows data based on dataState. Generated hooks from this fork always give you:

const { data /* dataState exists at runtime, but */ } = useMyQuery();
// data type: MyQueryQuery | undefined (no discriminated unions, no partial DeepPartial<...>)

So:

  • You may access properties guarded only by optional chaining (data?.field) even when data is partial/streaming.
  • You do not get compile‑time enforcement distinguishing partial vs complete results.
  • dataState will still be present on the runtime object returned by Apollo Client; it’s just not part of the generated type contract.

If you depend on precise narrowing (e.g. wanting a compiler error when using a field before complete), you currently need either:

  1. The upstream plugin once it adds full v4 support, or
  2. Manual wrapper types / custom plugin changes (PRs welcome).

Installation

pnpm add -D graphql-codegen-typescript-react-apollo-4
# or
npm i -D graphql-codegen-typescript-react-apollo-4
# or
yarn add -D graphql-codegen-typescript-react-apollo-4

Peer deps you must already have:

  • graphql (see peerDependencies in package.json)
  • @apollo/client ^4.x

Example Codegen config

withHooks option is treated as default because support for components and hocs is removed in apollo v4, other options are mostly preserved. Check src/config.ts for reference

codegen.yml:

schema: schema.graphql
documents: src/**/*.graphql
generates:
  src/generated/graphql.tsx:
    plugins:
      - typescript
      - typescript-operations
      - graphql-codegen-typescript-react-apollo-4 # <- this fork

Import & use:

import { useMyQuery } from "./generated/graphql";
const { data, dataState } = useMyQuery();
// data: MyQueryQuery | undefined (no narrowing)
// dataState: "empty" | "complete" | "streaming"

Typical manual guard pattern

const { data, dataState } = useMyQuery({ returnPartialData: true });
if (dataState === "complete" && data) {
  // here you *know* at runtime it's complete, but TS still sees `data | undefined`
}

Rationale for postponing dataState typing

Implementing full fidelity requires:

  • Emitting discriminated union wrappers around each hook result
  • Tracking returnPartialData, errorPolicy, @defer streaming, and fragments
  • Coordinating with Apollo’s overridable TypeOverrides + HKT patterns

That would be a larger design effort; this fork prioritizes unblocking v4 adoption first. Precise typing can be layered later without breaking runtime behavior.

License

MIT — see package.json.