All Projects → benawad → Apollo Mocked Provider

benawad / Apollo Mocked Provider

Licence: mit
Automatically mock GraphQL data with a mocked ApolloProvider

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Apollo Mocked Provider

Booben
Web app constructor based on React, with GraphQL bindings
Stars: ✭ 96 (+37.14%)
Mutual labels:  graphql, apollo, apollographql
Graphql Up
Get a ready-to-use GraphQL API for your schema
Stars: ✭ 415 (+492.86%)
Mutual labels:  graphql, apollo, apollographql
React Fullstack Graphql
Starter projects for fullstack applications based on React & GraphQL.
Stars: ✭ 1,352 (+1831.43%)
Mutual labels:  graphql, apollo, apollographql
React Graphql Github Apollo
🚀 A React + Apollo + GraphQL GitHub Client. Your opportunity to learn about these technologies in a real world application.
Stars: ✭ 1,563 (+2132.86%)
Mutual labels:  graphql, apollo, apollographql
Offix
GraphQL Offline Client and Server
Stars: ✭ 694 (+891.43%)
Mutual labels:  graphql, apollo, apollographql
Angular Fullstack Graphql
🚀 Starter projects for fullstack applications based on Angular & GraphQL.
Stars: ✭ 92 (+31.43%)
Mutual labels:  graphql, apollo, apollographql
Graphback
Graphback - Out of the box GraphQL server and client
Stars: ✭ 323 (+361.43%)
Mutual labels:  graphql, apollo, apollographql
Apollo Prophecy
🔮 GraphQL error management made Easy, generate custom machine-readable errors for Apollo Client/Server from the CLI
Stars: ✭ 83 (+18.57%)
Mutual labels:  graphql, apollo, apollographql
Learnapollo
👩🏻‍🏫 Learn Apollo - A hands-on tutorial for Apollo GraphQL Client (created by Graphcool)
Stars: ✭ 5,274 (+7434.29%)
Mutual labels:  graphql, apollo, apollographql
Get Graphql Schema
Fetch and print the GraphQL schema from a GraphQL HTTP endpoint. (Can be used for Relay Modern.)
Stars: ✭ 443 (+532.86%)
Mutual labels:  graphql, apollo, apollographql
Fraql
GraphQL fragments made simple ⚡️
Stars: ✭ 433 (+518.57%)
Mutual labels:  graphql, apollo, apollographql
Graphql React Apollo
A GraphQL implementation in React using Apollo.
Stars: ✭ 9 (-87.14%)
Mutual labels:  graphql, apollo, apollographql
Graphql
GraphQL (TypeScript) module for Nest framework (node.js) 🍷
Stars: ✭ 697 (+895.71%)
Mutual labels:  graphql, apollo, apollographql
Link state demo
🚀 Demonstrate how to support multiple stores in Apollo Link State
Stars: ✭ 30 (-57.14%)
Mutual labels:  graphql, apollo, apollographql
Graphql Modules
⚠️ [DEPRECATED] GraphQL module library for Apollo.
Stars: ✭ 53 (-24.29%)
Mutual labels:  graphql, apollo
Apollo Redux Form
Redux forms powered by Apollo
Stars: ✭ 52 (-25.71%)
Mutual labels:  graphql, apollo
Graphql Upload
Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
Stars: ✭ 1,071 (+1430%)
Mutual labels:  graphql, apollo
Artemis Dev Tool
An Apollo GraphQL Query Schema Testing Tool
Stars: ✭ 66 (-5.71%)
Mutual labels:  graphql, apollo
Data Source Base
Boilerplate for creating a GrAMPS-compatible data source.
Stars: ✭ 52 (-25.71%)
Mutual labels:  graphql, apollographql
Vue Apollo Todos
Vue Apollo GraphQL mutation examples
Stars: ✭ 69 (-1.43%)
Mutual labels:  graphql, apollographql

apollo-mocked-provider

Automatically mock GraphQL data with a mocked ApolloProvider

Inspiration: https://www.freecodecamp.org/news/a-new-approach-to-mocking-graphql-data-1ef49de3d491/

Table of Contents

install

yarn add apollo-mocked-provider

setup

