New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hops-apollo-mock-server

Package Overview
Dependencies
Maintainers
6
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hops-apollo-mock-server

Apollo based mock server for Hops

  • 15.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42
decreased by-42.47%
Maintainers
6
Weekly downloads
 
Created
Source

hops-apollo-mock-server

npm

Please see the main Hops Readme for general information and a Getting Started Guide.

This is a preset for Hops in order to start and configure an Apollo Server that can be used for GraphQL mocking to enable faster local development.

Installation

Add this preset (and any other packages that you may need to create an executable schema, such as @graphql-tools/schema) to your existing Hops React project:

npm install --save hops-apollo-mock-server

If you don't already have an existing Hops project read this section on how to set up your first Hops project.

Usage

Creating custom mocks and stitching schemas using Apollo Server

When you are using GraphQL on client side to fetch and bind data into your UI components, it's quite often necessary to work with mock/stub data. There exists tons of feasible reasons why mocking makes sense in daily practices. In summary, the following seem to be the most important.

  • GraphQL schema design in a Frontend-Driven approach
  • Switching between local and remote query execution to work autonomously without an online GraphQL-Server access
  • Faster execution of component integration test using local mock data sets
  • Mock data set support to prove experimental/feature functionality thesis

You can enable mocking by configuring a file that exports an executable schema. Read more about schema stitching and check out this blog post for more examples.

Supports Local GraphQL Playground against your GraphQL schema

open http://localhost:<port>/graphql

GraphiQL Playground

Configuration

Preset Options
NameTypeDefaultRequiredDescription
fragmentsFileString<rootDir>/fragmentTypes.jsonnoWhere to store the generated fragment types file
graphqlUriString''yesUrl to your GraphQL endpoint or mock server
graphqlSchemaFileString''noPath to your GraphQL schema file
graphqlMockSchemaFileString''noPath to your GraphQL schema mocks
graphqlMockServerPathString'/graphql'noPath of the mock server endpoint
fragmentsFile

This option controls where the fragment type information that are used for the IntrospectionFragmentMatcher should be saved.

By default executing $ hops graphql introspect will create a file called fragmentTypes.json in the application root directory.

"hops": {
  "fragmentsFile": "<rootDir>/fragmentTypes.json"
}
graphqlUri

This is the full URI to your GraphQL endpoint which should be used by the client- and server-side when executing requests.

This will also be used to generate fragment type information with $ hops graphql introspect in case no graphqlSchemaFile has been provided.

"hops": {
  "graphqlUri": "https://www.graphqlhub.com/graphql"
}
graphqlSchemaFile

In case your GraphQL server (configured via graphqlUri) does not answer to introspection queries, you can provide the full schema as a file from which the introspection fragment matcher can generate information about unions and interfaces.

"hops": {
  "graphqlSchemaFile": "<rootDir>/schema.graphql"
}
graphqlMockSchemaFile

Specify the path to your stitched mock schema, which is a file that exports an executable schema or a promise that resolves to an executable schema.

{
  "hops": {
    "graphqlMockSchemaFile": "<rootDir>/graphql/index.js"
  }
}

Example mock schema: graphql/index.js

import { makeExecutableSchema } from '@graphql-tools/schema';
import { addMocksToSchema } from '@graphql-tools/mock';
import merge from 'lodash.merge';

import schema1 from './schema1.graphql';
import schema2 from './schema2.graphql';

import resolvers1 from './resolvers1';
import resolvers2 from './resolvers2';

const typeDefs = [schema1, schema2];

const resolvers = merge(resolvers1, resolvers2);

const mockSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

export default addMocksToSchema({
  schema: mockSchema,
  mocks: {
    Date: () => '2017-10-17T13:06:22Z',
  },
  preserveResolvers: true,
});

Keywords

FAQs

Package last updated on 20 Jun 2022

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