All Projects → gyzerok → Adrenaline

gyzerok / Adrenaline

Licence: mit
Simple Relay alternative

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Adrenaline

Fraql
GraphQL fragments made simple ⚡️
Stars: ✭ 433 (-39.86%)
Mutual labels:  graphql, relay
React Starter Kit
React Starter Kit — front-end starter kit using React, Relay, GraphQL, and JAM stack architecture
Stars: ✭ 21,060 (+2825%)
Mutual labels:  graphql, relay
Create Graphql
Command-line utility to build production-ready servers with GraphQL.
Stars: ✭ 441 (-38.75%)
Mutual labels:  graphql, relay
Reactjs101
從零開始學 ReactJS(ReactJS 101)是一本希望讓初學者一看就懂的 React 中文入門教學書,由淺入深學習 ReactJS 生態系 (Flux, Redux, React Router, ImmutableJS, React Native, Relay/GraphQL etc.)。
Stars: ✭ 4,004 (+456.11%)
Mutual labels:  graphql, relay
Js Graphql Intellij Plugin
GraphQL language support for WebStorm, IntelliJ IDEA and other IDEs based on the IntelliJ Platform.
Stars: ✭ 686 (-4.72%)
Mutual labels:  graphql, relay
Graphql Dataloader Boilerplate
Very simple boilerplate using GraphQL and DataLoader
Stars: ✭ 405 (-43.75%)
Mutual labels:  graphql, relay
Get Graphql Schema
Fetch and print the GraphQL schema from a GraphQL HTTP endpoint. (Can be used for Relay Modern.)
Stars: ✭ 443 (-38.47%)
Mutual labels:  graphql, relay
Jseg
A super simple, in-memory, JS graph database.
Stars: ✭ 344 (-52.22%)
Mutual labels:  graphql, relay
Graphql Ruby
Ruby implementation of GraphQL
Stars: ✭ 4,931 (+584.86%)
Mutual labels:  graphql, relay
Graphql Crunch
Reduces the size of GraphQL responses by consolidating duplicate values
Stars: ✭ 472 (-34.44%)
Mutual labels:  graphql, relay
Graphql Ws
Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
Stars: ✭ 398 (-44.72%)
Mutual labels:  graphql, relay
Graphqlbundle
This bundle provides tools to build a complete GraphQL server in your Symfony App.
Stars: ✭ 628 (-12.78%)
Mutual labels:  graphql, relay
Parse Server
API server module for Node/Express
Stars: ✭ 19,165 (+2561.81%)
Mutual labels:  graphql, relay
Graphql Up
Get a ready-to-use GraphQL API for your schema
Stars: ✭ 415 (-42.36%)
Mutual labels:  graphql, relay
Absinthe
The GraphQL toolkit for Elixir
Stars: ✭ 3,805 (+428.47%)
Mutual labels:  graphql, relay
Graphql Pokemon
Get information of a Pokémon with GraphQL!
Stars: ✭ 441 (-38.75%)
Mutual labels:  graphql, relay
Relay Modern Course
Relay Modern Course
Stars: ✭ 310 (-56.94%)
Mutual labels:  graphql, relay
Fsharp.data.graphql
FSharp implementation of Facebook GraphQL query language.
Stars: ✭ 320 (-55.56%)
Mutual labels:  graphql, relay
React Firebase Starter
Boilerplate (seed) project for creating web apps with React.js, GraphQL.js and Relay
Stars: ✭ 4,366 (+506.39%)
Mutual labels:  graphql, relay
React App
Create React App with server-side code support
Stars: ✭ 614 (-14.72%)
Mutual labels:  graphql, relay

Adrenaline

build status npm version npm downloads

This library provides a subset of Relay's behaviour with a cleaner API.

Why?

Relay is a great framework with exciting ideas behind it. The downside is that in order to get all the cool features, you need to deal with a complex API. Relay provides you a lot of tricky optimizations which probably are more suitable for huge projects. In small, medium and even large ones you would prefer to have better developer experience while working with a simple, minimalistic set of APIs.

Adrenaline intends to provide you Relay-like ability to describe your components with declarative data requirements, while keeping the API as simple as possible. You are free to use it with different libraries like Redux, React Router, etc.

When not to use it?

  • You have a huge project and highly need tricky optimisations to reduce client-server traffic.
  • When you don't understand why you should prefer Adrenaline to Relay.

Installation

npm install --save adrenaline

Adrenaline requires React 15.0 or later.

Adrenaline uses fetch under the hood so you need to install the polyfill by yourself.

npm install --save whatwg-fetch

and then import it at the very top of your entry JavaScript file:

import 'whatwg-fetch';
import React from 'react';
import ReactDOM from 'react-dom';
import { Adrenaline } from 'adrenaline';

import App from './components/App';

ReactDOM.render(
  <Adrenaline>
    <App />
  </Adrenaline>,
  document.getElementById('root')
)

API

Adrenaline follows the idea of Presentational and Container Components

<Adrenaline endpoint />

Root of your application should be wrapped with Adrenaline component. This component is a provider component which injects some helpful stuff into your React application.

prop name type default/required purpose
endpoint string "/graphql" URI of your GraphQL endpoint

container({ variables, query })(Component)

In Adrenaline, you create container components mostly for your route handlers. The purpose of containers is to collect data requirements from presentation components in a single GraphQL query. They also behave like view controllers and are able to speak to the outside world using mutations.

key type default/required purpose
variables (props: Props) => Object () => ({}) describe query variables as a pure function of props
query string required your GraphQL query for this container
import React, { Component, PropTypes } from 'react';
import { container } from 'adrenaline';
import TodoList from './TodoList';

class UserItem extends Component {
  static propTypes = {
    viewer: PropTypes.object.isRequired,
  }
  /* ... */
}

export default container({
  variables: (props) => ({
    id: props.userId,
  }),
  query: `
    query ($id: ID!) {
      viewer(id: $id) {
        id,
        name,
        ${TodoList.getFragment('todos')}
      }
    }
  `,
})(UserItem);

Containers may also pass 2 additional properties to your component: mutate and isFetching.

  • mutate({ mutation: String, variables: Object = {}, invalidate: boolean = true }): Promise: You need to use this function in order to perform mutations. invalidate argument means you need to resolve data declarations after mutation.
  • isFetching: boolean: This property helps you understand if your component is in the middle of resolving data.

presenter({ fragments })(Component)

As in the presentational components idea, all your dumb components may be declared as simple React components. But if you want to declare your data requirements in a way similar to Relay, you can use the presenter() higher-order component.

import React, { Component } from 'react';
import { presenter } from 'adrenaline';

class TodoList extends Component {
  /* ... */
}

export default presenter({
  fragments: {
    todos: `
      fragment on User {
        todos {
          id,
          text
        }
      }
    `,
  },
})(TodoList);

Using ES7 decorators

Adrenaline works as higher-order components, so you can decorate your container components using ES7 decorators

import { container } from 'adrenaline'

@container({
  variables: (props) => ({
    id: props.userId,
  }),
  query: `
    query ($id: ID!) {
      viewer(id: $id) {
        id,
        name,
        ${TodoList.getFragment('todos')}
      }
    }
  `
})
export default class extends Component {
  static propTypes = {
    viewer: PropTypes.object.isRequired,
  }
  /* ... */
}

Mutations

You can declare your mutations as simple as:

const createTodo = `
  mutation ($text: String, $owner: ID) {
    createTodo(text: $text, owner: $owner) {
      id,
      text,
      owner {
        id
      }
    }
  }
`;

Then you can use this mutation with your component:

import React, { Component, PropTypes } from 'react';
import { createSmartComponent } from 'adrenaline';

class UserItem extends Component {
  static propTypes = {
    viewer: PropTypes.object.isRequired,
  }

  onSomeButtonClick() {
    this.props.mutate({
      mutation: createTodo,
      variables: {
        text: hello,
        owner: this.props.viewer.id
      },
    });
  }

  render() {
    /* render some stuff */
  }
}

Testing

There is a common problem I've discovered so far while developing applications. When you change the GraphQL schema, you'd like to know which particular subtrees in your applications need to be fixed. And you probably do not want to check this by running your application and going through it by hand.

For this case, Adrenaline provides you helper utilities for integration testing (currently for expect only). You can use toBeValidAgainst for checking your components' data requirements against your schema with GraphQL validation mechanism.

import expect from 'expect';
import TestUtils from 'adrenaline/lib/test';

import schema from 'path/to/schema';
// TodoApp is a container component
import TodoApp from 'path/to/TodoApp';

expect.extend(TestUtils.expect);

describe('Queries regression', () => {
  it('for TodoApp', () => {
    expect(TodoApp).toBeValidAgainst(schema);
  });
});

Image

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