Socket
Socket
Sign inDemoInstall

apollo-server-integration-msw

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-server-integration-msw

An Apollo Server integration for use with Mock Service Worker


Version published
Maintainers
1
Created
Source

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();

FAQs

Package last updated on 09 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc