All Projects → abhiaiyer91 → apollo-fragment

abhiaiyer91 / apollo-fragment

Licence: other
Use Apollo Link State to connect components to GraphQL fragments in the Apollo Cache

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
Vue
7211 projects
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to apollo-fragment

Apollo Php Client
携程Apollo配置中心PHP客户端
Stars: ✭ 147 (+31.25%)
Mutual labels:  apollo, apollo-client
Agollo
An elegant Go client for Ctrip Apollo
Stars: ✭ 167 (+49.11%)
Mutual labels:  apollo, apollo-client
Reactql
Universal React+GraphQL starter kit: React 16, Apollo 2, MobX, Emotion, Webpack 4, GraphQL Code Generator, React Router 4, PostCSS, SSR
Stars: ✭ 1,833 (+1536.61%)
Mutual labels:  apollo, apollo-client
GitHunt-Polymer
An example of a client-side app built with Polymer and Apollo Client.
Stars: ✭ 13 (-88.39%)
Mutual labels:  apollo, apollo-client
nap
[Deprecated] NextJS + Apollo + PassportJS
Stars: ✭ 52 (-53.57%)
Mutual labels:  apollo, apollo-client
React Redux Graphql Apollo Bootstrap Webpack Starter
react js + redux + graphQL + Apollo + react router + hot reload + devTools + bootstrap + webpack starter
Stars: ✭ 127 (+13.39%)
Mutual labels:  apollo, apollo-client
Ddp Apollo
DDP link for Apollo with GraphQL Subscriptions support
Stars: ✭ 163 (+45.54%)
Mutual labels:  apollo, apollo-client
Guide
📖 The GraphQL Guide website
Stars: ✭ 104 (-7.14%)
Mutual labels:  apollo, apollo-client
ctrip-apollo
The most delightful and handy Node.js client for ctrip apollo configuration service.
Stars: ✭ 56 (-50%)
Mutual labels:  apollo, apollo-client
Kit
ReactQL starter kit (use the CLI)
Stars: ✭ 232 (+107.14%)
Mutual labels:  apollo, apollo-client
Awesome Apollo Graphql
A curated list of amazingly awesome things regarding Apollo GraphQL ecosystem 🌟
Stars: ✭ 126 (+12.5%)
Mutual labels:  apollo, apollo-client
boilerplate
Boilerplate for @prisma-cms
Stars: ✭ 22 (-80.36%)
Mutual labels:  apollo, apollo-client
React Graphql Github Apollo
🚀 A React + Apollo + GraphQL GitHub Client. Your opportunity to learn about these technologies in a real world application.
Stars: ✭ 1,563 (+1295.54%)
Mutual labels:  apollo, apollo-client
Graphql Directive
Use custom directives in your GraphQL schema and queries 🎩
Stars: ✭ 142 (+26.79%)
Mutual labels:  apollo, apollo-client
Apollo Link
🔗 Interface for fetching and modifying control flow of GraphQL requests
Stars: ✭ 1,434 (+1180.36%)
Mutual labels:  apollo, apollo-client
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (+35.71%)
Mutual labels:  apollo, apollo-client
Apollo Upload Client
A terminating Apollo Link for Apollo Client that allows FileList, File, Blob or ReactNativeFile instances within query or mutation variables and sends GraphQL multipart requests.
Stars: ✭ 1,176 (+950%)
Mutual labels:  apollo, apollo-client
Polymer Apollo
🚀 Polymer Apollo Integration
Stars: ✭ 77 (-31.25%)
Mutual labels:  apollo, apollo-client
Apollo Cache Redux
Redux cache for Apollo Client 2.0. This project is no longer maintained.
Stars: ✭ 179 (+59.82%)
Mutual labels:  apollo, apollo-client
bookshelf
My GraphQL playground
Stars: ✭ 64 (-42.86%)
Mutual labels:  apollo, apollo-client

Apollo Fragment

Apollo Fragment holds libraries aimed at connecting UI components to GraphQL fragments in the Apollo Cache.

apollo-link-state-fragment exposes a cacheRedirect and withClientState configuration for querying fragments from the cache.

apollo-fragment-react exposes an ApolloFragment query component that will connect your component to a fragment in cache and automatically watch all changes to it.

apollo-fragment-vue exposes an ApolloFragment Vue component that will connect your component to a fragment in cache and automatically watch all changes to it.

Background

Read about this library here: https://medium.com/open-graphql/fragment-driven-uis-with-apollo-17d933fa1cbe

React

Npm download

Vue

Npm download

Link State

Npm download

Installation

It is simple to add to your current apollo client setup:

# installing cache addons and react package
yarn add apollo-link-state-fragment apollo-fragment-react -S

or

# installing cache addons and react package
yarn add apollo-link-state-fragment apollo-fragment-vue -S

Usage

To get started you will want to add the fragmentCacheRedirect to your InMemoryCache cacheRedirect map.

import { InMemoryCache } from "apollo-cache-inmemory";
import { fragmentCacheRedirect } from "apollo-link-state-fragment";

const cache = new InMemoryCache({
  cacheRedirects: {
    Query: {
      ...fragmentCacheRedirect(),
    },
  },
});

Next, import the fragmentLinkState and pass in the cache.

import { ApolloClient } from "apollo-client";
import { ApolloLink } from "apollo-link";
import { InMemoryCache } from "apollo-cache-inmemory";
import {
  fragmentCacheRedirect,
  fragmentLinkState,
} from "apollo-link-state-fragment";

const cache = new InMemoryCache({
  cacheRedirects: {
    Query: {
      ...fragmentCacheRedirect(),
    },
  },
});

const client = new ApolloClient({
  link: ApolloLink.from([fragmentLinkState(cache), new HttpLink()]),
  cache: new InMemoryCache(),
});

Once you have your client setup to make these kind of queries against the cache, we can now use the View layer integrations: All we have to do is pass the id of the fragment you're looking for, and the selection set in a named fragment.

React

import { useApolloFragment } from "apollo-fragment-react";

const fragment = `
  fragment fragmentFields on Person {
    idea
    name
    __typename
  }
`;

function App() {
  const { data } = useApolloFragment(fragment, "1");

  return (
    <section>
      <p>
        This component is "watching" a fragment in the cache, it will render the
        persons name once the data enters
      </p>
      <p>{data && `Person Name: ${data.name || ""}`}</p>

      <button
        onClick={function() {
          client.query({
            query: gql`
              query peeps {
                people {
                  id
                  name
                }
              }
            `,
          });
        }}
      >
        Click to load people
      </button>
    </section>
  );
}

Vue

<template>
  <div>
    <p>
      This list is created by calling a GraphQL Fragment with ApolloFragment
    </p>
    <ApolloFragment :fragment="fragment" :id="id">
      <template slot-scope="{ result: { loading, error, data } }">
        <div v-if="loading" class="loading apollo">Loading...</div>

        <!-- Error -->
        <div v-else-if="error" class="error apollo">An error occured</div>

        <!-- Result -->
        <div v-else-if="data" class="result apollo">
          <p>ID: {{data.id}} - {{data.name}}</p>
        </div>

        <!-- No result -->
        <div v-else class="no-result apollo">
          <p>No result :(</p>
        </div>
      </template>
    </ApolloFragment>
  </div>
</template>

<script>
  const fragment = `
  fragment fragmentFields on Person {
    idea
    name
    __typename
  }
`;

  export default {
    name: "Example",
    data() {
      return {
        id: "1",
        fragment,
      };
    },
  };
</script>

In our examples above, We have used the ApolloFragment query component to bind the current value of the fragment in cache. When a user clicks to load a list of people, our component subscribed to a user with id "1", will rerender and display the person's name.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].