npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-server-rxjs

v0.3.2-patch.3

Published

Production-ready Node.js GraphQL server for Express, Hapi, Koa

Downloads

12

Readme

GraphQL Server for Express, Connect, Hapi and Koa

npm version Build Status Coverage Status Get on Slack

Apollo Server is a community-maintained open-source GraphQL server. It works with all Node.js HTTP server frameworks: Express, Connect, Hapi and Koa.

Principles

Apollo Server is built with the following principles in mind:

  • By the community, for the community: Apollo Server's development is driven by the needs of developers
  • Simplicity: by keeping things simple, Apollo Server is easier to use, easier to contribute to, and more secure
  • Performance: Apollo Server is well-tested and production-ready - no modifications needed

Anyone is welcome to contribute to Apollo Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!

Getting started

Apollo Server is super-easy to set up. Just npm-install apollo-server, write a GraphQL schema, and then use one of the following snippets to get started. For more info, read the Apollo Server docs.

TypeScript

If you want to build your GraphQL server using TypeScript, Apollo Server is the project for you. NOTE: All typings mentioned below must be included in your project in order for it to compile.

npm install apollo-server
typings i -SG dt~express dt~express-serve-static-core dt~serve-static dt~mime dt~hapi dt~boom dt~cookies dt~koa

For using the project in JavaScript, just run npm install --save apollo-server and you're good to go!

Express

import express from 'express';
import { apolloExpress } from 'apollo-server';

const myGraphQLSchema = // ... define or import your schema here!
const PORT = 3000;

var app = express();

app.use('/graphql', bodyParser.json(), apolloExpress({ schema: myGraphQLSchema }));

app.listen(PORT);

Connect

import connect from 'connect';
import { apolloConnect } from 'apollo-server';

const PORT = 3000;

var app = connect();

app.use('/graphql', bodyParser.json(), apolloConnect({ schema: myGraphQLSchema }));

app.listen(PORT);

Hapi

Now with the Hapi plugins apolloHapi and graphiqlHapi you can pass a route object that includes options to be applied to the route. The example below enables CORS on the /graphql route.

import hapi from 'hapi';
import { apolloHapi } from 'apollo-server';

const server = new hapi.Server();

const HOST = 'localhost';
const PORT = 3000;

server.connection({
    host: HOST,
    port: PORT,
});

server.register({
    register: apolloHapi,
    options: {
      path: '/graphql',
      apolloOptions: {
        schema: myGraphQLSchema,
      },
      route: {
        cors: true
      }
    },
});

server.start((err) => {
    if (err) {
        throw err;
    }
    console.log(`Server running at: ${server.info.uri}`);
});

Koa

import koa from 'koa';
import koaRouter from 'koa-router';
import { apolloKoa } from 'apollo-server';

const app = new koa();
const router = new koaRouter();
const PORT = 3000;

app.use(koaBody());

router.post('/graphql', apolloKoa({ schema: myGraphQLSchema }));
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(PORT);

Websocket

import { createServer } from 'http';
import { Server as WsServer } from 'ws';
import { wsApollo } from 'apollo-server';
import * as url from 'url';

let server = createServer();
const PORT = 3000;
let wss = new WsServer({ server });
wss.on("connection", wsApollo((ws) => {
	const location = url.parse(ws.upgradeReq.url, true);

	// Multiplex ws connections by path.
	switch ( location.pathname ) {
		case GRAPHQL_ROUTE:
			return {
				context: {},
				schema: Schema,
			}
		default:
			ws.terminate();
			return undefined;
	}
}));

server.listen(PORT);

Options

Apollo Server can be configured with an options object with the the following fields:

  • schema: the GraphQLSchema to be used
  • context: the context value passed to resolvers during GraphQL execution
  • rootValue: the value passed to the first resolve function
  • formatError: a function to apply to every error before sending the response to clients
  • validationRules: additional GraphQL validation rules to be applied to client-specified queries
  • formatParams: a function applied for each query in a batch to format parameters before execution
  • formatResponse: a function applied to each response after execution

All options except for schema are optional.

Whitelisting

The formatParams function can be used in combination with the OperationStore to enable whitelisting.

const store = new OperationStore(Schema);
store.put('query testquery{ testString }');
apolloOptions = {
    schema: Schema,
    formatParams(params) {
        params['query'] = store.get(params.operationName);
        return params;
    },
};

Differences to express-graphql

Apollo Server and express-graphql are more or less the same thing (GraphQL middleware for Node.js), but there are a few key differences:

  • express-graphql works with Express and Connect, Apollo Server supports Express, Connect, Hapi and Koa.
  • express-graphql's main goal is to be a minimal reference implementation, whereas Apollo Server's goal is to be a complete production-ready GraphQL server.
  • Compared to express-graphql, Apollo Server has a simpler interface and supports exactly one way of passing queries.
  • Apollo Server separates serving GraphiQL (GraphQL UI) from responding to GraphQL requests.
  • express-graphql contains code for parsing HTTP request bodies, Apollo Server leaves that to standard packages like body-parser.
  • Includes an OperationStore to easily manage whitelisting
  • Built with TypeScript

Despite express-graphql being a reference implementation, Apollo Server is actually easier to understand and more modular than express-graphql.

That said, Apollo Server is heavily inspired by express-graphql (it's the reference implementation after all). Rather than seeing the two as competing alternatives, we think that they both have separate roles in the GraphQL ecosystem: express-graphql is a reference implementation, and Apollo Server is a GraphQL server to be used in production and evolve quickly with the needs of the community. Over time, express-graphql can adopt those features of Apollo Server that have proven their worth and become established more widely.

Apollo Server Development

If you want to develop apollo server locally you must follow the following instructions:

  • Fork this repository

  • Install the Apollo Server project in your computer

git clone https://github.com/[your-user]/apollo-server
cd apollo-server
npm install -g typescript live-server
npm install
npm run typings
npm run compile
npm link
  • Install your local Apollo Server in other App
cd ~/myApp
npm link apollo-server