All Projects → grafoojs → Grafoo

grafoojs / Grafoo

Licence: mit
A GraphQL Client and Toolkit

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Grafoo

Babel Blade
(under new management!) ⛸️Solve the Double Declaration problem with inline GraphQL. Babel plugin/macro that works with any GraphQL client!
Stars: ✭ 266 (+0.76%)
Mutual labels:  graphql, babel, babel-plugin, graphql-client
Magiql
🌐 💫 Simple and powerful GraphQL Client, love child of react-query ❤️ relay
Stars: ✭ 45 (-82.95%)
Mutual labels:  graphql, babel-plugin, graphql-client
Reason Urql
Reason bindings for Formidable's Universal React Query Library, urql.
Stars: ✭ 203 (-23.11%)
Mutual labels:  graphql, graphql-client
36 Graphql Concepts
📜 36 concepts every GraphQL developer should know.
Stars: ✭ 209 (-20.83%)
Mutual labels:  graphql, graphql-client
Apollo Android
🤖  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
Stars: ✭ 2,949 (+1017.05%)
Mutual labels:  graphql, graphql-client
Graphql Deduplicator
A GraphQL response deduplicator. Removes duplicate entities from the GraphQL response.
Stars: ✭ 258 (-2.27%)
Mutual labels:  graphql, graphql-client
Graphql.js
A Simple and Isomorphic GraphQL Client for JavaScript
Stars: ✭ 2,206 (+735.61%)
Mutual labels:  graphql, graphql-client
Neuron
A GraphQL client for Elixir
Stars: ✭ 244 (-7.58%)
Mutual labels:  graphql, graphql-client
Graphpack
☄️ A minimalistic zero-config GraphQL server.
Stars: ✭ 1,994 (+655.3%)
Mutual labels:  graphql, babel
babel-plugin-source-map-support
A Babel plugin which automatically makes stack traces source-map aware
Stars: ✭ 41 (-84.47%)
Mutual labels:  babel, babel-plugin
core-web
like core-js but for Web APIs (based on polyfill.io)
Stars: ✭ 34 (-87.12%)
Mutual labels:  babel, babel-plugin
babel-plugin-transform-replace-expressions
A Babel plugin for replacing expressions with other expressions
Stars: ✭ 23 (-91.29%)
Mutual labels:  babel, babel-plugin
Hotchocolate
Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
Stars: ✭ 3,009 (+1039.77%)
Mutual labels:  graphql, graphql-client
Gqlify
[NOT MAINTAINED]An API integration framework using GraphQL
Stars: ✭ 182 (-31.06%)
Mutual labels:  graphql, graphql-client
Crate
👕 👖 📦 A sample web and mobile application built with Node, Express, React, React Native, Redux and GraphQL. Very basic replica of stitchfix.com / krate.in (allows users to get monthly subscription of trendy clothes and accessories).
Stars: ✭ 2,281 (+764.02%)
Mutual labels:  graphql, babel
Modelizr
Generate GraphQL queries from models that can be mocked and normalized.
Stars: ✭ 175 (-33.71%)
Mutual labels:  graphql, graphql-client
Aws Mobile Appsync Sdk Ios
iOS SDK for AWS AppSync.
Stars: ✭ 231 (-12.5%)
Mutual labels:  graphql, graphql-client
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-84.85%)
Mutual labels:  babel, babel-plugin
Babel Plugin Graphql Tag
Compiles GraphQL tagged template strings using graphql-tag.
Stars: ✭ 156 (-40.91%)
Mutual labels:  graphql, babel-plugin
Graphql Tag.macro
Babel Macro for graphql-tag
Stars: ✭ 168 (-36.36%)
Mutual labels:  graphql, babel

Grafoo

A GraphQL Client and Toolkit

build coverage npm downloads code style: prettier mantained with: lerna slack

Grafoo is a GraphQL client that tries to be different by adopting a simpler API, without giving up of a good caching strategy.

Some useful information

  • It's a multiple paradigm library. So far we have view layer integrations for react and preact and there are more to come.
  • It's not just a HTTP client. It comes with a sophisticated caching system under the hood to make sure your data is consistent across your app.
  • It's build time dependent. A important piece of Grafoo is it's babel plugin that compiles your queries based on the schema your app consumes.
  • It's environment agnostic. Apart from the browser you can run Grafoo on the server and even on native with react.

Why should I use this

Many of the work that has been put into this project came from borrowed ideas and concepts that are present in the GraphQL clients we have today. Grafoo wants to stand apart from the others trying to be in that sweet spot between simplicity and usability. Moreover, most of the benefits this library brings to the table are related to the fact that it does a lot at build time. It's fast, because it spares runtime computation and it's really small (something like ~1.6kb for core and react) because it does not ship with a GraphQL parser.

