<commit_before>/* tslint:disable:no-unused-expression */
import MockReq = require('mock-req');

import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLNonNull,
  parse,
  DocumentNode,
  ValidationContext,
  GraphQLFieldResolver,
} from 'graphql';

import {
  GraphQLExtensionStack,
  GraphQLExtension,
  GraphQLResponse,
} from 'graphql-extensions';

import { CacheControlExtensionOptions } from 'apollo-cache-control';

import { processGraphQLRequest, GraphQLRequest } from '../requestPipeline';
import { Request } from 'apollo-server-env';

// This is a temporary kludge to ensure we preserve runQuery behavior with the
// GraphQLRequestProcessor refactoring.
// These tests will be rewritten as GraphQLRequestProcessor tests after the
// refactoring is complete.

function runQuery(options: QueryOptions): Promise<GraphQLResponse> {
  const request: GraphQLRequest = {
    query: options.queryString,
    operationName: options.operationName,
    variables: options.variables,
    extensions: options.extensions,
    http: options.request,
  };

  return processGraphQLRequest(options, {
    request,
    context: options.context || {},
    debug: options.debug,
    cache: {} as any,
  });
}

interface QueryOptions {
  schema: GraphQLSchema;

  queryString?: string;
  parsedQuery?: DocumentNode;

  rootValue?: any;
  context?: any;
  variables?: { [key: string]: any };
  operationName?: string;
  validationRules?: Array<(context: ValidationContext) => any>;
  fieldResolver?: GraphQLFieldResolver<any, any>;
  formatError?: Function;
  formatResponse?: Function;
  debug?: boolean;
  tracing?: boolean;
  cacheControl?: CacheControlExtensionOptions;
  request: Pick<Request, 'url' | 'method' | 'headers'>;
  extensions?: Array<() => GraphQLExtension>;
}

const queryType = new GraphQLObjectType({
  name: 'QueryType',
  fields: {
    testString: {
      type: GraphQLString,
      resolve() {
        return 'it works';
      },
    },
    testObject: {
      type: new GraphQLObjectType({
        name: 'TestObject',
        fields: {
          testString: {
            type: GraphQLString,
          },
        },
      }),
      resolve() {
        return {
          testString: 'a very test string',
        };
      },<commit_msg>chore: DRY-up `runQuery` test interface via Pick'd `GraphQLOptions`. (#2107)<commit_after>/* tslint:disable:no-unused-expression */
import MockReq = require('mock-req');

import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLNonNull,
  parse,
  DocumentNode,
} from 'graphql';

import {
  GraphQLExtensionStack,
  GraphQLExtension,
  GraphQLResponse,
} from 'graphql-extensions';

import { processGraphQLRequest, GraphQLRequest } from '../requestPipeline';
import { Request } from 'apollo-server-env';
import { GraphQLOptions, Context as GraphQLContext } from 'apollo-server-core';

// This is a temporary kludge to ensure we preserve runQuery behavior with the
// GraphQLRequestProcessor refactoring.
// These tests will be rewritten as GraphQLRequestProcessor tests after the
// refactoring is complete.

function runQuery(options: QueryOptions): Promise<GraphQLResponse> {
  const request: GraphQLRequest = {
    query: options.queryString,
    operationName: options.operationName,
    variables: options.variables,
    extensions: options.extensions,
    http: options.request,
  };

  return processGraphQLRequest(options, {
    request,
    context: options.context || {},
    debug: options.debug,
    cache: {} as any,
  });
}

interface QueryOptions
  extends Pick<
    GraphQLOptions<GraphQLContext<any>>,
    | 'cacheControl'
    | 'context'
    | 'debug'
    | 'extensions'
    | 'fieldResolver'
    | 'formatError'
    | 'formatResponse'
    | 'rootValue'
    | 'schema'
    | 'tracing'
    | 'validationRules'
  > {
  queryString?: string;
  parsedQuery?: DocumentNode;
  variables?: { [key: string]: any };
  operationName?: string;
  request: Pick<Request, 'url' | 'method' | 'headers'>;
}

const queryType = new GraphQLObjectType({
  name: 'QueryType',
  fields: {
    testString: {
      type: GraphQLString,
      resolve() {
        return 'it works';
      },
    },
    testObject: {
      type: new GraphQLObjectType({
        name: 'TestObject',
        fields: {
          testString: {
            type: GraphQLString,
          },
        },
      }),
      resolve() {
        return {
          testString: 'a very test string',
        };
      },
{'fraction_non_alphanumeric': 0.11670378619153675, 'fraction_numerical': 0.0008908685968819599, 'mean_word_length': 3.3224254090471605, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3016947', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1314, 'n_words': 327}