All Projects → kennetpostigo → React Reach

kennetpostigo / React Reach

A small library for React to communicate with GraphQL

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Reach

Frontend
🌏 The front-end application code for https://buildkite.com
Stars: ✭ 132 (-3.65%)
Mutual labels:  graphql
Saleor
A modular, high performance, headless e-commerce platform built with Python, GraphQL, Django, and React.
Stars: ✭ 14,720 (+10644.53%)
Mutual labels:  graphql
Schema Stitching Handbook
Guided examples exploring GraphQL Tools v6+ Schema Stitching
Stars: ✭ 137 (+0%)
Mutual labels:  graphql
Python Graphql Client
Simple GraphQL client for Python 2.7+
Stars: ✭ 133 (-2.92%)
Mutual labels:  graphql
Graphql Compose Examples
Live examples of schemas builded with graphql-compose
Stars: ✭ 134 (-2.19%)
Mutual labels:  graphql
Graphql Api For Wp
[READ ONLY] GraphQL API for WordPress
Stars: ✭ 136 (-0.73%)
Mutual labels:  graphql
Gatsby Blog Starter Kit
A simple starter kit for a static blog created with Gatsby
Stars: ✭ 131 (-4.38%)
Mutual labels:  graphql
Graphql Compose Aws
AWS Cloud API via GraphQL
Stars: ✭ 137 (+0%)
Mutual labels:  graphql
Gatsby Starter Foundation
A starter to launch your blazing fast personal website and a blog, Built with Gatsby and Netlify CMS. Made with ❤ by Stackrole
Stars: ✭ 135 (-1.46%)
Mutual labels:  graphql
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-0.73%)
Mutual labels:  graphql
Webspace
This is the repo which hosts the front end for people.amfoss.in
Stars: ✭ 134 (-2.19%)
Mutual labels:  graphql
Graphql Mqtt Subscriptions
graphql-subscriptions implementation for MQTT protocol
Stars: ✭ 133 (-2.92%)
Mutual labels:  graphql
Graphql Cli
📟 Command line tool for common GraphQL development workflows
Stars: ✭ 1,814 (+1224.09%)
Mutual labels:  graphql
Graphql Postgres Subscriptions
A graphql subscriptions implementation using postgres and apollo's graphql-subscriptions
Stars: ✭ 133 (-2.92%)
Mutual labels:  graphql
Graphql Recipes
A list of GraphQL recipes that, when used with the Amplify CLI, will deploy an entire AWS AppSync GraphQL backend.
Stars: ✭ 137 (+0%)
Mutual labels:  graphql
Serene
Generate clojure.spec with GraphQL and extend GraphQL with clojure.spec
Stars: ✭ 132 (-3.65%)
Mutual labels:  graphql
Subzero Starter Kit
Starter Kit and tooling for authoring GraphQL/REST API backends with subZero
Stars: ✭ 136 (-0.73%)
Mutual labels:  graphql
Basic Shopify Api
A simple API wrapper for Shopify using Guzzle for REST and GraphQL
Stars: ✭ 137 (+0%)
Mutual labels:  graphql
Reason Graphql
GraphQL server in pure Reason (Bucklescript)
Stars: ✭ 137 (+0%)
Mutual labels:  graphql
Graphql Go Example
Example GraphQL API implemented in Go and backed by Postgresql
Stars: ✭ 135 (-1.46%)
Mutual labels:  graphql

react-reach

A small library for react to communicate with graphQL

version downloads MIT License

react-reach helps you write applications that use tools you are familiar with from the react ecosystem. react-reach is designed to be used along side redux and react. React-reach works as the layer that handles communication of data between React and graphQL. Reach enables developers to make queries and mutations against GraphQL.

The need for react-reach

When developing applications with react everything goes smoothly until you begin to account for request to the server. Usually you would go about making network request to specified endpoints with REST, or networks request to "/graphql" if you use GraphQL. Now Relay may come to mind, and what makes reach different is that it only does one thing. You can use reach along Redux.

Features are a Work In Progress

  • [x] Talk to a GraphQL server
  • [x] Cache responses in Redux store
  • [ ] Optimistic Updates
  • [ ] Create Example Application
  • [ ] Create Blog Post with Explanations
  • [x] UMD Build
  • [x] When used with react-router dynamically request data needed onEnter & onLeave Hooks

Developer Guide

  • Need to create a blog post to demonstrate how to use it in a sample application

Usage

npm install --save react-reach

react-reach makes fetching data from a GraphQL server as easy as this:

//reachGraphQL() to make a query for some data, or add mutation
//I created this function with for simply communicating back and forth with graphQL
//params: reachGraphQL(url, query/mutation, queryParameters)
import React from 'react';
import reactDOM from 'react-dom';
import {reachGraphQL} from 'react-reach';

class App extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      shipName: ''
    };
  };

  getAllStarShips () {
    reachGraphQL('http://localhost:4000/', `{
     allStarships(first: 7) {
       edges {
         node {
           id
           name
           model
           costInCredits
           pilotConnection {
             edges {
               node {
                 ...pilotFragment
               }
             }
           }
         }
       }
     }
    }
    fragment pilotFragment on Person {
     name
     homeworld { name }
   }`, {})
   .then((data) => {
      this.setState({
        shipName: data.allStarships.edges[1].node.name
      });
   });
  }

  componentDidMount() {
    this.getAllStarShips();
  }

  render() {
    console.log('state:',JSON.stringify(this.state.shipName, null, 2));
    return (
      <div>
        <h1>React-Reach!</h1>
        <p>{this.state.shipName}</p>
      </div>
    );
  }
}

reactDOM.render(
  <App></App>,
  document.getElementById('app')
);

//reachWithDispatch() to make a query and dispatch actionCreator passed in
//I created this function for receiving data from the server and automatically caching it in the redux store.
//To be used with redux-thunk or any similar middleware.
//params: reachWithDispatch(url, query/mutation, queryParameters, actionCreator)

import { reachWithDispatch } from 'react-reach';

reachWithDispatch('localhost:1000', `{
    user(id: "1") {
        name
    }
}`, {}, somePredefinedActionCreator);


//reachWithOpts() to make a query and dispatch specified actionCreators from an Object  passed in
//I created this function for sending data to the server and optimistically updating the redux store client-side
//To be used with redux-thunk or any similar middleware.
//params: reachWithDispatch(url, query/mutation, queryParameters, actionCreator, store, retry)
//Automatically handles updating redux store client-side
//This function is still a WIP not implemented

import { reachWithOpts } from 'react-reach';

reachWithOpts('localhost:1000', `mutation {
    CreateUser {
        _id: "12345",
        name: JohnDoe
    }
}`, {}, ObjectContainingActionCreators, store, true);

Caveat

Make sure to enable CORS on your server with __ OPTIONS__ to avoid CORS error or Preflight fetch error. Heres an example when using Express:

app.use(function (req, res, next) {
	res.setHeader('Access-Control-Allow-Origin', '*');
	res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
	res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');

	if (req.method === "OPTIONS") {
		res.status(200).end();
	} else {
		next();
	}
	next();
});

The State of react-reach

I began working on react-reach the past few day. I hope people are willing to try it out and help me spot bugs and problems.Feel free to give me feedback and request features you would like to see in the project.

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