<commit_before>/**
 * @see https://www.apollographql.com/docs/apollo-server/whats-new.html
 * @see https://www.apollographql.com/docs/apollo-server/migration-two-dot.html
 */

// Meteor WebApp use a Connect server, so we need to
// use apollo-server-express integration
//import express from 'express';
import {ApolloServer} from 'apollo-server-express';

import {Meteor} from 'meteor/meteor';

import {WebApp} from 'meteor/webapp';

// import cookiesMiddleware from 'universal-cookie-express';
// import Cookies from 'universal-cookie';
import voyagerMiddleware from 'graphql-voyager/middleware/express';
import getVoyagerConfig from './voyager';
import {graphiqlMiddleware, getGraphiqlConfig} from './graphiql';
import getPlaygroundConfig from './playground';

import initGraphQL from './initGraphQL';
import './settings';
import {engineConfig} from './engine';
import {initContext, computeContextFromReq} from './context.js';

import {GraphQLSchema} from '../../modules/graphql.js';

import {enableSSR} from '../apollo-ssr';

import universalCookiesMiddleware from 'universal-cookie-express';

import {
  getApolloApplyMiddlewareOptions,
  getApolloServerOptions,
} from './settings';

import {getSetting} from '../../modules/settings.js';
import {formatError} from 'apollo-errors';

//import _merge from 'lodash/merge';
/**
 * Options: Apollo server usual options
 *
 * Config: a config specific to Vulcan
 */
const createApolloServer = ({
  apolloServerOptions = {}, // apollo options
  config = {}, // Vulcan options
  apolloApplyMiddlewareOptions, // apollo.applyMiddleware,
}) => {
  const initialContext = initContext(apolloServerOptions.context);

  // this replace the previous syntax graphqlExpress(async req => { ... })
  // this function takes the context, which contains the current request,
  // and setup the options accordingly ({req}) => { ...; return options }
  const contextFromReq = computeContextFromReq(
    initialContext,
    config.customContextFromReq
  );

  // given options contains the schema
  const apolloServer = new ApolloServer({
    engine: engineConfig,
    // graphql playground (replacement to graphiql), available on the app path
    playground: getPlaygroundConfig(config),
    // context optionbject or a function of the current request (+ maybe some other params)
    context: ({req}) => contextFromReq(req),
    debug: Meteor.isDevelopment,
    ...apolloServerOptions,
  });

  // default function does nothing
  config.configServer(apolloServer);

  // WebApp.connectHandlers is a connect server
  // you can add middlware as usual when using Express/Connect

  // parse cookies and assign req.universalCookies object
  WebApp.connectHandlers.use(universalCookiesMiddleware());

  // Provide the Meteor WebApp Connect server instance to Apollo
  // Apollo will use it instead of its own HTTP server
  apolloServer.applyMiddleware({
    // @see https://github.com/meteor/meteor/blob/master/packages/webapp/webapp_server.js
    app: WebApp.connectHandlers,
    path: config.path,
    // @see https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#Parameters-2
    ...apolloApplyMiddlewareOptions,
  });

  // setup the end point
  WebApp.connectHandlers.use(config.path, (req, res) => {
    if (req.method === 'GET') {
      res.end();
    }
  });

  /* Syntax for adding middlewares to /graphql
     Uses Connect + Meteor + Apollo
     For the list of already set middlewares (cookies, compression...), see:
    @see https://github.com/meteor/meteor/blob/master/packages/webapp/webapp_server.js<commit_msg>Add body-parser to Apollo server<commit_after>/**
 * @see https://www.apollographql.com/docs/apollo-server/whats-new.html
 * @see https://www.apollographql.com/docs/apollo-server/migration-two-dot.html
 */

// Meteor WebApp use a Connect server, so we need to
// use apollo-server-express integration
//import express from 'express';
import {ApolloServer} from 'apollo-server-express';

import {Meteor} from 'meteor/meteor';

import {WebApp} from 'meteor/webapp';
import bodyParser from 'body-parser';

// import cookiesMiddleware from 'universal-cookie-express';
// import Cookies from 'universal-cookie';
import voyagerMiddleware from 'graphql-voyager/middleware/express';
import getVoyagerConfig from './voyager';
import {graphiqlMiddleware, getGraphiqlConfig} from './graphiql';
import getPlaygroundConfig from './playground';

import initGraphQL from './initGraphQL';
import './settings';
import {engineConfig} from './engine';
import {initContext, computeContextFromReq} from './context.js';

import {GraphQLSchema} from '../../modules/graphql.js';

import {enableSSR} from '../apollo-ssr';

import universalCookiesMiddleware from 'universal-cookie-express';

import {
  getApolloApplyMiddlewareOptions,
  getApolloServerOptions,
} from './settings';

import {getSetting} from '../../modules/settings.js';
import {formatError} from 'apollo-errors';

//import _merge from 'lodash/merge';
/**
 * Options: Apollo server usual options
 *
 * Config: a config specific to Vulcan
 */
const createApolloServer = ({
  apolloServerOptions = {}, // apollo options
  config = {}, // Vulcan options
  apolloApplyMiddlewareOptions, // apollo.applyMiddleware,
}) => {
  const initialContext = initContext(apolloServerOptions.context);

  // this replace the previous syntax graphqlExpress(async req => { ... })
  // this function takes the context, which contains the current request,
  // and setup the options accordingly ({req}) => { ...; return options }
  const contextFromReq = computeContextFromReq(
    initialContext,
    config.customContextFromReq
  );

  // given options contains the schema
  const apolloServer = new ApolloServer({
    engine: engineConfig,
    // graphql playground (replacement to graphiql), available on the app path
    playground: getPlaygroundConfig(config),
    // context optionbject or a function of the current request (+ maybe some other params)
    context: ({req}) => contextFromReq(req),
    debug: Meteor.isDevelopment,
    ...apolloServerOptions,
  });

  // default function does nothing
  config.configServer(apolloServer);

  // WebApp.connectHandlers is a connect server
  // you can add middlware as usual when using Express/Connect

  // parse cookies and assign req.universalCookies object
  WebApp.connectHandlers.use(universalCookiesMiddleware());

  WebApp.connectHandlers.use(bodyParser.text({ type: 'application/graphql' }));

  // Provide the Meteor WebApp Connect server instance to Apollo
  // Apollo will use it instead of its own HTTP server
  apolloServer.applyMiddleware({
    // @see https://github.com/meteor/meteor/blob/master/packages/webapp/webapp_server.js
    app: WebApp.connectHandlers,
    path: config.path,
    // @see https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#Parameters-2
    ...apolloApplyMiddlewareOptions,
  });

  // setup the end point
  WebApp.connectHandlers.use(config.path, (req, res) => {
    if (req.method === 'GET') {
      res.end();
    }
  });

  /* Syntax for adding middlewares to /graphql
     Uses Connect + Meteor + Apollo
     For the list of already set middlewares (cookies, compression...), see:
    @see https://github.com/meteor/meteor/blob/master/packages/webapp/webapp_server.js
{'fraction_non_alphanumeric': 0.1224237427864798, 'fraction_numerical': 0.0002748007694421544, 'mean_word_length': 4.026933701657459, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 10, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2013613', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2094, 'n_words': 584}