import { Meteor } from 'meteor/meteor';
import { createGraphQLMethod } from './createGraphQLMethod';
import { createGraphQLPublication } from './createGraphQLPublication';
import {
  DEFAULT_METHOD,
  DEFAULT_CREATE_CONTEXT,
} from '../common/defaults';

export const DDP_APOLLO_SCHEMA_REQUIRED = 'DDP_APOLLO_SCHEMA_REQUIRED';

export function setup({
  schema,
  method = DEFAULT_METHOD,
  publication,
  context,
} = {}) {
  if (!schema) {
    throw new Error(DDP_APOLLO_SCHEMA_REQUIRED);
  }

  let createContext;

  switch (typeof context) {
    case 'object':
      createContext = defaultContext => ({ ...defaultContext, ...context });
      break;
    case 'function':
      createContext = context;
      break;
    default:
      createContext = DEFAULT_CREATE_CONTEXT;
  }

  Meteor.methods({
    [method]: createGraphQLMethod(schema, { createContext }),
  });

  createGraphQLPublication({
    schema,
    createContext,
    publication,
  });
}

@@ -321,23 +321,16 @@
 IRED';%0A%0A
-export 
 function
@@ -334,181 +334,36 @@
 ion 
-setup(%7B%0A  schema,%0A  method = DEFAULT_METHOD,%0A  publication,%0A  context,%0A%7D = %7B%7D) %7B%0A  if (!schema) %7B%0A    throw new Error(DDP_APOLLO_SCHEMA_REQUIRED);%0A  %7D%0A%0A  let createContext;%0A
+contextToFunction(context) %7B
 %0A  s
@@ -412,31 +412,22 @@
 :%0A      
-c
 re
-ateContext =
+turn
  default
@@ -474,29 +474,16 @@
 ext %7D);%0A
-      break;%0A
     case
@@ -501,31 +501,22 @@
 :%0A      
-c
 re
-ateContext =
+turn
  context
@@ -521,21 +521,8 @@
 xt;%0A
-      break;%0A
     
@@ -540,23 +540,14 @@
     
-c
 re
-ateContext =
+turn
  DEF
@@ -570,16 +570,239 @@
 EXT;%0A  %7D
+%0A%7D%0A%0Aexport function setup(%7B%0A  schema,%0A  method = DEFAULT_METHOD,%0A  publication,%0A  context,%0A%7D = %7B%7D) %7B%0A  if (!schema) %7B%0A    throw new Error(DDP_APOLLO_SCHEMA_REQUIRED);%0A  %7D%0A%0A  const createContext = contextToFunction(context);
 %0A%0A  Mete

Move context function creation to own function