Skip to content

Commit

Permalink
Merge branch 'version-2' into server-2.0/avoid-global-env
Browse files Browse the repository at this point in the history
  • Loading branch information
evans committed Jun 27, 2018
2 parents b654b5b + 8ef1818 commit 1dc3459
Show file tree
Hide file tree
Showing 42 changed files with 798 additions and 133 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GraphQL Server for Express, Connect, Hapi, Cloudflare workers, and more

[![npm version](https://badge.fury.io/js/apollo-server-core.svg)](https://badge.fury.io/js/apollo-server-core)
[![Build Status](https://circleci.com/gh/apollographql/apollo-cache-control-js.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-cache-control-js)
[![Build Status](https://circleci.com/gh/apollographql/apollo-server.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server)
[![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://www.apollographql.com/#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](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/).
Expand Down Expand Up @@ -42,8 +42,8 @@ const typeDefs = gql`
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => 'world'
}
hello: () => 'world',
},
};

const server = new ApolloServer({
Expand Down Expand Up @@ -89,8 +89,8 @@ const app = express();
server.applyMiddleware({ app });

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

### Connect
Expand Down Expand Up @@ -123,8 +123,8 @@ server.use(query());
server.applyMiddleware({ app, path });

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

> Note; `qs-middleware` is only required if running outside of Meteor
Expand All @@ -141,7 +141,7 @@ async function StartServer() {
const server = new ApolloServer({ typeDefs, resolvers });

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

await server.applyMiddleware({
Expand Down
3 changes: 2 additions & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sidebar_categories:
- features/data-sources
- features/cdn
- features/subscriptions
- features/file-uploads
- features/metrics
- features/scalars-enums
- features/unions-interfaces
Expand All @@ -35,7 +36,7 @@ sidebar_categories:
Deployment:
# - deployment/index
- deployment/heroku
# - deployment/lambda
- deployment/lambda
API Reference:
- api/apollo-server
- title: graphql-subscriptions
Expand Down
75 changes: 74 additions & 1 deletion docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ new ApolloServer({

An executable GraphQL schema that will override the `typeDefs` and `resolvers` provided


* `subscriptions`: <`Object`> | <`String`> | false

String defining the path for subscriptions or an Object to customize the subscriptions server. Set to false to disable subscriptions
Expand All @@ -79,6 +78,10 @@ new ApolloServer({
* `onConnect`: <`Function`>
* `onDisconnect`: <`Function`>

* `engine`: <`EngineReportingOptions`> | boolean _(required)_

Provided the `ENGINE_API_KEY` environment variable is set, the engine reporting agent will be started. The API key can also be provided as the `apiKey` field in an object passed as the `engine` field. See the [EngineReportingOptions](#EngineReportingOptions) section for a full description of how to configure the reporting agent, including how to blacklist variables

#### Returns

`ApolloServer`
Expand Down Expand Up @@ -239,3 +242,73 @@ addMockFunctionsToSchema({
preserveResolvers: false,
});
```

## `EngineReportingOptions`

* `apiKey`: string __(required)__

API key for the service. Get this from
[Engine](https://engine.apollographql.com) by logging in and creating
a service. You may also specify this with the `ENGINE_API_KEY`
environment variable the option takes precedence.

* `calculateSignature`: (ast: DocumentNode, operationName: string) => string

Specify the function for creating a signature for a query. See signature.ts
for details.

* `reportIntervalMs`: number

How often to send reports to the Engine server. We'll also send reports
when the report gets big see maxUncompressedReportSize.

* `maxUncompressedReportSize`: number

We send a report when the report size will become bigger than this size in
bytes (default: 4MB). (This is a rough limit --- we ignore the size of the
report header and some other top level bytes. We just add up the lengths of
the serialized traces and signatures.)

* `endpointUrl`: string

The URL of the Engine report ingress server.

* `debugPrintReports`: boolean

If set, prints all reports as JSON when they are sent.

* `maxAttempts`: number

Reporting is retried with exponential backoff up to this many times
(including the original request). Defaults to 5.

* `minimumRetryDelayMs`: number

Minimum backoff for retries. Defaults to 100ms.

* `reportErrorFunction`: (err: Error) => void

By default, errors sending reports to Engine servers will be logged
to standard error. Specify this function to process errors in a different
way.

* `privateVariables`: Array<String> | boolean

A case-sensitive list of names of variables whose values should not be sent
to Apollo servers, or 'true' to leave out all variables. In the former
case, the report will indicate that each private variable was redacted in
the latter case, no variables are sent at all.

* `privateHeaders`: Array<String> | boolean

A case-insensitive list of names of HTTP headers whose values should not be
sent to Apollo servers, or 'true' to leave out all HTTP headers. Unlike
with privateVariables, names of dropped headers are not reported.

* `handleSignals`: boolean

By default, EngineReportingAgent listens for the 'SIGINT' and 'SIGTERM'
signals, stops, sends a final report, and re-sends the signal to
itself. Set this to false to disable. You can manually invoke 'stop()' and
'sendReport()' on other signals if you'd like. Note that 'sendReport()'
does not run synchronously so it cannot work usefully in an 'exit' handler.
4 changes: 2 additions & 2 deletions docs/source/deployment/lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AWS Lambda is a service that lets you run code without provisioning or managing
Learn how to integrate Apollo Server 2 with AWS Lambda. First, install the `apollo-server-lambda` package:

```sh
npm install apollo-server-lambda@rc
npm install apollo-server-lambda@rc graphql
```

## Deploying with AWS Serverless Application Model (SAM)
Expand Down Expand Up @@ -193,4 +193,4 @@ exports.graphqlHandler = server.createHandler({
credentials: true,
},
});
```
```
2 changes: 1 addition & 1 deletion docs/source/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To install, run:

When adding Apollo Server to an existing application, a corresponding HTTP server support package needs to be installed as well. For example, for Express this is:

npm install --save apollo-server-express@rc
npm install --save apollo-server-express@rc graphql

> Note: During the release candidate period, it's necessary to use the `rc` npm package, as shown in the above commands.
Expand Down
2 changes: 1 addition & 1 deletion docs/source/features/cdn.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: CDN Integration
title: CDN integration
description: Getting content delivery networks to cache GraphQL responses
---

Expand Down
65 changes: 61 additions & 4 deletions docs/source/features/data-sources.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Data Sources
title: Data sources
description: Caching Partial Query Results
---

Expand All @@ -21,7 +21,10 @@ To define a data source, extend the `RESTDataSource` class and implement the dat
const { RESTDataSource } = require('apollo-datasource-rest');

class MoviesAPI extends RESTDataSource {
baseURL = 'https://movies-api.example.com';
constructor() {
super();
this.baseURL = 'https://movies-api.example.com/';
}

async getMovie(id) {
return this.get(`movies/${id}`);
Expand All @@ -37,11 +40,62 @@ class MoviesAPI extends RESTDataSource {
}
```

### Supported Methods

The `get` method on the `RESTDataSource` makes an HTTP `GET` request. Similarly, there are methods built-in to allow for `POST`, `PUT`, `PATCH`, and `DELETE` requests.

```js
class MoviesAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://movies-api.example.com/';
}

// an example making an HTTP POST request
async postMovie(movie) {
return this.post(
`movies`, // path
movie, // request body
);
}

// an example making an HTTP PUT request
async newMovie(movie) {
return this.put(
`movies`, // path
movie, // request body
);
}

// an example making an HTTP PATCH request
async updateMovie(movie) {
return this.patch(
`movies`, // path
{ id: movie.id, movie }, // request body
);
}

// an example making an HTTP DELETE request
async deleteMovie(movie) {
return this.delete(
`movies/${movie.id}`, // path
);
}
}
```

All of the HTTP helper functions (`get`, `put`, `post`, `patch`, and `delete`) accept a third `options` parameter, which can be used to set things like headers and referrers. For more info on the options available, see MDN's [fetch docs](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters).

### Example API

Data sources allow you to intercept fetches to set headers or make other changes to the outgoing request. This is most often used for authorization. Data sources also get access to the GraphQL execution context, which is a great place to store a user token or other information you need to have available.

```js
class PersonalizationAPI extends RESTDataSource {
baseURL = 'https://personalization-api.example.com';
constructor() {
super();
this.baseURL = 'https://personalization-api.example.com/';
}

willSendRequest(request) {
request.headers.set('Authorization', this.context.token);
Expand Down Expand Up @@ -110,7 +164,10 @@ Our recommendation is to restrict batching to requests that can't be cached. In

```js
class PersonalizationAPI extends RESTDataSource {
baseURL = 'https://personalization-api.example.com';
constructor() {
super();
this.baseURL = 'https://personalization-api.example.com/';
}

willSendRequest(request) {
request.headers.set('Authorization', this.context.token);
Expand Down
Loading

0 comments on commit 1dc3459

Please sign in to comment.