Skip to content

Commit 74134b9

Browse files
committed
initial
0 parents  commit 74134b9

File tree

10 files changed

+1042
-0
lines changed

10 files changed

+1042
-0
lines changed

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
!src/**/*
3+
!dist/**/*
4+
dist/**/*.test.*
5+
!package.json
6+
!README.md

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# apollo-server-core
2+
3+
This is the core module of the Apollo community GraphQL Server. [Read the docs.](https://www.apollographql.com/docs/apollo-server/)
4+
[Read the CHANGELOG.](https://github.com/apollographql/apollo-server/blob/master/CHANGELOG.md)

package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "apollo-server-core",
3+
"version": "1.3.6",
4+
"description": "Core engine for Apollo GraphQL server",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"compile": "tsc",
8+
"watch": "tsc -w",
9+
"prepublish": "npm run compile"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-core"
14+
},
15+
"keywords": [
16+
"GraphQL",
17+
"Apollo",
18+
"Server",
19+
"Javascript"
20+
],
21+
"author": "Jonas Helfer <jonas@helfer.email>",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/apollographql/apollo-server/issues"
25+
},
26+
"homepage": "https://github.com/apollographql/apollo-server#readme",
27+
"devDependencies": {
28+
"@types/fibers": "0.0.30",
29+
"@types/graphql": "0.12.7",
30+
"fibers": "1.0.15",
31+
"meteor-promise": "0.8.6",
32+
"typescript": "2.8.3"
33+
},
34+
"peerDependencies": {
35+
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0"
36+
},
37+
"typings": "dist/index.d.ts",
38+
"typescript": {
39+
"definition": "dist/index.d.ts"
40+
},
41+
"dependencies": {
42+
"apollo-cache-control": "^0.1.0",
43+
"apollo-tracing": "^0.1.0",
44+
"graphql-extensions": "^0.0.x"
45+
}
46+
}

src/graphqlOptions.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
GraphQLSchema,
3+
ValidationContext,
4+
GraphQLFieldResolver,
5+
} from 'graphql';
6+
import { LogFunction } from './runQuery';
7+
import { GraphQLExtension } from 'graphql-extensions';
8+
import { CacheControlExtensionOptions } from 'apollo-cache-control';
9+
10+
/*
11+
* GraphQLServerOptions
12+
*
13+
* - schema: an executable GraphQL schema used to fulfill requests.
14+
* - (optional) formatError: Formatting function applied to all errors before response is sent
15+
* - (optional) rootValue: rootValue passed to GraphQL execution
16+
* - (optional) context: the context passed to GraphQL execution
17+
* - (optional) logFunction: a function called for logging events such as execution times
18+
* - (optional) formatParams: a function applied to the parameters of every invocation of runQuery
19+
* - (optional) validationRules: extra validation rules applied to requests
20+
* - (optional) formatResponse: a function applied to each graphQL execution result
21+
* - (optional) fieldResolver: a custom default field resolver
22+
* - (optional) debug: a boolean that will print additional debug logging if execution errors occur
23+
*
24+
*/
25+
export interface GraphQLServerOptions<TContext = any> {
26+
schema: GraphQLSchema;
27+
formatError?: Function;
28+
rootValue?: any;
29+
context?: TContext;
30+
logFunction?: LogFunction;
31+
formatParams?: Function;
32+
validationRules?: Array<(context: ValidationContext) => any>;
33+
formatResponse?: Function;
34+
fieldResolver?: GraphQLFieldResolver<any, TContext>;
35+
debug?: boolean;
36+
tracing?: boolean;
37+
cacheControl?: boolean | CacheControlExtensionOptions;
38+
}
39+
40+
export default GraphQLServerOptions;
41+
42+
export async function resolveGraphqlOptions(
43+
options: GraphQLServerOptions | Function,
44+
...args
45+
): Promise<GraphQLServerOptions> {
46+
if (typeof options === 'function') {
47+
try {
48+
return await options(...args);
49+
} catch (e) {
50+
throw new Error(`Invalid options provided to ApolloServer: ${e.message}`);
51+
}
52+
} else {
53+
return options;
54+
}
55+
}

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export {
2+
runQuery,
3+
LogFunction,
4+
LogMessage,
5+
LogStep,
6+
LogAction,
7+
} from './runQuery';
8+
export { runHttpQuery, HttpQueryRequest, HttpQueryError } from './runHttpQuery';
9+
export {
10+
default as GraphQLOptions,
11+
resolveGraphqlOptions,
12+
} from './graphqlOptions';

src/runHttpQuery.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* tslint:disable:no-unused-expression */
2+
import { expect } from 'chai';
3+
import { stub } from 'sinon';
4+
import 'mocha';
5+
6+
import {
7+
GraphQLSchema,
8+
GraphQLObjectType,
9+
GraphQLString,
10+
GraphQLInt,
11+
} from 'graphql';
12+
13+
import { runHttpQuery, HttpQueryError } from './runHttpQuery';
14+
15+
const queryType = new GraphQLObjectType({
16+
name: 'QueryType',
17+
fields: {
18+
testString: {
19+
type: GraphQLString,
20+
resolve() {
21+
return 'it works';
22+
},
23+
},
24+
},
25+
});
26+
27+
const schema = new GraphQLSchema({
28+
query: queryType,
29+
});
30+
31+
describe('runHttpQuery', () => {
32+
describe('handling a GET query', () => {
33+
const mockQueryRequest = {
34+
method: 'GET',
35+
query: {
36+
query: '{ testString }',
37+
},
38+
options: {
39+
schema,
40+
},
41+
};
42+
43+
it('raises a 400 error if the query is missing', () => {
44+
const noQueryRequest = Object.assign({}, mockQueryRequest, {
45+
query: 'foo',
46+
});
47+
return runHttpQuery([], noQueryRequest).catch(err => {
48+
expect(err.statusCode).to.equal(400);
49+
expect(err.message).to.equal('Must provide query string.');
50+
});
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)