Skip to content

Commit

Permalink
Update Docs, Add Example, Fix Header Bug
Browse files Browse the repository at this point in the history
fixes #2

Added `node-fetch` to polyfill the DOM Headers API.

Changed server response to use `res.send()` instead of `res.json()` because it was causing the response to be sent as a string.

Minor config changes to ignore files in testing/builds.
  • Loading branch information
Saeris committed Aug 20, 2020
1 parent 5538b73 commit e2b779e
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ serverless.env.yml

coverage.lcov
engine.json

.vercel
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
## 📦 Installation

```bash
npm install --save @saeris/apollo-server-vercel
npm install --save @saeris/apollo-server-vercel graphql
# or
yarn add @saeris/apollo-server-vercel
yarn add @saeris/apollo-server-vercel graphql
```

## 🔧 Usage
Expand Down Expand Up @@ -49,9 +49,33 @@ const server = new ApolloServer({
export default server.createHandler();
```

## 🏖️ Example
## 🕹️ Demo

Give it a try [via CodeSandbox](https://codesandbox.io/s/apollo-server-vercel-demo-oumls?file=/pages/api/demo.ts)!
You can give it a try [via CodeSandbox](https://codesandbox.io/s/apollo-server-vercel-demo-oumls?file=/pages/api/demo.ts) or locally by cloning this repo, running `yarn && yarn start`, and then navigate to the URL provided in your terminal (usually http://localhost:3000/api/example).

## 🧪 Testing

Testing is provided via `jest` and is pre-configured to run with `codecov` as well. Tests for this project have been adapted from the official Apollo Server integration tests and they can be found under `src/__test__`. Additionally, this project uses `eslint`, `typescript`, and `prettier`, all three of which are automatically run on each commit via `husky` + `lint-staged`. To manually lint and test, use the following commands:

Lint:
```bash
yarn lint
```

Typecheck:
```bash
yarn typecheck
```

Test and watch for changes:
```bash
yarn test:watch
```

Lint + Typecheck + Test:
```bash
yarn test
```

## 🥂 License

Expand Down
29 changes: 29 additions & 0 deletions api/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApolloServer } from "../pkg";

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

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

const server = new ApolloServer({
typeDefs,
resolvers,
// By default, the GraphQL Playground interface and GraphQL introspection
// is disabled in "production" (i.e. when `process.env.NODE_ENV` is `production`).
//
// If you'd like to have GraphQL Playground and introspection enabled in production,
// the `playground` and `introspection` options must be set explicitly to `true`.
playground: true,
introspection: true
});

export default server.createHandler();
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ module.exports = {
displayName: `graphql-scalars`,
coverageDirectory: `./.coverage/`,
collectCoverage: true,
collectCoverageFrom: [
// include
`./src/**/*.ts`,
// exclude
`!**/__mocks__/**/*`,
`!**/__test__/**/*`,
`!**/node_modules/**`,
`!**/vendor/**`
],
testEnvironment: `node`,
transform: {
"^.+\\.(js|ts)x?$": `babel-jest`
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
"access": "public"
},
"main": "src/index.js",
"engines": {
"node": ">=10.x"
},
"scripts": {
"build": "pika build",
"start": "vercel dev",
"build": "yarn build:lib && yarn build:docs",
"build:lib": "pika build",
"build:docs": "fiddly",
"typecheck": "tsc --noEmit",
"lint": "eslint ./src/**/*.{js,ts}",
Expand Down Expand Up @@ -83,9 +88,11 @@
"js-sha256": "^0.9.0",
"lint-staged": "^10.2.11",
"micro": "^9.3.4",
"node-fetch": "^2.6.0",
"prettier": "2.0.5",
"supertest": "^4.0.2",
"typescript": "^3.9.7",
"vercel": "^19.2.0",
"vercel-node-server": "^2.2.1"
},
"peerDependencies": {
Expand All @@ -98,7 +105,8 @@
{
"exclude": [
"*.gql",
"__TEST__/*"
"__mocks__/**/*",
"__test__/**/*"
]
}
],
Expand Down
6 changes: 6 additions & 0 deletions sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"infiniteLoopProtection": true,
"hardReloadOnChange": false,
"view": "browser",
"template": "node"
}
3 changes: 2 additions & 1 deletion src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RenderPageOptions as PlaygroundRenderPageOptions
} from "@apollographql/graphql-playground-html";
import { NowRequest, NowResponse } from "@vercel/node";
import { Headers } from 'node-fetch';
import { graphqlVercel } from "./vercelApollo";
import { setHeaders } from "./setHeaders";

Expand Down Expand Up @@ -174,7 +175,7 @@ export class ApolloServer extends ApolloServerBase {
}
};

fileUploadHandler(() =>
return fileUploadHandler(() =>
graphqlVercel(async () => {
await promiseWillStart;
return this.createGraphQLServerOptions(req, res);
Expand Down
2 changes: 1 addition & 1 deletion src/vercelApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function graphqlVercel(options: GraphQLOptions | NowGraphQLOptionsFunctio
request: convertNodeHttpToRequest(req)
});
setHeaders(res, responseInit.headers ?? {});
return res.status(200).json(graphqlResponse);
return res.status(200).send(graphqlResponse);
} catch (error) {
const { headers, statusCode, message }: HttpQueryError = error;
setHeaders(res, headers ?? {});
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"sourceMap": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
"exclude": ["node_modules", "**/__mocks__/**/*", "**/__test__/**/*", "**/*.spec.ts"]
}
10 changes: 10 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "apollo-server-vercel",
"version": 2,
"scope": "saeris",
"functions": {
"api/example.ts": {
"maxDuration": 5
}
}
}
Loading

1 comment on commit e2b779e

@vercel
Copy link

@vercel vercel bot commented on e2b779e Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.