apollo-local-network-interface icon indicating copy to clipboard operation
apollo-local-network-interface copied to clipboard

Use Apollo without a GraphQL server (porting of relay-local-schema)

Almost deprecated since the introduction ApolloLinks in Apollo 2

Use ApolloClient without a GraphQL server. This is a porting of relay-local-schema for Apollostack.

Usage

This package implements a common pattern for using ApolloClient without a GraphQL server. The code is simple, even trivial, and you might prefer to roll up your own version. You can add this package to your project with:

npm install mstn:apollo-local-network-interface#v1.0.0

LocalNetworkInterface enables ApolloClient to execute GraphQL queries and mutations locally.

import ApolloClient from 'apollo-client';

import schema from './data/schema';

const networkInterface = createLocalNetworkInterface({ schema });
const client = new ApolloClient({ networkInterface });

// ... then you can use client.query or client.mutate as usual (see tests for examples)

Why

As the original package relay-local-schema for Relay framework, LocalNetworkInterface could be useful in a variety of cases:

  • testing,
  • demos,
  • client side wrapper for legacy REST APIs.

Credits

More info

  • @lucasconstantino wrote a very nice blog post about this topic. I recommend it!

Update: Apollo 2

If you use Apollo 2, you do not need this package. Indeed, you can pass an apollo-link-schema to your Apollo client. From official documentation:

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { SchemaLink } from 'apollo-link-schema';
 
import schema from './data/schema';
 
const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new SchemaLink({ schema })
});