All Projects → lucasconstantino → React Apollo Defragment

lucasconstantino / React Apollo Defragment

Licence: mit
💿 Automatic query defragmentation based on React trees.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Apollo Defragment

React Apollo Decorators
Better decorators for Apollo and React
Stars: ✭ 39 (+178.57%)
Mutual labels:  graphql, apollo, fragments
Js Graphql Intellij Plugin
GraphQL language support for WebStorm, IntelliJ IDEA and other IDEs based on the IntelliJ Platform.
Stars: ✭ 686 (+4800%)
Mutual labels:  graphql, apollo
Pizzaql
🍕 Modern OSS Order Management System for Pizza Restaurants
Stars: ✭ 631 (+4407.14%)
Mutual labels:  graphql, apollo
Graphql
GraphQL (TypeScript) module for Nest framework (node.js) 🍷
Stars: ✭ 697 (+4878.57%)
Mutual labels:  graphql, apollo
Apollo Fetch
🐶 Lightweight GraphQL client that supports middleware and afterware
Stars: ✭ 581 (+4050%)
Mutual labels:  graphql, apollo
Learn Graphql
Real world GraphQL tutorials for frontend developers with deadlines!
Stars: ✭ 586 (+4085.71%)
Mutual labels:  graphql, apollo
React Apollo Koa Example
An example app using React, Apollo and Koa
Stars: ✭ 26 (+85.71%)
Mutual labels:  graphql, apollo
Chatty
A WhatsApp clone with React Native and Apollo (Tutorial)
Stars: ✭ 481 (+3335.71%)
Mutual labels:  graphql, apollo
Chimp
Tooling that helps you do quality, faster.
Stars: ✭ 783 (+5492.86%)
Mutual labels:  graphql, apollo
Ionic Apollo Simple App
Explains how to develop Ionic application with Apollo GraphQL client
Stars: ✭ 16 (+14.29%)
Mutual labels:  graphql, apollo
Graphql React Apollo
A GraphQL implementation in React using Apollo.
Stars: ✭ 9 (-35.71%)
Mutual labels:  graphql, apollo
Pup
The Ultimate Boilerplate for Products.
Stars: ✭ 563 (+3921.43%)
Mutual labels:  graphql, apollo
Brian Lovin Next
My personal site
Stars: ✭ 522 (+3628.57%)
Mutual labels:  graphql, apollo
Learnapollo
👩🏻‍🏫 Learn Apollo - A hands-on tutorial for Apollo GraphQL Client (created by Graphcool)
Stars: ✭ 5,274 (+37571.43%)
Mutual labels:  graphql, apollo
Animavita
Trigger life-saving alerts, register animals for adoption and find the closest pet friend to adopt 🐶
Stars: ✭ 508 (+3528.57%)
Mutual labels:  graphql, apollo
Offix
GraphQL Offline Client and Server
Stars: ✭ 694 (+4857.14%)
Mutual labels:  graphql, apollo
React Apollo
♻️ React integration for Apollo Client
Stars: ✭ 6,932 (+49414.29%)
Mutual labels:  graphql, apollo
Graphql Crunch
Reduces the size of GraphQL responses by consolidating duplicate values
Stars: ✭ 472 (+3271.43%)
Mutual labels:  graphql, apollo
Learning Graphql
The code samples for Learning GraphQL by Eve Porcello and Alex Banks, published by O'Reilly Media
Stars: ✭ 477 (+3307.14%)
Mutual labels:  graphql, apollo
Storefront Api Examples
Example custom storefront applications built on Shopify's Storefront API
Stars: ✭ 769 (+5392.86%)
Mutual labels:  graphql, apollo

React Apollo Defragment

Automatic query defragmentation based on React trees.

Build status

⚠️ This is experimental software, and though there are plenty of unit tests you should probably avoid using on production code

Installation

npm install react-apollo-defragment

react-apollo-defragment has peer dependency on react, react-apollo, graphql, and prop-types. Make sure to have them installed.

Motivation

When using fragments one thing that bothers me is that we lose some of the decoupling between child and parent components. When the fragment is used by a first-level child using fragment as we currently do is not a big problem; you are already declaring the use of the child component via importing it on the parent, after all. But when the fragment is used way below down the tree, it just becomes odd to have the querying component have so much logic on what fragments exactly - and many times sub-fragments - must be in use.

There was already some discussion on fragment composition, but the proposals did not went forward.

What I needed was some way to decouple my components once more, and avoid having to define too many inner-queries to solve something that GraphQL should be used for: walking through a graph.

How does it work

This project exposes a substitute Query component which traverses the underlying React element tree to find the fragments in use in the query. The components must expose fragments on the static property fragment for this to work.

Usage

The usage is the same as with react-apollo's Query component, only in this case no fragments must be imported and declared from the component:

import { Query } from 'react-apollo-defragment'
import gql from 'graphql-tag'

// PersonAvatar.js: a component using fragments.

const PersonAvatar = ({ photo, name }) => (
  <div>
    <img src={ photo } alt={ name } />
    <h3>{ name }</h3>
  </div>
)

PersonAvatar.fragment = gql`
  fragment Avatar on Person {
    name
    photo
  }
`

// People.js: a component querying people and displaying avatars.

const query = gql`
  query People {
    people {
      id
      ...Avatar
    }
  }
`

const People = () => (
  <Query>
    { ({ loading, data: { people } }) => (
      !loading && (
        <ul>
          { people.map(person => (
            <li key={ person.id }>
              <PersonAvatar { ...person } />
            </li>
          )) }
        </ul>
      )
    ) }
  </Query>
)

This package should be temporary

I believe something similar to what is accomplished by this package should be soon implemented on the React Apollo core. If someday that happens, this package will either be deprecated or hold other experimental functionality on the subject of GraphQL fragments with Apollo and React.

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].