• Stars
    star
    199
  • Rank 196,105 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

๐ŸŒ ๐Ÿ’ซ Simple and powerful GraphQL Client, love child of react-query โค๏ธ relay

magiql

A bunch of simple but magical React hooks to work with GraphQL.
powered by react-query, inspired by relay and urql

A set of React hooks to work with GraphQL data. magiql stands on the shoulders of massive giants in the data-synchronization and state-management space, both conceputally and some as actual dependencies. It uses the amazing react-query library as its data-fetching and synchronization layer which forms the foundation of this library. Seriously, without react-query, this won't be any good. The API is also very similar to react-query and will be familiar to the users. It's just slightly tweaked to make it easier to work with GraphQL. On top of the these great synchronization primitives, we add normalized caching which allows us to enable the amazing developer experience that relay provides without any of the restrictions and buy-in required.

magiql is designed to be easy to adopt (moving from vanilla react-query or other GraphQL clients). Most features (like normalized caching, recoil-based store implementation, relay-compiler and build-time optimizations) are completely optional and configurable. But everything is included under one installed dependency so that you don't have to install things again and again (don't worry, the different entry-points are code-split and you will only include things that you actually use). You can start using magiql as a GraphQL Client immediately and it grows to cater to your needs as they arise by allowing extensive customization and incrementally adoption of more speciliazed features.

Features

