support all apollo-server-core options and setup build

This commit is contained in:
James Baxley 2018-04-23 10:37:46 -04:00 committed by Evans Hauser
parent 35a5858125
commit 2bdc1fa19b
No known key found for this signature in database
GPG key ID: 88AF586817F52EEC
6 changed files with 112 additions and 24 deletions

1
packages/apollo-server/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
npm

View file

@ -1,4 +1,4 @@
# apollo-server-core
# Apollo Server
This is the core module of the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/)
Apollo Server is a production ready GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/)
[Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md)

View file

@ -1,12 +1,17 @@
{
"name": "apollo-server",
"version": "2.0.0-alpha.2",
"private": true,
"version": "2.0.0-beta.0",
"description": "Production ready GraphQL Server",
"author": "opensource@apollographql.com",
"main": "dist/index.js",
"scripts": {
"compile": "tsc",
"watch": "tsc -w",
"prepublish": "npm run compile"
"compile": "rimraf dist && tsc",
"deploy": "cd npm && npm publish --tag=beta",
"postcompile": "rimraf npm && mkdir npm && mv dist/* npm/. && ./scripts/prepare-package.sh",
"predeploy": "npm run compile",
"prepublish": "echo '\n\nPlease use npm run deploy to publish apollo-server\n\n' && exit 1",
"watch": "tsc -w"
},
"repository": {
"type": "git",
@ -35,16 +40,15 @@
"@types/node": "^9.6.1",
"@types/react": "^16.3.8",
"@types/ws": "^4.0.2",
"apollo-cache-control": "^0.1.1",
"apollo-engine": "^1.0.6",
"rimraf": "^2.6.2",
"typescript": "2.8.1"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
},
"typings": "dist/index.d.ts",
"typescript": {
"definition": "dist/index.d.ts"
},
"dependencies": {
"apollo-server-core": "^1.4.0-alpha.0",
"apollo-server-express": "^1.3.4",
@ -52,6 +56,7 @@
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"express": "^4.16.3",
"graphql-disable-introspection": "^1.0.1",
"graphql-playground-middleware-express": "^1.6.1",
"graphql-playground-middleware-hapi": "^1.6.1",
"graphql-playground-middleware-koa": "^1.5.1",

View file

@ -0,0 +1,26 @@
#!/bin/bash -e
# When we publish to npm, the published files are available in the root
# directory, which allows for a clean include or require of sub-modules.
#
# var language = require('apollo-server/express');
#
# Ensure a vanilla package.json before deploying so other tools do not interpret
# The built output as requiring any further transformation.
node -e "var package = require('./package.json'); \
delete package.scripts; \
delete package.private; \
delete package.devDependencies; \
package.main = 'index.js'; \
package.module = 'index.js'; \
package.typings = 'index.d.ts'; \
var origVersion = 'local';
var fs = require('fs'); \
fs.writeFileSync('./npm/package.json', JSON.stringify(package, null, 2)); \
"
# Copy few more files to ./npm
cp README.md npm/
cp ../../LICENSE npm/

View file

@ -6,8 +6,12 @@ import {
subscribe,
ExecutionResult,
GraphQLError,
GraphQLResolveInfo,
ValidationContext,
FieldDefinitionNode,
} from 'graphql';
import { formatError } from 'apollo-server-core';
import { CacheControlExtensionOptions } from 'apollo-cache-control';
import { formatError, GraphQLOptions, LogFunction } from 'apollo-server-core';
import { ApolloEngine as Engine } from 'apollo-engine';
import {
SubscriptionServer,
@ -25,12 +29,35 @@ import {
ContextFunction,
} from './types';
export {
LogFunction,
ValidationContext,
CacheControlExtensionOptions,
GraphQLResolveInfo,
};
// taken from engine
export function joinHostPort(host: string, port: number) {
if (host.includes(':')) host = `[${host}]`;
return `${host}:${port}`;
}
const env = process.env.NODE_ENV;
const isDev = env !== 'production' && env !== 'test';
const NoIntrospection = (context: ValidationContext) => ({
Field(node: FieldDefinitionNode) {
if (node.name.value === '__schema' || node.name.value === '__type') {
context.reportError(
new GraphQLError(
'GraphQL introspection is not allowed by Apollo Server, but the query containted __schema or __type. To enable introspection, pass introspection: true to ApolloServer in production',
[node],
),
);
}
},
});
export class ApolloServerBase<
Server = HttpServer,
Request = any,
@ -47,24 +74,34 @@ export class ApolloServerBase<
private graphqlEndpoint: string = '/graphql';
private cors?: Cors;
private engineEnabled: boolean = false;
private debug?: boolean;
private requestOptions: Partial<GraphQLOptions<any>>;
private disableTools: boolean = !isDev;
constructor(config: Config<Server, Request, Cors>) {
const {
typeDefs,
resolvers,
schemaDirectives,
schema,
context,
app,
context,
cors,
engine,
engineInRequestPath,
resolvers,
schema,
schemaDirectives,
subscriptions,
cors,
debug,
typeDefs,
introspection,
...requestOptions
} = config;
this.debug = debug;
if (introspection === false || !isDev) {
this.disableTools = true;
const noIntro = [NoIntrospection];
requestOptions.validationRules = requestOptions.validationRules
? requestOptions.validationRules.concat(noIntro)
: noIntro;
}
this.requestOptions = requestOptions;
this.context = context;
// XXX should we move this to the `start` call? This would make hot
// reloading eaiser but may not be worth it?
@ -151,7 +188,9 @@ ApolloServer was unable to load the configuration for Apollo Engine. Please veri
subscriptions: true,
...opts,
graphiql:
opts.graphiql === false ? null : `${opts.graphiql || '/graphiql'}`,
opts.graphiql === false || this.disableTools
? null
: `${opts.graphiql || '/graphiql'}`,
app: this.app,
request: this.request.bind(this),
};
@ -294,9 +333,11 @@ when calling this.request, either call it using an error function, or bind it li
schema: this.schema,
tracing: Boolean(this.engineEnabled),
cacheControl: Boolean(this.engineEnabled),
formatError: (e: GraphQLError) => formatError(e, Boolean(this.debug)),
debug: Boolean(this.debug),
formatError: (e: GraphQLError) =>
formatError(e, this.requestOptions.debug),
context,
// allow overrides from options
...this.requestOptions,
};
}

View file

@ -2,6 +2,7 @@ import { GraphQLSchema } from 'graphql';
import { CorsOptions } from 'cors';
import { SchemaDirectiveVisitor, IResolvers } from 'graphql-tools';
import { ConnectionContext } from 'subscriptions-transport-ws';
import { GraphQLOptions } from 'apollo-server-core';
export type Context<T = any> = T;
export type ContextFunction<T = any> = (
@ -15,7 +16,21 @@ export interface SubscriptionServerOptions {
keepAlive?: number;
}
export interface Config<Server, ContextShape = any, Cors = CorsOptions> {
export interface Config<Server, ContextShape = any, Cors = CorsOptions>
extends Pick<
GraphQLOptions<Context<ContextShape>>,
| 'formatError'
| 'debug'
| 'rootValue'
| 'logFunction'
| 'formatParams'
| 'validationRules'
| 'formatResponse'
| 'fieldResolver'
| 'debug'
| 'cacheControl'
| 'tracing'
> {
app?: Server;
typeDefs?: string | [string];
resolvers?: IResolvers;
@ -26,7 +41,7 @@ export interface Config<Server, ContextShape = any, Cors = CorsOptions> {
subscriptions?: SubscriptionServerOptions | string | false;
engineInRequestPath?: boolean;
engine?: boolean | Object;
debug?: boolean;
introspection?: boolean;
}
export interface EngineConfig {