import {
  createApolloErrorProvider,
  createApolloMockedProvider,
  createApolloLoadingProvider,
} from 'apollo-mocked-provider';
import { typeDefs } from './typeDefs';

export const ApolloMockedProvider = createApolloMockedProvider(typeDefs);
export const ApolloErrorProvider = createApolloErrorProvider();
export const ApolloLoadingProvider = createApolloLoadingProvider();

You can get the typeDefs with this helper file

// downloadTypeDefs.js
const { fetchTypeDefs } = require('apollo-mocked-provider');

(() => {
  fetchTypeDefs({ uri: 'http://localhost:4000/graphql' });
})();

Then run that file

node downloadTypeDefs.js

testing

import React from 'react';
import { render, cleanup } from '@testing-library/react';
import { Todos } from './Todos';
import {
  ApolloLoadingProvider,
  ApolloErrorProvider,
  ApolloMockedProvider,
} from './test-utils/providers';

afterEach(cleanup);

test('TodoForm', async () => {
  const { debug } = render(
    <ApolloMockedProvider>
      <Todos />
    </ApolloMockedProvider>
  );

  debug();
  await Promise.resolve();
  debug();
});

Loading:

import React from 'react';
import { render, cleanup } from '@testing-library/react';
import { Todos } from './Todos';
import {
  ApolloLoadingProvider,
  ApolloErrorProvider,
  ApolloMockedProvider,
} from './test-utils/providers';

afterEach(cleanup);

test('TodoForm', async () => {
  const { debug } = render(
    <ApolloLoadingProvider>
      <Todos />
    </ApolloLoadingProvider>
  );

  debug();
});

Error:

import React from 'react';
import { render, cleanup } from '@testing-library/react';
import { Todos } from './Todos';
import {
  ApolloLoadingProvider,
  ApolloErrorProvider,
  ApolloMockedProvider,
} from './test-utils/providers';

afterEach(cleanup);

test('TodoForm', async () => {
  const { debug } = render(
    <ApolloErrorProvider graphQLErrors={[{ message: 'something went wrong' }]}>
      <Todos />
    </ApolloErrorProvider>
  );

  debug();
  await Promise.resolve();
  debug();
});

Custom mocks:

import React from 'react';
import { render, cleanup } from '@testing-library/react';
import { Todos } from './Todos';
import {
  ApolloLoadingProvider,
  ApolloErrorProvider,
  ApolloMockedProvider,
} from './test-utils/providers';

afterEach(cleanup);

test('TodoForm', async () => {
  const { debug } = render(
    <ApolloMockedProvider
      customResolvers={{
        Query: () => ({
          todos: () => [{ id: 1, type: 'hello from custom mocked data' }],
        }),
      }}
    >
      <Todos />
    </ApolloMockedProvider>
  );

  debug();
  await Promise.resolve();
  debug();
});

Custom mocks mixed with errors (if you need to have some resolver succeed and then some others throw errors):

<MockedProvider
  customResolvers={{
    Query: () => ({
      todo: (_obj: any, args: any) => {
        console.log(args.id)
        throw new Error('Boom');
      },
      todos: () => [
        {
          text: 'Success',
        },
      ],
    }),
  }}
>

Cache

By default, providers will use a new instance of InMemoryCache, but you can override that at a global or per component level by providing an object that implements ApolloCache to the create* methods or mocked components respectively.

import { InMemoryCache } from 'apollo-boost';

// global, shared cache
const globalCache = new InMemoryCache();
export const ApolloMockedProvider = createApolloMockedProvider(
  typeDefs,
  globalCache
);

test('local cache', async () => {
  // local, scoped cache
  const localCache = new InMemoryCache();
  const { debug } = render(
    <ApolloMockedProvider cache={localCache}>
      <Todos />
    </ApolloMockedProvider>
  );
});

Using links

If you would like to provide custom links in the chain of the mocked provider, you can pass them in the creation function.

export const ApolloMockedProvider = createApolloMockedProvider(typeDefs, {
  links: ({ cache, schema }) => [
    myLinkFromCache(cache),
    myLinkFromSchema(schema),
  ],
});

Custom links will be inserted before the terminating link which provides schema mocking.

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