Code Monkey home page Code Monkey logo

apollo-server's Introduction

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.

npm install apollo-server
typings i -SG dt~express dt~express-serve-static-core dt~serve-static dt~mime dt~hapi 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

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: new ApolloHAPI(),
    options: { schema: myGraphQLSchema },
    routes: { prefix: '/graphql' },
});

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

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's People

Contributors

benjamn avatar greenkeeperio-bot avatar helfer avatar hribb avatar nevir avatar nnance avatar pradel avatar rdrey avatar xeoneux avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.