apollo-server-integration-msw
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

apollo-server-integration-msw

An Apollo Server integration for use with Mock Service Worker

Working example

https://codesandbox.io/s/apollo-server-integration-msw-l44psn

Getting started

First create a mock definition file at for example src/handler.ts.

Next create an Apollo Server instance and pass it to startServerAndCreateMSWHandler:

import { ApolloServer } from '@apollo/server';
import { startServerAndCreateMSWHandler } from 'apollo-server-integration-msw';
import { gql } from 'graphql-tag';

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  resolvers,
  typeDefs,
});

export default startServerAndCreateMSWHandler(server);

You may also pass a context function to startServerAndCreateMSWHandler as such:

export default startServerAndCreateMSWHandler(server, {
  context: async (req, res, ctx) => ({ req, res, ctx, user: await getLoggedInUser(req) }),
});

The MSW req, res and ctx objects are passed along to the context function.

Use created handler for MSW

import { setupWorker, rest } from 'msw';
import apolloMSWHandler from './handler';

const worker = setupWorker(rest.post('/api/graphql', apolloMSWHandler));

// Register the Service Worker and enable the mocking
worker.start();

Readme

Keywords

none

Package Sidebar

Install

npm i apollo-server-integration-msw

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

17.7 kB

Total Files

7

Last publish

Collaborators

  • adascal