/* @flow */
/**
 *  Copyright (c) 2015, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

import { Source } from './language/source';
import { parse } from './language/parser';
import { validateDocument } from './validator';
import { execute } from './executor/executor';
import { formatError } from './error';
import type { GraphQLFormattedError } from './error/formatError';
import type { GraphQLSchema } from './type/schema';
import { GraphQLError } from './error/GraphQLError';

/**
 * This is the primary entry point function for fulfilling GraphQL operations
 * by parsing, validating, and executing a GraphQL document along side a
 * GraphQL schema.
 *
 * More sophisticated GraphQL servers, such as those which persist queries,
 * may wish to separate the validation and execution phases to a static time
 * tooling step, and a server runtime step.
 */
export function graphql(
  schema: GraphQLSchema,
  requestString: string,
  rootValue?: ?any,
  variableValues?: ?{[key: string]: any},
  operationName?: ?string
): Promise<GraphQLResult> {
  return new Promise(resolve => {
    var source = new Source(requestString || '', 'GraphQL request');
    var documentAST = parse(source);
    var validationErrors = validateDocument(schema, documentAST);
    if (validationErrors.length > 0) {
      resolve({
        errors: validationErrors.map(formatError)
      });
    } else {
      resolve(
        execute(
          schema,
          documentAST,
          rootValue,
          variableValues,
          operationName
        ).then(result => {
          if (result.errors) {
            return {
              data: result.data,
              errors: result.errors.map(formatError)
            };
          }
          return result;
        })
      );
    }
  }).catch(error => {
    if (!(error instanceof GraphQLError)) {
      throw error;
    }
    return { errors: [ formatError(error) ] };
  });
}

/**
 * The result of a GraphQL parse, validation and execution.
 *
 * `data` is the result of a successful execution of the query.
 * `errors` is included when any errors occurred as a non-empty array.
 */
type GraphQLResult = {
  data?: ?Object;
  errors?: Array<GraphQLFormattedError>;
}

@@ -655,60 +655,8 @@
 a';%0A
-import %7B GraphQLError %7D from './error/GraphQLError';
 %0A%0A/*
@@ -1969,77 +1969,8 @@
 %3E %7B%0A
-    if (!(error instanceof GraphQLError)) %7B%0A      throw error;%0A    %7D%0A
     

Revert "Improve error handling for the graphql function. This only returns the"