Skip to content

Commit

Permalink
update exports for es6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
nnance committed Jun 18, 2016
1 parent 84bcb4d commit ce4e4ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
49 changes: 21 additions & 28 deletions src/core/runQuery.ts
Expand Up @@ -7,58 +7,51 @@ import {
execute,
} from 'graphql';

import { Promise } from 'es6-promise';

export interface GqlResponse {
data?: Object;
errors?: Array<string>;
}

function runQuery({
schema,
query,
rootValue,
context,
variables,
operationName,
}: {
schema: GraphQLSchema,
query: string | Document,
rootValue?: any,
context?: any,
variables?: { [key: string]: any },
operationName?: string,
//logFunction?: function => void
//validationRules?: No, too risky. If you want extra validation rules, then parse it yourself.
}): Promise<GraphQLResult> {
export interface QueryOptions {
schema: GraphQLSchema;
query: string | Document;
rootValue?: any;
context?: any;
variables?: { [key: string]: any };
operationName?: string;
//logFunction?: function => void
//validationRules?: No, too risky. If you want extra validation rules, then parse it yourself.
}

function runQuery(options: QueryOptions): Promise<GraphQLResult> {
let documentAST: Document;

// if query is already an AST, don't parse or validate
if (typeof query === 'string') {
if (typeof options.query === 'string') {
// parse
try {
documentAST = parse(query);
documentAST = parse(options.query as string);
} catch (syntaxError) {
return Promise.resolve({ errors: [syntaxError] });
}

// validate
const validationErrors = validate(schema, documentAST);
const validationErrors = validate(options.schema, documentAST);
if (validationErrors.length) {
return Promise.resolve({ errors: validationErrors });
}
} else {
documentAST = query;
documentAST = options.query as Document;
}

// execute
return execute(
schema,
options.schema,
documentAST,
rootValue,
context,
variables,
operationName
options.rootValue,
options.context,
options.variables,
options.operationName
);
}

Expand Down
12 changes: 8 additions & 4 deletions src/integrations/expressApollo.ts
Expand Up @@ -2,7 +2,7 @@ import * as express from 'express';
import * as graphql from 'graphql';
import { runQuery } from '../core/runQuery';

import { renderGraphiQL, GraphiQLData } from '../modules/renderGraphiQL';
import * as GraphiQL from '../modules/renderGraphiQL';

// TODO: will these be the same or different for other integrations?
export interface ExpressApolloOptions {
Expand All @@ -15,7 +15,11 @@ export interface ExpressApolloOptions {
// answer: yes, it does. Func(req) => options
}

export function graphqlHTTP(options: ExpressApolloOptions) {
export interface ExpressHandler {
(req: express.Request, res: express.Response, next): void;
}

export function graphqlHTTP(options: ExpressApolloOptions): ExpressHandler {
if (!options) {
throw new Error('Apollo graphqlHTTP middleware requires options.');
}
Expand Down Expand Up @@ -46,9 +50,9 @@ function getQueryString(req: express.Request): string {

// this returns the html for the GraphiQL interactive query UI
// TODO: it's still missing a way to tell it where the GraphQL endpoint is.
export function renderGraphiQL(options: GraphiQLData) {
export function renderGraphiQL(options: GraphiQL.GraphiQLData) {
return (req: express.Request, res: express.Response, next) => {
const graphiQLString = renderGraphiQL({
const graphiQLString = GraphiQL.renderGraphiQL({
query: options.query,
variables: options.variables,
operationName: options.operationName,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
Expand Down
1 change: 0 additions & 1 deletion typings.json
Expand Up @@ -5,7 +5,6 @@
},
"globalDependencies": {
"body-parser": "registry:dt/body-parser#0.0.0+20160317120654",
"es6-promise": "registry:dt/es6-promise#0.0.0+20160317120654",
"express": "registry:dt/express#4.0.0+20160317120654",
"express-serve-static-core": "registry:dt/express-serve-static-core#0.0.0+20160322035842",
"hapi": "registry:dt/hapi#13.0.0+20160602125023",
Expand Down

0 comments on commit ce4e4ab

Please sign in to comment.