• Stars
    star
    528
  • Rank 83,941 (Top 2 %)
  • Language
    JavaScript
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Light and simple GraphQL React client with extensible, composable cache invalidation. Works with Suspense.

npm version codecov code style: prettier

micro-graphql-react


The current version is 0.4.0-beta, but the beta is only because of the React Suspense stuff which itself is still in beta. The non-Suspense code in the latest version should be considered stable and safe.


A light (2.8K min+gzip) and simple solution for painlessly connecting your React components to a GraphQL endpoint.

Like any other GraphQL React client, there are simple hooks which query and mutate data from your GraphQL endpoint. Where this project differs is how it approaches cache invalidation. Rather than adding metadata to queries and forming a normalized, automatically-managed cache, it instead provides simple, low-level building blocks to handle cache management yourself. The reason for this (ostensibly poor!) tradeoff is because of my experience with other GraphQL clients which attempted the normalized cache route. I consistently had difficulty getting the cache to behave exactly as I wanted, so decided to build a GraphQL client that gave me the low-level control I always wound up wanting. This project is the result.

Full docs are here

The slides for the GraphQL Texas talk I gave are here

The rest of this README describes in better detail the kind of cache management problems this project attempts to avoid.

Common cache difficulties other GraphQL clients contend with

Coordinating mutations with filtered result sets

A common problem with GraphQL clients is configuring when a certain mutation should not just update existing data results, but also, more importantly, clear all other cache results, since the completed mutations might affect other queries' filters. For example, let's say you run

tasks(assignedTo: "Adam") {
  Tasks {
    id, description, assignedTo
  }
}

and get back

[
  { id: 1, description: "Adam's Task 1", assignedTo: "Adam" },
  { id: 2, description: "Adam's Task 2", assignedTo: "Adam" }
];

Now, if you subsequently run something like

mutation {
  updateTask(id: 1, assignedTo: "Bob", description: "Bob's Task")
}

the original query from above will update and now display

[
  { "id": 1, "description": "Bob's Task", "assignedTo": "Bob" },
  { "id": 2, "description": "Adam's Task 2", "assignedTo": "Adam" }
];

which may or may not be what you want, but worse, if you browse to some other filter, say, tasks(assignedTo: "Rich"), and then return to tasks(assignedTo: "Adam"), those data above will still be returned, which is wrong, since task number 1 should no longer be in this result set at all. The assignedTo value has changed, and so no longer matches the filters of this query.


This library solves this problem by allowing you to easily declare that a given mutation should clear all cache entries for a given query, and reload them from the network (hard reset), or just update the on-screen results, but otherwise clear the cache for a given query (soft reset). See the docs for more info.

Properly processing empty result sets

An interesting approach that the first version of Urql took was to, after any mutation, invalidate any and all queries which dealt with the data type you just mutated. It did this by modifying queries to add __typename metadata, so it would know which types were in which queries, and therefore needed to be refreshed after relevant mutations. This is a lot closer in terms of correctness, but even here there are edge cases which GraphQL's limited type introspection make difficult. For example, let's say you run this query

tasks(assignedTo: "Adam") {
  Tasks {
    id, description, assignedTo
  }
}

and get back

{
  "data": {
    "tasks": {
      "__typename": "TaskQueryResults",
      "Tasks": []
    }
  }
}

It's more or less impossible to know what the underlying type of the empty Tasks array is, without a build step to introspect the entire endpoint's metadata.

Are these actual problems you're facing?

These are actual problems I ran into when evaluating GraphQL clients, which left me wanting a low-level, configurable caching solution. That's the value proposition of this project. If you're not facing these problems, for whatever reasons, you'll likely be better off with a more automated solution like Urql or Apollo.

To be crystal clear, nothing in this readme should be misconstrued as claiming this project to be "better" than any other. The point is to articulate common problems with client-side GraphQL caching, and show how this project solves them. Keep these problems in mind when evaluating GraphQL clients, and pick the best solution for your app.

More Repositories

1

customize-cra

Override webpack configurations for create-react-app 2.0
JavaScript
2,778
star
2

mongo-graphql-starter

Creates a fully functioning, performant, extensible GraphQL endpoint from a Mongo DB. Supports middleware, Mongo 4 transactions.
JavaScript
424
star
3

booklist

A book tracking website—my take LibraryThing.
JavaScript
262
star
4

svelte-helpers

A toolkit of (hopefully) useful Svelte utilities. Contains helpers for using spring animations in transitions, and a Modal component.
Svelte
54
star
5

micro-graphql-svelte

Light and simple GraphQL Svelte client with extensible, composable cache invalidation.
TypeScript
44
star
6

generic-persistgraphql

Reduce bundle size and bandwidth, while securing your backend by whitelisting valid queries and mutations.
JavaScript
25
star
7

easy-express-controllers

Add MVC style controllers to Express with class decorators.
JavaScript
19
star
8

preact-lightning-talk

JavaScript
12
star
9

my-blog

TypeScript
8
star
10

next-blurhash-previews

JavaScript
8
star
11

next-wc-ssr

JavaScript
7
star
12

simple-react-bootstrap

Simple and slim React bindings for those Bootstrap components which modify the DOM. Includes Modal, ButtonDropdown, and NavBar.
JavaScript
5
star
13

vscode-react-ecosystem-snippets

VS Code snippets for React development. Covers React, and its ecosystem—for now just MobX.
JavaScript
4
star
14

okcjs-react-redux-talk

okc.js talk on react and redux
JavaScript
3
star
15

npm_sandbox

3
star
16

typescript-workshop

TypeScript
2
star
17

graphql-node-mssql

JavaScript
2
star
18

sveltekit-blog-1

JavaScript
2
star
19

rsc-sandbox

CSS
2
star
20

sveltekit-form-error-repro

JavaScript
2
star
21

svelte-kit-form-nav-repro

JavaScript
2
star
22

next-13-data-fetching-blog-post

TypeScript
2
star
23

next-13-data-fetching-blog

TypeScript
1
star
24

next-repro

JavaScript
1
star
25

vite-sandbox

JavaScript
1
star
26

booklist-desktop

C++
1
star
27

test-mongo-graphql-starter

JavaScript
1
star
28

graphql-talk

JavaScript
1
star
29

sveltekit-next-auth-post

TypeScript
1
star
30

booklist-ios

Swift
1
star
31

dynamo-adapter-edge-deploy-bug

JavaScript
1
star
32

dynamo-adapter-edge-deploy

JavaScript
1
star
33

sveltekit-blog-2-caching

JavaScript
1
star