npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hipshot/react-simple-graphql

v2.1.1

Published

A simple GraphQL client for React

Downloads

8

Readme

React Simple GraphQL

Simple GraphQL, in-browser-client for React apps. Wraps apollo-fetch.

Goal is to be usable with a minimal amount of setup, but still configurable enough to handle more complex use-cases.

import Query from '@hipshot/react-simple-graphql/Query';

<Query query={query} variables={variables}>
  {({ data }) => data && <pre>{JSON.stringify(data, null, 2)}</pre>}
</Query>

Install

npm i @hipshot/react-simple-graphql

Components

Query Component

import Query from '@hipshot/react-simple-graphql/Query';
const uri = "https//example.org/graphql";

const query = `
  {
    dog {
      name
    }
  }
`;

Calling with the function-as-child pattern:

<Query
  uri={uri}
  query={query}
  variables={variables}
  before={beforeMiddleware}
  after={afterMiddleware}
  onData={fn}
  onError={fn}
>
  {({ loading, data, errors }) => {
    //
  }}
</Query>

Calling with render callbacks render and renderLoading.

<Query
  uri={uri}
  query={query}
  variables={variables}
  before={beforeMiddleware}
  after={afterMiddleware}
  onData={fn}
  onError={fn}
  renderLoading={ () => <Loading />}
  render={ ({data, errors}) => <SomethingOnceQueryCompletes /> }
/>

Mutation Component

Example:

import Mutation from '@hipshot/react-simple-graphql/Mutation';

const uri = "https//example.org/graphql";
const mutation = `
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}`;

<Mutation
  uri={uri}
  mutation={mutation}
  before={beforeMiddleware}
  after={afterMiddleware}
>
  {({ mutation, data, errors }) =>
    errors ? (
      // mutation ran unsuccessfully,
      // do stuff with errors
    ) : data ? (
      // mutation ran successfully,
      // do stuff with results
    ) : (
      // `mutation` is a function that passes its
      // args as variables to the mutation, eg:
      <button
        onClick={() =>
          mutation({
            "ep": "JEDI",
            "review": {
              "stars": 5,
              "commentary": "This is a great movie!"
            }
          })}
        >
        add review
      </button>
    )}
</Mutation>

Provider Component

To globally change the uri. before, and after of all <Query/> and <Mutation/>, a <Provider/> component can be added at the top of your UI tree.

Any <Query/> or <Mutation/> component that's a descendent of <Provider/> will receive the uri prop, as well as any configured before and after middleware via context.

import Provider from '@hipshot/react-simple-graphql/Provider';

<Provider uri="https//example.org/graphql" before={beforeMiddleware} after={afterMiddleware}>
  <div>
    <Query query={query} variables={variables}>
    {({data})=> (
      // do stuff with data
    )}
    </Query>
  </div>
</Provider>

A uri prop on the <Query/> or <Mutation/> will always take precendence over the uri provided by <Provider/>.

If before and after middleware exist in both the <Provider /> and <Query /> or <Mutation />, those middlewares are chained together with middleware added via <Provider /> are ran first.

Middleware

Both the request and the response can be decorated or acted upon immedately before or after making the request or receiving the response respectively. This is helpful for adjusting headers (before) or global-handling of particular responses.

before

const appendTokenMiddleware = ({ request, options }, next) => {
  options.headers = {
    ...options.headers,
    "x-access-token": `1234`
  };

  next();
};

This before middleware can now be attached to <Provider />, <Query />, or <Mutation /> via the before prop.

<Query before={appendTokenMiddleware} query={query} />

// or
<Mutation before={appendTokenMiddleware} query={query} />

// or
<Provider before={appendTokenMiddleware}>
  ...
</Provider>
// where all descendant `Query` or `Mutation` elements will use this middleware.

after

const logStatus = ({ response }, next) => {
  action("response status")(response.status);
  next();
};

This after middleware can now be attached to <Provider />, <Query />, or <Mutation /> via the after prop.

<Query after={logStatus} query={query} />

// or
<Mutation after={logStatus} query={query} />

// or
<Provider after={logStatus}>
  ...
</Provider>
// where all descendant `Query` or `Mutation` elements will use this middleware.

withUri HoC

Another way to make the uri prop optional on <Query/> and <Mutatation/> is with a static method provided on both Query and Mutation called withUri:

Eg:

// my-gql-client.js
import {Query as Q, Mutation as M} from '@hipshot/react-simple-graphql';

const uri = "https//example.org/graphql";

export const Query = Q.withUri(uri);
export const Mutation = M.withMutation(uri);