-
Notifications
You must be signed in to change notification settings - Fork 2
/
run-mutation.js
42 lines (40 loc) · 1.27 KB
/
run-mutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// @flow
import type {Context, Logging, MutationType, MutationResult, ObjectType, Query, Schema} from '../flow-types';
import Promise from 'promise';
import typeNameFromValue from '../utils/type-name-from-value';
import createError from '../utils/create-error';
import freeze from '../utils/freeze';
import validateArg from './arg-validator';
import validateArgs from './args-validator';
import {validateMutationReturnType} from './validate-return-type';
const EMPTY_OBJECT = freeze({});
export default function runMutation(
schema: Schema,
logging: Logging,
mutation: {method: string, args: Object},
typeName: string,
mutationName: string,
Type: ObjectType,
method: MutationType,
context: Context,
): Promise<MutationResult> {
const args = mutation.args;
return Promise.resolve(null).then(() => {
const typedArgs = freeze(
method.args
? validateArgs(schema, method.args, args)
: EMPTY_OBJECT
);
return method.resolve(typedArgs, context, {type: typeName, name: mutationName});
}).then(value => {
if (method && method.type) {
return Promise.resolve(
validateMutationReturnType(schema, logging, method.type, value),
).then(value => {
return {s: true, v: value};
});
} else {
return true;
}
});
}