apollo-server-koa
Advanced tools
Comparing version 1.3.2 to 1.3.3
{ | ||
"name": "apollo-server-koa", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Production-ready Node.js GraphQL server for Koa", | ||
@@ -28,14 +28,14 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"apollo-server-core": "^1.3.2", | ||
"apollo-server-module-graphiql": "^1.3.0" | ||
"apollo-server-core": "^1.3.3", | ||
"apollo-server-module-graphiql": "^1.3.3" | ||
}, | ||
"devDependencies": { | ||
"@types/graphql": "0.11.7", | ||
"@types/koa": "2.0.43", | ||
"@types/koa-bodyparser": "3.0.26", | ||
"@types/graphql": "0.12.6", | ||
"@types/koa": "2.0.44", | ||
"@types/koa-bodyparser": "4.2.0", | ||
"@types/koa-router": "7.0.27", | ||
"apollo-server-integration-testsuite": "^1.3.2", | ||
"koa": "2.4.1", | ||
"apollo-server-integration-testsuite": "^1.3.3", | ||
"koa": "2.5.0", | ||
"koa-bodyparser": "4.2.0", | ||
"koa-router": "7.3.0" | ||
"koa-router": "7.4.0" | ||
}, | ||
@@ -42,0 +42,0 @@ "typings": "dist/index.d.ts", |
@@ -45,5 +45,8 @@ --- | ||
// Setup the /graphiql route to show the GraphiQL UI | ||
router.get('/graphiql', graphiqlKoa({ | ||
endpointURL: '/graphql' // a POST endpoint that GraphiQL will make the actual requests to | ||
})); | ||
router.get( | ||
'/graphiql', | ||
graphiqlKoa({ | ||
endpointURL: '/graphql', // a POST endpoint that GraphiQL will make the actual requests to | ||
}), | ||
); | ||
``` | ||
@@ -54,9 +57,13 @@ | ||
For example: | ||
```js | ||
import { graphiqlKoa } from 'apollo-server-koa'; | ||
router.get('/graphiql', graphiqlKoa({ | ||
router.get( | ||
'/graphiql', | ||
graphiqlKoa({ | ||
endpointURL: '/graphql', | ||
passHeader: `'Authorization': 'Bearer lorem ipsum'` | ||
})); | ||
passHeader: `'Authorization': 'Bearer lorem ipsum'`, | ||
}), | ||
); | ||
``` | ||
@@ -72,3 +79,2 @@ | ||
Anyone is welcome to contribute to Apollo Server, just read [CONTRIBUTING.md](https://github.com/apollographql/apollo-server/blob/master/CONTRIBUTING.md), take a look at the [roadmap](https://github.com/apollographql/apollo-server/blob/master/ROADMAP.md) and make your first PR! |
@@ -9,3 +9,6 @@ import * as koa from 'koa'; | ||
import testSuite, { schema as Schema, CreateAppOptions } from 'apollo-server-integration-testsuite'; | ||
import testSuite, { | ||
schema as Schema, | ||
CreateAppOptions, | ||
} from 'apollo-server-integration-testsuite'; | ||
@@ -21,7 +24,7 @@ function createApp(options: CreateAppOptions = {}) { | ||
} | ||
if (options.graphiqlOptions ) { | ||
router.get('/graphiql', graphiqlKoa( options.graphiqlOptions )); | ||
if (options.graphiqlOptions) { | ||
router.get('/graphiql', graphiqlKoa(options.graphiqlOptions)); | ||
} | ||
router.get('/graphql', graphqlKoa( options.graphqlOptions )); | ||
router.post('/graphql', graphqlKoa( options.graphqlOptions )); | ||
router.get('/graphql', graphqlKoa(options.graphqlOptions)); | ||
router.post('/graphql', graphqlKoa(options.graphqlOptions)); | ||
app.use(router.routes()); | ||
@@ -37,9 +40,12 @@ app.use(router.allowedMethods()); | ||
describe('koaApollo', () => { | ||
it('throws error if called without schema', function(){ | ||
expect(() => graphqlKoa(undefined as GraphQLOptions)).to.throw('Apollo Server requires options.'); | ||
it('throws error if called without schema', function() { | ||
expect(() => graphqlKoa(undefined as GraphQLOptions)).to.throw( | ||
'Apollo Server requires options.', | ||
); | ||
}); | ||
it('throws an error if called with more than one argument', function(){ | ||
expect(() => (<any>graphqlKoa)({}, 'x')).to.throw( | ||
'Apollo Server expects exactly one argument, got 2'); | ||
it('throws an error if called with more than one argument', function() { | ||
expect(() => (<any>graphqlKoa)({}, 'x')).to.throw( | ||
'Apollo Server expects exactly one argument, got 2', | ||
); | ||
}); | ||
@@ -46,0 +52,0 @@ }); |
import * as koa from 'koa'; | ||
import { GraphQLOptions, HttpQueryError, runHttpQuery } from 'apollo-server-core'; | ||
import { | ||
GraphQLOptions, | ||
HttpQueryError, | ||
runHttpQuery, | ||
} from 'apollo-server-core'; | ||
import * as GraphiQL from 'apollo-server-module-graphiql'; | ||
@@ -13,3 +17,5 @@ | ||
export function graphqlKoa(options: GraphQLOptions | KoaGraphQLOptionsFunction): KoaHandler { | ||
export function graphqlKoa( | ||
options: GraphQLOptions | KoaGraphQLOptionsFunction, | ||
): KoaHandler { | ||
if (!options) { | ||
@@ -20,3 +26,5 @@ throw new Error('Apollo Server requires options.'); | ||
if (arguments.length > 1) { | ||
throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`); | ||
throw new Error( | ||
`Apollo Server expects exactly one argument, got ${arguments.length}`, | ||
); | ||
} | ||
@@ -28,20 +36,24 @@ | ||
options: options, | ||
query: ctx.request.method === 'POST' ? ctx.request.body : ctx.request.query, | ||
}).then((gqlResponse) => { | ||
ctx.set('Content-Type', 'application/json'); | ||
ctx.body = gqlResponse; | ||
}, (error: HttpQueryError) => { | ||
if ( 'HttpQueryError' !== error.name ) { | ||
throw error; | ||
} | ||
query: | ||
ctx.request.method === 'POST' ? ctx.request.body : ctx.request.query, | ||
}).then( | ||
gqlResponse => { | ||
ctx.set('Content-Type', 'application/json'); | ||
ctx.body = gqlResponse; | ||
}, | ||
(error: HttpQueryError) => { | ||
if ('HttpQueryError' !== error.name) { | ||
throw error; | ||
} | ||
if ( error.headers ) { | ||
Object.keys(error.headers).forEach((header) => { | ||
ctx.set(header, error.headers[header]); | ||
}); | ||
} | ||
if (error.headers) { | ||
Object.keys(error.headers).forEach(header => { | ||
ctx.set(header, error.headers[header]); | ||
}); | ||
} | ||
ctx.status = error.statusCode; | ||
ctx.body = error.message; | ||
}); | ||
ctx.status = error.statusCode; | ||
ctx.body = error.message; | ||
}, | ||
); | ||
}; | ||
@@ -54,13 +66,18 @@ } | ||
export function graphiqlKoa(options: GraphiQL.GraphiQLData | KoaGraphiQLOptionsFunction) { | ||
export function graphiqlKoa( | ||
options: GraphiQL.GraphiQLData | KoaGraphiQLOptionsFunction, | ||
) { | ||
return (ctx: koa.Context) => { | ||
const query = ctx.request.query; | ||
return GraphiQL.resolveGraphiQLString(query, options, ctx).then(graphiqlString => { | ||
ctx.set('Content-Type', 'text/html'); | ||
ctx.body = graphiqlString; | ||
}, error => { | ||
ctx.status = 500; | ||
ctx.body = error.message; | ||
}); | ||
return GraphiQL.resolveGraphiQLString(query, options, ctx).then( | ||
graphiqlString => { | ||
ctx.set('Content-Type', 'text/html'); | ||
ctx.body = graphiqlString; | ||
}, | ||
error => { | ||
ctx.status = 500; | ||
ctx.body = error.message; | ||
}, | ||
); | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12636
189
78
0