Remove packages for frameworks that don't yet support 2.0
Previously, we were planning to offer two kinds of APIs in "Apollo Server 2":
middleware functions just like in 1.0, and the new ApolloServer class. We have
not yet implemented ApolloServer for all of our supported web frameworks, so
this meant that the 2.0 version of modules such as apollo-server-koa (which does
not yet have an ApolloServer class) offered very little benefits over
apollo-server-koa@1.0 (as most of the benefits of 2.0 come from ApolloServer).
This is confusing. We are going to improve the current registerServer API so
that there's no real benefit to using a separately-exported middleware directly
rather than creating an ApolloServer and applying it to your web framework. So
the AS 2.0 API will just be ApolloServer. This means it doesn't make sense for
us to publish 2.x versions of the packages that don't yet support this API.
So this commit removes support for the following web frameworks: Adonis, Azure
Functions, Koa, Lambda, Micro, and Restify. (We leave in place Express, Hapi,
and Cloudflare workers.)
This isn't because we don't like these frameworks and don't want them to work
with Apollo Server 2.0! We would love to see each package resurrected and an
ApolloServer implementation built, either during this current 2.x beta phase or
after the official 2.0 release. Deleting these packages for now makes it more
clear which frameworks support 2.0 and which don't, rather than existing in a
state where apollo-server-express@2 has ApolloServer and apollo-server-koa@2
does not.
2018-06-13 13:45:10 -07:00
# GraphQL Server for Express, Connect, Hapi, Cloudflare workers, and more
2016-04-24 16:00:42 -07:00
2017-07-17 16:29:40 -07:00
[](https://badge.fury.io/js/apollo-server-core)
2018-06-26 16:47:33 +02:00
[](https://circleci.com/gh/apollographql/apollo-server)
2017-12-11 23:11:11 -08:00
[](https://www.apollographql.com/#slack )
2016-04-24 16:00:42 -07:00
2017-12-11 23:44:32 -08:00
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 ](https://github.com/graphql/graphql-js ), so you can build your schema with that directly or with a convenience library such as [graphql-tools ](https://www.apollographql.com/docs/graphql-tools/ ).
2016-04-24 16:00:42 -07:00
2017-12-11 21:14:32 -08:00
## Documentation
2018-06-22 17:51:56 -07:00
[Read the docs! ](https://www.apollographql.com/docs/apollo-server/v2 )
2017-12-11 21:14:32 -08:00
2016-08-01 12:54:47 -07:00
## Principles
2016-04-24 16:00:42 -07:00
2017-07-17 16:29:40 -07:00
Apollo Server is built with the following principles in mind:
2016-06-10 22:22:38 -07:00
2018-06-21 16:05:23 +02:00
- **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
2016-06-10 22:22:38 -07:00
2017-07-17 16:29:40 -07:00
Anyone is welcome to contribute to Apollo Server, just read [CONTRIBUTING.md ](./CONTRIBUTING.md ), take a look at the [roadmap ](./ROADMAP.md ) and make your first PR!
2016-06-10 22:22:38 -07:00
2016-08-01 12:54:47 -07:00
## Getting started
2018-06-22 17:51:56 -07:00
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 ](https://www.apollographql.com/docs/apollo-server/v2 ).
2016-08-01 12:54:47 -07:00
2016-09-29 17:46:40 +03:00
### Installation
2016-08-02 10:04:15 -07:00
2018-06-22 17:51:56 -07:00
Run `npm install --save apollo-server-<integration>` and you're good to go!
```js
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: {
2018-06-26 16:47:33 +02:00
hello: () => 'world',
},
2018-06-22 17:51:56 -07:00
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
```
2018-08-14 19:33:40 +02:00
While we recommend the use [schema-definition language (SDL) ](https://www.apollographql.com/docs/apollo-server/essentials/schema.html#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:
```js
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}` );
});
```
2018-06-22 17:51:56 -07:00
## Integrations
2016-10-05 02:11:42 +03:00
2018-11-22 16:18:03 +02:00
Often times, Apollo Server needs to be run with a particular integration. To start, run `npm install --save apollo-server-<integration>` where `apollo-server-<integration>` is one of the following:
- `apollo-server-express`
- `apollo-server-koa`
- `apollo-server-hapi`
- `apollo-server-lambda`
- `apollo-server-azure-functions`
- `apollo-server-cloud-functions`
- `apollo-server-cloudflare`
2016-08-02 10:40:40 -07:00
2016-08-01 12:54:47 -07:00
### Express
```js
2018-06-22 17:51:56 -07:00
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
2016-08-01 12:54:47 -07:00
2018-06-22 17:51:56 -07:00
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;
2016-08-01 12:54:47 -07:00
2018-06-22 17:51:56 -07:00
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
2016-08-01 12:54:47 -07:00
2018-06-22 17:51:56 -07:00
const app = express();
server.applyMiddleware({ app });
2016-08-01 12:54:47 -07:00
2018-10-10 15:26:23 +02:00
const port = 4000;
app.listen({ port }, () =>
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}` ),
2018-06-26 16:47:33 +02:00
);
2016-08-01 12:54:47 -07:00
```
### Connect
2018-01-09 00:08:01 +01:00
2016-08-01 12:54:47 -07:00
```js
2018-06-22 17:51:56 -07:00
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!',
},
};
2016-08-01 12:54:47 -07:00
2018-06-22 17:51:56 -07:00
const server = new ApolloServer({ typeDefs, resolvers });
2016-08-01 12:54:47 -07:00
2017-06-25 10:51:10 +02:00
const app = connect();
2018-06-22 17:51:56 -07:00
const path = '/graphql';
2016-08-01 12:54:47 -07:00
2018-11-16 06:35:42 -05:00
app.use(query());
2018-06-22 17:51:56 -07:00
server.applyMiddleware({ app, path });
2016-08-01 12:54:47 -07:00
2018-10-10 15:26:23 +02:00
const port = 4000;
app.listen({ port }, () =>
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}` ),
2018-06-26 16:47:33 +02:00
);
2016-08-01 12:54:47 -07:00
```
2018-06-22 17:51:56 -07:00
> Note; `qs-middleware` is only required if running outside of Meteor
2016-09-09 07:49:23 -05:00
2018-10-10 15:26:23 +02:00
### Koa
```js
2018-10-25 16:06:55 +08:00
const Koa = require('koa');
2018-10-10 15:26:23 +02:00
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}` ),
);
```
2018-06-22 17:51:56 -07:00
### Hapi
2016-09-09 07:49:23 -05:00
2018-10-10 15:26:23 +02:00
> The code below requires Hapi 17 or higher.
2017-12-08 00:04:12 -08:00
2016-08-01 12:54:47 -07:00
```js
2018-06-22 17:51:56 -07:00
const { ApolloServer, gql } = require('apollo-server-hapi');
const Hapi = require('hapi');
2016-08-01 12:54:47 -07:00
2017-12-08 00:04:12 -08:00
async function StartServer() {
2018-06-22 17:51:56 -07:00
const server = new ApolloServer({ typeDefs, resolvers });
const app = new Hapi.server({
2018-06-26 16:47:33 +02:00
port: 4000,
2018-01-09 00:08:01 +01:00
});
2018-06-22 17:51:56 -07:00
await server.applyMiddleware({
app,
2018-01-09 00:08:01 +01:00
});
2018-06-22 17:51:56 -07:00
await server.installSubscriptionHandlers(app.listener);
2018-01-09 00:08:01 +01:00
2018-06-22 17:51:56 -07:00
await app.start();
2017-12-08 00:04:12 -08:00
}
2018-06-22 17:51:56 -07:00
StartServer().catch(error => console.log(error));
2016-08-01 12:54:47 -07:00
```
2016-09-09 07:49:23 -05:00
2018-06-22 17:51:56 -07:00
### Context
2016-11-14 23:45:45 +02:00
2018-06-22 17:51:56 -07:00
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.
2018-01-09 00:08:01 +01:00
2016-10-24 11:04:18 -07:00
```js
2018-06-22 17:51:56 -07:00
new ApolloServer({
typeDefs,
resolvers,
context: async ({ request, h }) => {
return { ... };
},
})
2016-10-24 11:04:18 -07:00
```
2016-12-16 23:41:32 -06:00
### AWS Lambda
2018-06-22 17:51:56 -07:00
Apollo Server can be run on Lambda and deployed with AWS Serverless Application Model (SAM). It requires an API Gateway with Lambda Proxy Integration.
2017-04-08 14:17:49 -07:00
```js
2018-06-22 17:51:56 -07:00
const { ApolloServer, gql } = require('apollo-server-lambda');
2017-04-08 14:17:49 -07:00
2018-06-22 17:51:56 -07:00
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;
2016-08-20 20:33:20 -07:00
2018-06-22 17:51:56 -07:00
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
2018-01-09 00:08:01 +01:00
},
2016-08-20 20:33:20 -07:00
};
2016-08-01 12:54:47 -07:00
2018-06-22 17:51:56 -07:00
const server = new ApolloServer({ typeDefs, resolvers });
2016-08-01 12:54:47 -07:00
2018-06-22 17:51:56 -07:00
exports.graphqlHandler = server.createHandler();
2017-03-10 10:37:12 +01:00
```
2017-07-17 16:29:40 -07:00
## Apollo Server Development
2016-09-10 18:37:14 -04:00
2017-07-17 16:29:40 -07:00
If you want to develop Apollo Server locally you must follow the following instructions:
2016-09-10 18:37:14 -04:00
2018-06-21 16:05:23 +02:00
- Fork this repository
2016-09-10 18:37:14 -04:00
2018-06-21 16:05:23 +02:00
- Install the Apollo Server project in your computer
2016-09-10 18:37:14 -04:00
```
2017-07-17 16:29:40 -07:00
git clone https://github.com/[your-user]/apollo-server
cd apollo-server
2016-10-05 02:32:12 +03:00
npm install
2018-06-22 17:51:56 -07:00
cd packages/apollo-server-< integration > /
2016-09-10 18:37:14 -04:00
npm link
```
2018-06-21 16:05:23 +02:00
- Install your local Apollo Server in other App
2016-09-10 18:37:14 -04:00
```
cd ~/myApp
2018-06-22 17:51:56 -07:00
npm link apollo-server-< integration >
2016-09-10 18:37:14 -04:00
```
2018-11-28 08:18:48 -05:00
## Maintainers
- [@martijnwalraven ](https://github.com/martijnwalraven ) (Apollo)
- [@abernix ](https://github.com/abernix ) (Apollo)