No description
Find a file
Jesse Rosenberger aae212f869 Triggers for newly introduced serverWillStart lifecycle hooks.
This commit follows-up on #1795, which introduced the new request pipeline, and
implements the triggers for its new life-cycle hooks within the various
integration packages.  Previously, the only implementation was within the
`apollo-server` package and didn't get triggered for those invoking the
`applyMiddleware` method on integrations (e.g. `apollo-server-express`,
`...-hapi` and `...-koa`).

While in an ideal world, ALL existing `applyMiddleware` functions would be
marked as `async` functions and allow us to `await ApolloServer#willStart`,
in practice the only `applyMiddleware` which is currently `async` is the one
within the Hapi implementation.  Therefore, we'll instead kick off the
`willStart` lifecycle hook as soon as `applyMiddleware` is started, return
as quickly as we have before and then (essentially) yield the completion of
Apollo Server's `willStart` prior to serving the first request — thus
ensuring the completion of server-startup activities.

Similarly, we'll do the same for `createHandler` methods on integrations
which don't utilize Node.js server frameworks but don't have `async`
handlers (e.g. AWS, Lambda, etc.).
2018-10-26 15:07:13 +03:00
.circleci Add support for Jest JUnit test output for consumption by CircleCI. 2018-09-26 22:47:58 +03:00
.github Update ISSUE_TEMPLATE.md 2018-09-27 14:36:26 +03:00
.vscode Add TypeScript watch as default build task 2018-10-12 11:23:30 +02:00
__mocks__ Fix apollo-server-env mock 2018-10-11 14:43:29 +02:00
docs Fix typo in metrics documentation (#1856) 2018-10-25 11:07:21 +03:00
packages Triggers for newly introduced serverWillStart lifecycle hooks. 2018-10-26 15:07:13 +03:00
types Add missing type dependencies for tests 2018-10-11 23:10:45 +02:00
.gitignore Add support for Jest JUnit test output for consumption by CircleCI. 2018-09-26 22:47:58 +03:00
.prettierignore Move prettier file globs into .prettierrc.js file. 2018-09-26 14:32:09 +03:00
.prettierrc.js Move prettier file globs into .prettierrc.js file. 2018-09-26 14:32:09 +03:00
CHANGELOG.md Enables parsing of application/hal+json as JSON in RESTDataSource (#1853) 2018-10-26 05:14:00 +02:00
codecov.yml Add a Codecov configuration file to adjust the defaults. 2018-10-11 13:04:22 +03:00
CONTRIBUTING.md Tick off Prettier checkmarks, which were present on CI but not locally. (#880) 2018-03-16 10:25:07 +02:00
DESIGN.md Tick off Prettier checkmarks, which were present on CI but not locally. (#880) 2018-03-16 10:25:07 +02:00
jest.config.base.js Add comments to moduleNameMapper option in Jest config 2018-10-12 13:37:33 +02:00
lerna.json Clean up lerna.json 2018-08-31 11:01:32 +02:00
LICENSE cherry-pick changes from master 2016-07-05 15:01:49 -07:00
netlify.toml Introduce a netlify.toml configuration to fix docs generation on Netlify. 2018-08-28 12:05:12 +03:00
package-lock.json chore(deps): update dependency @types/aws-lambda to v8.10.14 (#1877) 2018-10-25 13:38:05 +03:00
package.json chore(deps): update dependency @types/aws-lambda to v8.10.14 (#1877) 2018-10-25 13:38:05 +03:00
README.md fix typo in README of Koa example (#1876) 2018-10-25 11:06:55 +03:00
renovate.json Update renovate.json 2018-06-20 15:04:50 +03:00
ROADMAP.md Setup prettier (#724) 2018-01-08 15:08:01 -08:00
tsconfig.base.json Enable forceConsistentCasingInFileNames in tsconfig.base.json 2018-10-11 23:10:44 +02:00
tsconfig.build.json Remove test dependencies from non-test tsconfig.json files 2018-10-11 12:58:03 +02:00
tsconfig.json Remove test dependencies from non-test tsconfig.json files 2018-10-11 12:58:03 +02:00
tsconfig.test.base.json Expose global fetch and URL types to avoid relying on dom lib 2018-10-11 23:10:45 +02:00
tsconfig.test.json Consistently leave out explicit tsconfig.json when referring to TypeScript project 2018-10-12 13:34:07 +02:00

GraphQL Server for Express, Connect, Hapi, Cloudflare workers, and more

npm version Build Status Get on Slack

Apollo Server is a community-maintained open-source GraphQL server. It works with pretty much all Node.js HTTP server frameworks, and we're happy to take PRs for more! Apollo Server works with any GraphQL schema built with GraphQL.js, so you can build your schema with that directly or with a convenience library such as graphql-tools.

Documentation

Read the docs!

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-<integration>, write a GraphQL schema, and then use one of the following snippets to get started. For more info, read the Apollo Server docs.

Installation

Run npm install --save apollo-server-<integration> and you're good to go!

const { ApolloServer, gql } = require('apollo-server');

// The GraphQL schema
const typeDefs = gql`
  type Query {
    "A simple type for getting started!"
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

While we recommend the use schema-definition language (SDL) for defining a GraphQL schema since we feel it's more human-readable and language-agnostic, Apollo Server can be configured with a GraphQLSchema object:

const { ApolloServer, gql } = require('apollo-server');
const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');

// The GraphQL schema
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      hello: {
        type: GraphQLString,
        description: 'A simple type for getting started!',
        resolve: () => 'world',
      },
    },
  }),
});

const server = new ApolloServer({ schema });

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

Integrations

Often times, Apollo Server needs to be run with a particular integration. To start, run npm install --save apollo-server-<integration> where <integration> is one of the following:

  • express
  • koa
  • hapi
  • lambda
  • cloudflare

If a framework is not on this list and it should be supported, please open a PR.

Express

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
server.applyMiddleware({ app });

const port = 4000;

app.listen({ port }, () =>
  console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`),
);

Connect

const connect = require('connect');
const { ApolloServer, gql } = require('apollo-server-express');
const query = require('qs-middleware');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

const app = connect();
const path = '/graphql';

server.use(query());
server.applyMiddleware({ app, path });

const port = 4000;

app.listen({ port }, () =>
  console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`),
);

Note; qs-middleware is only required if running outside of Meteor

Koa

const Koa = require('koa');
const { ApolloServer, gql } = require('apollo-server-koa');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

const app = new Koa();
server.applyMiddleware({ app });

const port = 3000;
const host = 'localhost';

app.listen(port, host, () =>
  console.log(`🚀 Server ready at http://${host}:${port}${server.graphqlPath}`),
);

Hapi

The code below requires Hapi 17 or higher.

const { ApolloServer, gql } = require('apollo-server-hapi');
const Hapi = require('hapi');

async function StartServer() {
  const server = new ApolloServer({ typeDefs, resolvers });

  const app = new Hapi.server({
    port: 4000,
  });

  await server.applyMiddleware({
    app,
  });

  await server.installSubscriptionHandlers(app.listener);

  await app.start();
}

StartServer().catch(error => console.log(error));

Context

The context is created for each request. The following code snippet shows the creation of a context. The arguments are the request, the request, and h, the response toolkit.

new ApolloServer({
  typeDefs,
  resolvers,
  context: async ({ request, h }) => {
    return { ... };
  },
})

AWS Lambda

Apollo Server can be run on Lambda and deployed with AWS Serverless Application Model (SAM). It requires an API Gateway with Lambda Proxy Integration.

const { ApolloServer, gql } = require('apollo-server-lambda');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

exports.graphqlHandler = server.createHandler();

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
cd packages/apollo-server-<integration>/
npm link
  • Install your local Apollo Server in other App
cd ~/myApp
npm link apollo-server-<integration>