All Projects β†’ lucasconstantino β†’ react-apollo-fragments

lucasconstantino / react-apollo-fragments

Licence: MIT license
True Fragment component for react-apollo

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to react-apollo-fragments

recompose-apollo
Recompose HOCs for React Apollo
Stars: ✭ 36 (+176.92%)
Mutual labels:  apollo-client, react-apollo
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 (+11923.08%)
Mutual labels:  apollo-client, react-apollo
apollo-progressive-fragment-matcher
A smart alternative to the introspection fragment matcher.
Stars: ✭ 21 (+61.54%)
Mutual labels:  fragments, apollo-client
nap
[Deprecated] NextJS + Apollo + PassportJS
Stars: ✭ 52 (+300%)
Mutual labels:  apollo-client
grunt-angular-combine
Grunt task for combining AngularJS partials into a single HTML file.
Stars: ✭ 16 (+23.08%)
Mutual labels:  fragments
navigator
Annotation processor that eliminates navigation and Bundle boilerplate
Stars: ✭ 13 (+0%)
Mutual labels:  fragments
RxApolloClient
RxSwift extensions for Apollo Client
Stars: ✭ 46 (+253.85%)
Mutual labels:  apollo-client
matters-web
Website of Matters.News, built with Next.js.
Stars: ✭ 70 (+438.46%)
Mutual labels:  apollo-client
react-native-appsync-s3
React Native app for image uploads to S3 and storing their records in Amazon DynamoDB using AWS Amplify and AppSync SDK
Stars: ✭ 18 (+38.46%)
Mutual labels:  apollo-client
apollo-graphql-full-stack
Full-stack Apollo GraphQL app using React and Node JS
Stars: ✭ 31 (+138.46%)
Mutual labels:  apollo-client
pokedex-react
A Pokedex App using and teaching Apollo and React
Stars: ✭ 47 (+261.54%)
Mutual labels:  apollo-client
Easy-Fragment-Argument
This library will help you to pass and receive fragment arguments in easier way
Stars: ✭ 17 (+30.77%)
Mutual labels:  fragments
vscode-graphiql-explorer
Use GraphiQL + GraphiQL Explorer to build your GraphQL operations, right from inside of VSCode.
Stars: ✭ 35 (+169.23%)
Mutual labels:  apollo-client
react-native-feed
News feed built with react-native, apollo-client and graphql & powered by postgraphql
Stars: ✭ 99 (+661.54%)
Mutual labels:  apollo-client
news-fragments
An easy way to create your changelog file
Stars: ✭ 31 (+138.46%)
Mutual labels:  fragments
boilerplate
Boilerplate for @prisma-cms
Stars: ✭ 22 (+69.23%)
Mutual labels:  apollo-client
lifemanager
⏱ ν•œ 일을 κΈ°λ‘ν•˜λ©΄ μ‹œκ°ν™” ν•΄μ„œ λ³΄μ—¬μ£ΌλŠ” μ›Ή 앱⏱
Stars: ✭ 85 (+553.85%)
Mutual labels:  apollo-client
apollo-fragment
Use Apollo Link State to connect components to GraphQL fragments in the Apollo Cache
Stars: ✭ 112 (+761.54%)
Mutual labels:  apollo-client
Pursuit-Core-Android
Pursuit Core Android
Stars: ✭ 45 (+246.15%)
Mutual labels:  fragments
devto-monorepo
Source code for the Dev.to article - Next.js, Apollo Client and Server on a single Express app
Stars: ✭ 33 (+153.85%)
Mutual labels:  apollo-client

React Apollo Fragments

True Fragment component for react-apollo

Build status

⚠️ This package is not ready for production, and some functionality depends on a pull-request to graphql-tag

Installation

npm install react-apollo-fragments

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

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 fragments be declared in it's parent component 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 knowledge on what fragments exactly - and many times sub-fragments - are in use down the rendering tree.

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, avoid having to define too many inner-queries to keep them decouple, and use GraphQL for the task it was meant to be used: walking through a graph.

How does it work

This project exposes a Fragment component and a substitute Query component. When a Fragment component is rendered down the tree, it will automatically present it's fragment to the parent Query component, which will then update it's query to contain the provided fragment.

The Fragment component is a render prop based component which will provide it's children with the same query result as the Query component provides. You can also provide an id prop to Fragment, which will result in that fragment's data being provided as data prop on the Fragment's children.

Usage

Say you have the following type:

type User {
  id
  name
  surname
  photo
  age
}

... and you have an Avatar fragment:

# ./avatar.gql
fragment Avatar on User {
  name
  photo
}

... which is consumed in the following user listing query:

# ./user-list.gql
query List {
  users {
    id
    ...Avatar
  }
}

... then the fragment can be used in a component Avatar as follows:

import { Fragment } from 'react-apollo-fragments'

import avatarFragment from './avatar.gql'

const Avatar = ({ user: { name, photo } }) => (
  <Fragment fragment={ avatarFragment }>
    { () => (
      <div>
        <img src={ photo } alt={ name } />
        <h3>{ name }</h3>
      </div>
    ) }
  </Fragment>
)

... and the query can be executed as in:

import { Query } from 'react-apollo-fragments'

import userListQuery from './user-list.gql'
import Avatar from './Avatar'

const Page = () => (
  <div>
    <h1>List of Users</h1>
    <Query query={ userListQuery }>
      { ({ data: { users = [] } }) => (
        <ul>
          { users.map(user => (
            <Avatar key={ user.id } user={ user } />
          )) }
        </ul>
      ) }
    </Query>
  </div>
)

... and that's it! No more direct fragment usage on the querying component :)

Fragment data injection

The Fragment can also be smarter and provide you with the fragment's data. For that to happen, all you have to do is provide the component with and id, which must match the one retrieve via getDataIdFromObject.

The Avatar component could be updated to the following:

import { Fragment } from 'react-apollo-fragments'

import avatarFragment from './avatar.gql'

const Avatar = ({ user }) => (
  <Fragment fragment={ avatarFragment } id={ getDataIdFromObject(user) }>
    { ({ data: { name, photo }, loading }) => (
      <div>
        <img src={ photo } alt={ name } />
        <h3>{ name }</h3>
      </div>
    ) }
  </Fragment>
)

This package should be temporary

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