Giter Site home page Giter Site logo

apollo-query-resource's Introduction

Apollo Query Result

Proof of concept implementation of Suspense for Data Fetching for Apollo.

WARNING! This is not a real package (yet)!

Implementation Details

Check out the useQueryResource.

First, we assign a unique ID to every query. By default, it is an operation name, but it can be configured explicitly. Then, we create an ObservableQuery and cache it in a Suspense Cache. Every ID corresponds to exactly one ObservableQuery.

const observableQuert = useObservableQuery(document, { variables });

Next, we subscribe to ObservableQuery updates using new useSyncExternalStore API. Similar to the standard useQuery.

const { loading, data, networkError, graphQLErrors } = useObservableQueryState(observableQuery);

To suspend ObservableQuery we have a helper that suspends query while loading is true. It also uses Suspense Cache to maintain a map between ObservableQuery and a suspension `Promises.

settleObservableQuery(observableQuery); // Suspends!

Finally, we wrap everything together into a QueryResource.

const resource = useQueryResource(document, { variables });

resource.read(); // Suspends!

// You can also work with data as usual
resource.isLoading();
resource.error();

Example

import { gql } from "@apollo/client";
import { useQueryResource } from "@hired/apollo-query-resource";

const GetPostQuery = gql`
  query GetPost($postId: Int!) {
    post(id: $postId) {
      id
      title
      body
    }
  }
`;

interface GetPostResult {
  post: {
    id: number;
    title: string;
    body: string;
  }
}

interface interface GetPostVariables {
  postId: number;
}

export const Post({ postId }: { postId: number }) {
  const resource = useQueryResource<GetPostResult, GetPostVariables>(GetPostQuery, { variables: { postId } })

  // No errors, no loading, just data!
  const { post: { title, body }} = resource.read(); // Suspends!

  return (
    <>
      <h1>{title}</h1>
      <p>{body}</p>
    <>
  );
}

Requirements

Currently only works with React 18 experimental.

apollo-query-resource's People

Contributors

ddiachkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.