Example applications

You can refer to examples in this repository.

Basic usage

Installation

The basic packages you'll have to install in order to use Grafoo are core and babel-plugin.

$ npm i @grafoo/core && npm i -D @grafoo/babel-plugin

Configure babel

In @grafoo/babel-plugin the option schema is a path to a GraphQL schema in your file system relative to the root of your project and idFields is an array of strings that represent the fields that Grafoo will automatically insert on your queries to build unique identifiers in order to normalize the cache. Both options are required.

{
  "plugins": [
    [
      "@grafoo/babel-plugin",
      {
        "schema": "schema.graphql",
        "idFields": ["id"]
      }
    ]
  ]
}

Writing your app

From @grafoo/core you will import the factory that creates the client instance and from submodule @grafoo/core/tag you'll import the graphql or gql tag that will be compiled at build time.

import createClient from "@grafoo/core";
import gql from "@grafoo/core/tag";

function fetchQuery(query, variables) {
  const init = {
    method: "POST",
    body: JSON.stringify({ query, variables }),
    headers: {
      "content-type": "application/json"
    }
  };

  return fetch("http://some.graphql.api", init).then(res => res.json());
}

const client = createClient(fetchQuery);

const USER_QUERY = gql`
  query($id: ID!) {
    user(id: $id) {
      name
    }
  }
`;

const variables = { id: 123 };

client.execute(USER_QUERY, variables).then(data => {
  // Write to cache
  client.write(USER_QUERY, variables, data);

  // Do whatever with returned data
  console.log(data);

  // Read from cache at a later stage
  console.log(client.read(USER_QUERY, variables));
});

// If you wish to reset (clear) the cache:
client.reset();

With a framework

Here is how it would go for you to write a simple react app.

index.js

import React from "react";
import ReactDom from "react-dom";
import createClient from "@grafoo/core";
import { Provider } from "@grafoo/react";

import Posts from "./Posts";

function fetchQuery(query, variables) {
  const init = {
    method: "POST",
    body: JSON.stringify({ query, variables }),
    headers: {
      "content-type": "application/json"
    }
  };

  return fetch("http://some.graphql.api", init).then(res => res.json());
}

const client = createClient(fetchQuery);

ReactDom.render(
  <Provider client={client}>
    <Posts />
  </Provider>,
  document.getElementById("mnt")
);

Posts.js

import React from "react";
import gql from "@grafoo/core/tag";
import { Consumer } from "@grafoo/react";

const ALL_POSTS = gql`
  query getPosts($orderBy: PostOrderBy) {
    allPosts(orderBy: $orderBy) {
      title
      content
      createdAt
      updatedAt
    }
  }
`;

export default function Posts() {
  return (
    <Consumer query={ALL_POSTS} variables={{ orderBy: "createdAt_DESC" }}>
      {({ client, load, loaded, loading, errors, allPosts }) => (
        <marquee>👆 do whatever you want with the variables above 👆</marquee>
      )}
    </Consumer>
  );
}

Mutations

import React from "react";
import gql from "@grafoo/core/tag";
import { Consumer } from "@grafoo/react";

const ALL_POSTS = gql`
  query getPosts($orderBy: PostOrderBy) {
    allPosts(orderBy: $orderBy) {
      title
      content
      createdAt
      updatedAt
    }
  }
`;

const CREATE_POST = gql`
  mutation createPost($content: String, $title: String, $authorId: ID) {
    createPost(content: $content, title: $title, authorId: $authorId) {
      title
      content
      createdAt
      updatedAt
    }
  }
`;

const mutations = {
  createPost: {
    query: CREATE_POST,
    optimisticUpdate: ({ allPosts }, variables) => ({
      allPosts: [{ ...variables, id: "tempID" }, ...allPosts]
    }),
    update: ({ allPosts }, data) => ({
      allPosts: allPosts.map(p => (p.id === "tempID" ? data.createPost : p))
    })
  }
};

const submit = mutate => event => {
  event.preventDefault();

  const { title, content } = event.target.elements;

  mutate({ title: title.value, content: content.value });
};

export default function PostForm() {
  return (
    <Consumer query={ALL_POSTS} variables={{ orderBy: "createdAt_DESC" }} mutations={mutations}>
      {({ createPost }) => (
        <form onSubmit={submit(createPost)}>
          <input name="title" />
          <textarea name="content" />
          <button>submit</button>
        </form>
      )}
    </Consumer>
  );
}

LICENSE

MIT

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