-
Notifications
You must be signed in to change notification settings - Fork 2k
/
graphql.js
83 lines (79 loc) · 2.63 KB
/
graphql.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* @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 { validate } from './validation/validate';
import { execute } from './execution/execute';
import type { GraphQLError } from './error/GraphQLError';
import type { GraphQLSchema } from './type/schema';
/**
* 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.
*
* schema:
* The GraphQL type system to use when validating and executing a query.
* requestString:
* A GraphQL language formatted string representing the requested operation.
* rootValue:
* The value provided as the first argument to resolver functions on the top
* level type (e.g. the query object type).
* variableValues:
* A mapping of variable name to runtime value to use for all variables
* defined in the requestString.
* operationName:
* The name of the operation to use if requestString contains multiple
* possible operations. Can be omitted if requestString contains only
* one operation.
*/
export function graphql(
schema: GraphQLSchema,
requestString: string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string
): Promise<GraphQLResult> {
return new Promise(resolve => {
const source = new Source(requestString || '', 'GraphQL request');
const documentAST = parse(source);
const validationErrors = validate(schema, documentAST);
if (validationErrors.length > 0) {
resolve({ errors: validationErrors });
} else {
resolve(
execute(
schema,
documentAST,
rootValue,
contextValue,
variableValues,
operationName
)
);
}
}).catch(error => {
return { errors: [ 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<GraphQLError>;
}