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

use-apollo-mock

v0.1.2

Published

React hook for react-apollo-hooks like prototyping frontend

Downloads

245

Readme

use-apollo-mock

Package helping you speed up the frontend development with Apollo client when the backend implementation is lacking behind.

Reason behind making this package

While working with graphql, I had a realization that it's much easier to define the graphql queries/mutations only after the forntend feature is partially implemented. The issue was, how do you fake the apollo queries in a simple way? So, after the backend is implemneted you have to do minimal changes on the frotnend?

The result is a super simple quick package for faking apollo client queires for faster prototyping.

How it's used

1. install the package

The package is assuming you use react react-apollo and grapqhl-tag (apollo-boost has graphql-tag included)

npm install --save use-apollo-mock

2. use grapqhl query as a react hook

Largly inspirate from react-apollo-hooks, you simply provide the query (you will later use for real) to the userQueryMock hook and also provide fake data through options.

import React from 'react';
improt { useQueryMock } from 'use-apollo-mock';

// Standard graphql query. In whatever way you write it
const QUERY = gql`
  query myQuery {
    myQuery {
      data
    }
  }
`;

// This is the fake data we want to get back. Pretending
// we are receiving it from the server. 
// Notice I follow the exact same shape of the query
// to replicate the shape the server would send back.
const fakeData = {
  myQuery: {
    data: 'awesome'
  }
}

// Use in a React component as a hook. 
function Component() {
  const { data, loading, error } = useQueryMock(QUERY, {
    data: fakeData
  });
  
  return null;
}
 

This will immitate the loading state and will return you teh fake data.

3. Cache the fake data results

You can take it one step further and immitate the caching as well. Just wrap your app with a ApolloMockProvider. it doesn't need anything else.

import React from 'react';
import { ApolloMockProvider } from 'use-apollo-mock`;

function App() {
  return (
    <ApolloMockProvider>
      <YourApp />
    </ApolloMockProvider>
  )
}

4. Test an error case

You can also provide an erro mock, to pretend you got an error from the server. Just use the same principal from step 2:


// Use in a React component as a hook. 
function Component() {
  const { data, loading, error } = useQueryMock(QUERY, {
    data: fakeData,
    error: new Error('Something bad happened')
  });
  
  return null;
}

Final Notes

Please keep in mind this is work in progress and is meant to be a simple quick and dirty help for prototyping. However, if you find this package helpful and want to improve it, feel free to open a issue or create a pull request.

IMPORTANT

At the moment it only supports the "Query". It dooesn't fake "Mutation". I might add it when I have a little bit more time :)