-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zeit micro support #347
Zeit micro support #347
Changes from 12 commits
151c2f4
541404e
8486cf2
f8e6419
a7c033f
e37859f
0705005
0b079e8
ee581a8
4bc01e3
95918f1
5d8fa02
acd166d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,6 @@ build/Release | |
|
||
# Dependency directory | ||
node_modules | ||
typings | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
* Restify: Fix for calling next() ([@jadkap](https://github.com/jadkap)) on [#285](https://github.com/apollostack/graphql-server/pull/285) | ||
* Update GraphiQL to version 0.9.1 ([@ephemer](https://github.com/ephemer)) on [#293](https://github.com/apollostack/graphql-server/pull/293) | ||
* Add AWS Lambda Integration [#101](https://github.com/apollostack/graphql-server/issues/101) | ||
* Add Zeit Micro Integration [#324](https://github.com/apollographql/graphql-server/issues/324) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have we really not released since adding AWS lamdba or did we (I?) just forget to update the changelog with the latest version? |
||
|
||
### v0.5.2 | ||
* **Restify integration** ([@joelgriffith](https://github.com/joelgriffith)) on [#189](https://github.com/apollostack/graphql-server/pull/189) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
* | ||
!dist | ||
!dist/**/* | ||
dist/**/*.test.* | ||
!package.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# graphql-server-micro | ||
|
||
This is the [Micro](https://github.com/zeit/micro) integration for the Apollo community GraphQL Server. [Read the docs.](http://dev.apollodata.com/tools/apollo-server/index.html) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "graphql-server-micro", | ||
"version": "0.5.1", | ||
"description": "Production-ready Node.js GraphQL server for Micro", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"compile": "tsc", | ||
"prepublish": "npm run compile" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/apollostack/graphql-server/tree/master/packages/graphql-server-micro" | ||
}, | ||
"keywords": [ | ||
"GraphQL", | ||
"Apollo", | ||
"Micro", | ||
"Server", | ||
"Javascript" | ||
], | ||
"author": "Nick Nance <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/apollostack/graphql-server/issues" | ||
}, | ||
"homepage": "https://github.com/apollostack/graphql-server#readme", | ||
"dependencies": { | ||
"graphql-server-core": "^0.6.0", | ||
"graphql-server-module-graphiql": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"graphql-server-integration-testsuite": "^0.6.0", | ||
"micro": "^7.3.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does micro package has embeded typings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no |
||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.8.0 || ^0.9.0", | ||
"micro": "^7.3.0" | ||
}, | ||
"optionalDependencies": { | ||
"@types/graphql": "^0.9.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and @types/micro if such needed (see question above :)) |
||
}, | ||
"typings": "dist/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/index.d.ts" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { MicroGraphQLOptionsFunction, | ||
microGraphql, microGraphiql } from './microApollo'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { microGraphql, microGraphiql } from './microApollo'; | ||
import 'mocha'; | ||
|
||
import * as micro from 'micro'; | ||
import testSuite, { schema as Schema, CreateAppOptions } from 'graphql-server-integration-testsuite'; | ||
|
||
function createApp(options: CreateAppOptions) { | ||
if (options && options.graphiqlOptions ) { | ||
return micro(microGraphiql( options.graphiqlOptions )); | ||
} else { | ||
const graphqlOptions = (options && options.graphqlOptions) || { schema: Schema }; | ||
return micro(microGraphql(graphqlOptions)); | ||
} | ||
} | ||
|
||
describe('integration:Micro', () => { | ||
testSuite(createApp); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { GraphQLOptions, HttpQueryError, runHttpQuery } from 'graphql-server-core'; | ||
import * as GraphiQL from 'graphql-server-module-graphiql'; | ||
import { json } from 'micro'; | ||
import * as url from 'url'; | ||
import {IncomingMessage, ServerResponse} from 'http'; | ||
|
||
export interface MicroGraphQLOptionsFunction { | ||
(req?: IncomingMessage): GraphQLOptions | Promise<GraphQLOptions>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the |
||
} | ||
|
||
export function microGraphql(options: GraphQLOptions | MicroGraphQLOptionsFunction) { | ||
if (!options) { | ||
throw new Error('Apollo Server requires options.'); | ||
} | ||
|
||
if (arguments.length > 1) { | ||
throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`); | ||
} | ||
|
||
return async function (req: IncomingMessage, res: ServerResponse) { | ||
let query; | ||
if (req.method === 'POST') { | ||
try { | ||
query = await json(req); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await on json? oh? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. json is a function supplied with micro that is an async parser. the reason it is async is because it will use the request as a stream and will return the complete json once the full request payload is received. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh cool :) |
||
} catch (err) { | ||
query = undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and then? i mean, where do you expect the server to respond with an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually by setting query to undefined it causes the runHttpQuery function to trigger an error. This is fundamentally what happens with the express middleware when the body is empty or not parsable |
||
} | ||
} else { | ||
query = url.parse(req.url, true).query; | ||
} | ||
|
||
runHttpQuery([req, res], { | ||
method: req.method, | ||
options: options, | ||
query: query, | ||
}).then((gqlResponse) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(gqlResponse); | ||
res.end(); | ||
}, (error: HttpQueryError) => { | ||
if ( 'HttpQueryError' !== error.name ) { | ||
throw error; | ||
} | ||
|
||
if ( error.headers ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something we added recently? I don't remember having this in the initial versions. What are people using those headers for? |
||
Object.keys(error.headers).forEach((header) => { | ||
res.setHeader(header, error.headers[header]); | ||
}); | ||
} | ||
|
||
res.statusCode = error.statusCode; | ||
res.write(error.message); | ||
res.end(); | ||
}); | ||
}; | ||
} | ||
|
||
export function microGraphiql(options: GraphiQL.GraphiQLData) { | ||
return (req: IncomingMessage, res: ServerResponse) => { | ||
const q = req.url && url.parse(req.url, true).query || {}; | ||
const query = q.query || ''; | ||
const operationName = q.operationName || ''; | ||
|
||
const graphiQLString = GraphiQL.renderGraphiQL({ | ||
endpointURL: options.endpointURL, | ||
query: query || options.query, | ||
variables: q.variables && JSON.parse(q.variables) || options.variables, | ||
operationName: operationName || options.operationName, | ||
passHeader: options.passHeader, | ||
}); | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.write(graphiQLString); | ||
res.end(); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./dist", | ||
"typeRoots": [ | ||
"node_modules/@types" | ||
] | ||
}, | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ require('../packages/graphql-server-express/dist/connectApollo.test'); | |
require('../packages/graphql-server-hapi/dist/hapiApollo.test'); | ||
if (NODE_MAJOR_VERSION >= 6) { | ||
require('../packages/graphql-server-koa/dist/koaApollo.test'); | ||
require('../packages/graphql-server-micro/dist/microApollo.test'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. micro does not support es5? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no. it uses async await. |
||
} | ||
require('../packages/graphql-server-restify/dist/restifyApollo.test'); | ||
require('../packages/graphql-server-lambda/dist/lambdaApollo.test'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why? :O
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this old / left over from the old typings from the old version of typescript?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, but sometimes people still downloading typings locally, just wanted to make sure you didnt remove this to commit typings into the repo