Most of the features that we are providing are thanks to react-query. Using inspiration from relay and urql, we are able to provide a bunch of additional awesome GraphQL-specific features that you can find below

  • Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
  • Parallel + Dependent Queries
  • "Lazy" Queries
  • Request Deduplication
  • Paginated + Cursor-based Queries
  • Load-More + Infinite Scroll Queries w/ Scroll Recovery
  • Normalized caching + Entity Manipulation (multiple store implementations (recoil, react-query)
  • Fragment-first GraphQL approach (relay-style useFragment hook)
  • Request Cancellation
  • Mutations + Reactive Query Refetching
  • Optimistic Updates
  • Exchanges API to customize execution (logging, persisted queries, authentication, JWT token refresh)
  • React Suspense Support (must be enabled with React Concurrent Mode)
  • Dedicated Devtools

Using the relay-compiler (via magiql/babel or magiql cli) is required to unlock some more awesome features,

  • Schema-aware Normalized caching
  • Typescript Support + Code generation
  • Build-time GraphQL optimizations
  • Skip parsing at runtime

Note: You don't need a Relay-compliant server to get all these features in magiql. It will work with any GraphQL server. It also doesn't require you to commit to React Concurrent Mode which the new relay hooks require.

There is an example for this: https://magiql.vercel.app. You can see the components and pages folder for example code.

Warning: This is still in alpha stage and docs and examples are in the works

Documentation

Basic Usage
import {
  GraphQLClientProvider,
  GraphQLClient,
  useQuery,
  graphql,
} from "magiql";

const client = new GraphQLClient({
  endpoint: "https://swapi-graphql.netlify.app/.netlify/functions/index",
});

const People = () => {
  const { data, status, error } = useQuery(
    graphql`
      query PeopleQuery($limit: Int) {
        allPeople(first: $limit) {
          edges {
            node {
              id
              name
              homeworld {
                name
              }
            }
          }
        }
      }
    `,
    {
      variables: {
        limit: 10,
      },
    }
  );

  if (status === "loading") {
    return <div>Loading...</div>;
  }

  if (error) {
    return <div>{error.message}</div>;
  }

  return (
    <div>
      {data
        ? data.allPeople?.edges?.map((edge) => (
            <div key={edge.node.id}>
              <b>{edge.node.name}</b> ({edge.node.homeworld?.name})
            </div>
          ))
        : null}
    </div>
  );
};

const App = () => {
  return (
    <GraphQLClientProvider client={client}>
      <People />
    </GraphQLClientProvider>
  );
};
Installation

To install magiql with all its features to your project, run the following commands based on if you use yarn or npm. The single dependency includes multiple entry points to code-split features and not require user to install more dependencies.

yarn add magiql graphql

# or
npm install magiql graphql --save
Using the Relay compiler

This is required to use fragments and normalized caching

To use the relay-compiler, add magiql/babel to your Babel config as a plugin, eg. in babel.config.js. The magiql Babel plugin is just a wrapper around babel-plugin-relay to include everything in one dependency. It also runs the relay-compiler in watch mode by default.

module.exports {
  presets: [ ... ],
  plugins: ["magiql/babel", ... ]
}

Or, you can run the compiler from cli using the magiql command (use magiql --watch for watch mode, recommended for development). This is also just a wrapper around the relay-compiler. You still need to add the Babel plugin, but can disable running the compiler with Babel, but setting runWithBabel to false in magiql.config.js.

magiql.config.js

If need to customize the Relay compiler away from the defaults (specified below), add a magiql.config.js file in the root directory. It is very similar to relay.config.js, but tailored a little for magiql.

module.exports = {
  schema: "./schema.graphql",
  src: "./",
  artifactDirectory: "generated",
  extensions: ["ts", "tsx", "js", "jsx", "graphql"],
  quiet: false,
  watch: boolean,
  runWithBabel: true,
  language: "typescript",
  include: ["**"],
  exclude: [
      "**/node_modules/**",
      "**/__mocks__/**",
      `**/generated/**`,
    ];
 }
Working with fragments

With GraphQL, the biggest game changer when used with React are fragments. The useFragment hook introduced by relay makes it delightful to declare the data needs of your components. These are some of the advantages:

  • Date requirements completely localized and encapsulated in your component
  • Declarative, modular and composable
  • Fragments can include nest more fragments and fits naturally with the React component model
  • Don't need to add everything to the top level query
  • Easy to ensure type safety (using relay-compiler generated files)
  • Data available independent of how the data is fetched by some parent component
  • Components only subscribe to the precise part of the data store that it cares about (down to the field level).

Usage (with fragments)

// Person.tsx
import React from "react";
import { useFragment, graphql } from "magiql";
import { Person_person } from "generated/Person_person.graphql";

export function Person({ person }: { person: Person_person }) {
  const data = useFragment(
    graphql`
      fragment Person_person on Person {
        name
        homeworld {
          name
        }
      }
    `,
    person
  );

  return (
    <div>
      <b>{data.name}</b> ({data.homeworld?.name})
    </div>
  );
}
// People.tsx
import React from "react";
import { useQuery, graphql } from "magiql";
import { PeopleQuery } from "generated/PeopleQuery.graphql";
import { Person } from "./Person";

export const People = () => {
  const { data, status, error } = useQuery<PeopleQuery>(
    graphql`
      query PeopleQuery($limit: Int) {
        allPeople(first: $limit) {
          edges {
            node {
              id
              ...Person_person
            }
          }
        }
      }
    `,
    {
      variables: {
        limit: 10,
      },
    }
  );

  return (
    <div>
      {data
        ? data.allPeople?.edges?.map((edge) => <Person person={edge.node} />)
        : null}
    </div>
  );
};
import { GraphQLClientProvider, GraphQLClient } from "magiql";
import { createRecoilStore } from "magiql/recoil-store";
import { People } from "./People";

const client = new GraphQLClient({
  endpoint: "https://swapi-graphql.netlify.app/.netlify/functions/index",
  useStore: createRecoilStore(),
});

const App = () => {
  return (
    <GraphQLClientProvider client={client}>
      <People />
    </GraphQLClientProvider>
  );
};

These features and accompanying restrictions provide an excellent authoring experience that almost seems magical when it works.

Typescript Support

Using the Relay compiler, magiql can generate types for all your operations since it has access to your schema as well. These types are generated and updated by the compiler, so ensure that it's running in watch mode (either through Babel or the cli) when you are developing.

If the name of query is HomeQuery, then import type as such:

import { HomeQuery } from "generated/HomeQuery.graphql";
import { useQuery } from "magiql";

const { data, error } = useQuery<HomeQuery>(graphql`
  query HomeQuery {
    currentHome {
      name
    }
  }
`);
  • Types are imported from the folder specified as artifactDirectory in magiql.config.js (Default: generated).
  • Typescript support is enabled by default. To disable it, set language to javascript in magiql.config.js.
  • If not using the compiler, you can provide type parameters to each operation with the following sample signature
type HomeQuery = {
  response: {
    currentHome: {
      name: string;
    };
  };
  variables: {};
};
Customizing the GraphQL Client

Coming soon

Exchanges

Coming soon

Normalized caching

To fully unlock fragments, including optimistic responses and cache manipulation of entities, we needed a normalized cache of our data. We call this cache, the store in magiql.

  • Each entity is identified and stored once.
  • Component that access entities subscribe to changes to that entity
  • Implementation can be customized when creating a GraphQLClient via the useStore option, we provide three implementations of our own (using recoil and react-query's QueryCache)
  • Provide your own getDataID to these stores to control how id's are determined and then let magiql handle the rest for managing access.
import { GraphQLClient } from "magiql";
import { createRecoilStore } from "magiql/recoil-store";

const client = new GraphQLClient({
  endpoint: "...",
  useStore: createRecoilStore({
    // optional, by default it uses the `id` field if available otherwise falls back to an unnormalized id
    // this is the default function
    getDataID: (record, type) => (record.id ? `${type}:${record.id}` : null),
  }),
});

Store Implementations

  • Recoil createRecoilStore
    • Recommended if already working with the compiler and the Babel plugin
    • Each field of an entity is stored as atom, entities and fragments are both selectors on the atoms
    • Components subscribe to fields on entities (very granular and precise)
    • Customize how to determine id for each entity
  • React Query's QueryCache as store createNormalizedQueryCacheStore
    • Each entity is a query with the entity's id as the key
    • Components subscribe to entities (not field-level subscriptions)
    • Same API as createRecoilStore
  • React Query's QueryCache (unnormalized) createQueryCacheStore
    • Client's QueryCache stores data attached to queries, and doesnt identify entities
    • Doesn't allow cache manipulation with entities
    • No options required since doesn't actually normally, but will still work with Fragments
Naming convention for operations

Relay allows us to use fragments in queries and mutations without importing them as modules. For this to work, the names must be globally unique. It is also good habit to name the fragments and queries based on the components and props that use them. Thus, relay enforces a few conventions when it comes to naming your operations. These conventions are quite helpful and make your lives easier:

  • Queries must be named query ${ModuleName}Query { ... }, eg, a query in file Home.tsx can be named HomeQuery or HomeRoomsQuery
  • Mutations must be named mutation ${ModuleName}Mutation { ... }, eg, a mutation in file Home.tsx can be named HomeMutation or HomeDestroyMutation
  • Fragments must be named fragment ${ModuleName}_${propName} on type { ... }, eg, a fragment in file HomeDetails.tsx where the prop for the fragment ref is home can be named HomeDetails_home
Devtools

You can use the magiql Devtools which are inspired by react-query-devtools as follows:

import React from "react";
import { GraphQLClient, GraphQLClientProvider } from "magiql";
import GraphQLDevtools from "magiql/devtools";

export default function App({ children }) {
  return (
    <GraphQLClientProvider client={client}>
      {children}
      <GraphQLDevtools defaultIsOpen defaultTab="store" />
    </GraphQLClientProvider>
  );
}

API

The following is the core API for magiql. With the help of amazing libraries like react-query, relay-compiler and recoil, we are able to provide the following features as a GraphQL client. The runtime is your familiar react-query api. There is also an optional build time setup that unlocks fragments and normalized store.

useQuery

You can use the magiql Devtools which are inspired by react-query-devtools as follows:

import React from "react";
import { GraphQLClient, GraphQLClientProvider } from "magiql";
import GraphQLDevtools from "magiql/devtools";

export default function App({ children }) {
  return (
    <GraphQLClientProvider client={client}>
      {children}
      <GraphQLDevtools defaultIsOpen defaultTab="store" />
    </GraphQLClientProvider>
  );
}

Runtime

  • useQuery(query, { variables, ...options }), usePaginatedQuery and useInfiniteQuery: Data fetching patterns for GraphQL queries with numerous ways to customize them for refetching on different events
    • All options from react-query are valid in the second argument
    • No fetch function or query key required
    • Stale-while-revalidate caching strategy
    • Request deduplication
    • Infinite queries
    • Paginated queries
    • Parallel and dependent queries
    • Lazy queries
    • Window Focus refetching
    • Network Status refetching
    • Polling/interval refetching
    • Normalized caching (uses relay-compiler)
  • useMutation: a hook that provides a way to run GraphQL mutations (updates) on the server state conviniently with optmistic updates from your React app
    • Optimistic updates
    • Cache manipulation for entities in store
  • useFragment: a hook that allows you to properly encapsulate the date requirements of a component within it by subscribing to a GraphQL Fragment which resolves when provided a ref from the parent component via props
    • Field-level subscription for fragments, only rerenders when that changes
    • Allows nesting of fragments, great deal of composability and reusability
    • Doesn't need to be responsible for fetching data (gets reference from parent component)
    • Requires that you run the relay-compiler with either the Babel plugin or the cli command.
  • useGraphQLClient: Access the underlying GraphQLClient
    • Create new client using GraphQLClient class
    • Add the GraphQLClientProvider with an instance of a GraphQLClient
    • Can customize react-query config by using `new GraphQLClient({ endpoint: "...", queryConfig: {...} })
  • Exchanges based API for customizing query execution behaviour, allows for,
    • logging, authentication, refreshing tokens, normalization, error handling
  • React Suspense support
  • React Native support
    • Should work out of the box in expo or wherever the react-native package.json property is resolved

Foundation and inspirations

Here are some of the big dependencies and inspirations for magiql:

  • react-query

    • Data-fetching (network) layer
    • Stale-while-revalidate caching strategy
    • Request deduplication
    • Window Focus refetching
    • Network Status refetching
    • Infinite queries
    • Paginated queries
    • Parallel and dependent queries
    • Lazy queries
    • Polling/interval refetching
    • React Suspense support
    • Normalized caching (with the help of the relay compiler)
    • Differences:
      • No query key required (GraphQL document name + variables form the key)
      • No query fetch function required (provided by the client with customization options)
      • Pass GraphQL document as first argument
  • relay-compiler

    • Build time optimizations (flatten fragments, add id fields, etc.)
    • Code-generation for types (for full typescript support)
    • Using fragments effectively with optimizations
    • Concept: useFragment hook (game changer!) to declaratively define data needs for components independent of the fetching of the data
    • Implementation: relay-runtime inspiration for (de)normalizating data
    • magiql allows us to use the relay hooks API without jumping to React Suspense (can't use the new relay hooks without that)
    • Do not need relay compliant server (will work with any GraphQL server)
  • recoil (opt-in)

    • Alternative implementation for normalized cache for data
    • Granular subscription (field-level) to data for fragments and queries based on exactly what they ask
    • similar implementation for jotai is also being worked on
  • urql (inspiration)

    • Concept: exchange API to customize execution of graphql request
    • Allowed easy ways to add logging, persisted queries, auth (with token refresh support)

More Repositories

1

vinxi

The Full Stack JavaScript SDK
JavaScript
2,062
star
2

use-monaco

Use ๐Ÿ—’๏ธ monaco-editor in any โš›๏ธ React app with simple hooks ๐ŸŽฃ
TypeScript
101
star
3

vite-next

NextJS alternative built on top of Vite, React Router, React Query
TypeScript
46
star
4

magiql-browser

Web-based Browser and IDE for GraphQL (based on monaco-editor), useful for playgrounds for GraphQL servers
TypeScript
28
star
5

fully-react

TypeScript
26
star
6

lepton-footprint-extraction

Automated building polygon extraction from satellite imagery
Python
15
star
7

solid-mdx

MDX support for SolidJS
TypeScript
14
star
8

editable-jsx

TypeScript
9
star
9

scoreboard-api

Live NBA scores and boxscores API (data scraped from ESPN website)
Python
6
star
10

game-tools

TypeScript
6
star
11

excalidraw-studio

TypeScript
5
star
12

solid-three

TypeScript
4
star
13

react-box

A simple yet super powerful Box primitive (with others) for React powered by styled-system and framer-motion
TypeScript
4
star
14

vinxi-island

TypeScript
3
star
15

pkger

๐Ÿ“ฆ Simple (yet powerful) build tool for any target (browser/node/cli) โš™ Powered by rollup, babel, gluegun, typescript
TypeScript
3
star
16

create-hook-context

A better and more powerful React.createContext powered by hooks ๐ŸŽฃ
TypeScript
2
star
17

scoreboard

Desktop App for NBA live scores and boxscores
JavaScript
2
star
18

awesome-rsc

2
star
19

vinxi-trpc

JavaScript
2
star
20

elf

YAML syntax-based task runner
TypeScript
1
star
21

ocean-theme

๐ŸŒŠOceanic theme for VS Code
JavaScript
1
star
22

todo-wallpaper

โ˜‘๏ธ Todo App for MacOS tray with auto-updating desktop wallpaper
JavaScript
1
star
23

tanstack-router-app

HTML
1
star
24

solid-starter

Created with StackBlitz โšก๏ธ
TypeScript
1
star
25

tavern

Super simple framework for service-based architecture in monolithic servers
TypeScript
1
star
26

next-monaco-editor

JavaScript
1
star
27

test-solid-6

TypeScript
1
star
28

game-buildings

TypeScript
1
star
29

map-gsheets

TypeScript
1
star
30

examples

An example project showing how the <TLDraw/> component can be used and controlled in a React app.
TypeScript
1
star
31

sapiens

TypeScript
1
star
32

vite-react-starter

TypeScript
1
star
33

playground

TypeScript
1
star
34

vitap

The Vite App framework
JavaScript
1
star
35

reactscript

JavaScript
1
star
36

nba-stats

Java wrapper for NBA Stats API
Java
1
star
37

malaria-index

TypeScript
1
star
38

bluwy-whyframe-vq5xek

Created with StackBlitz โšก๏ธ
CSS
1
star
39

solid-start-hybrid

TypeScript
1
star
40

game-editor-monorepo

TypeScript
1
star