This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
44 lines (38 loc) · 1.68 KB
/
index.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
const debug = require('debug')('apollo-local')
/**
* Create a local graphql networkInterface for apollo-client.
*
* See:
* https://github.com/apollostack/apollo-client/issues/962
* https://github.com/apollostack/apollo-client/blob/master/src/transport/networkInterface.ts
*
* @arg {Object} graphql - The graphql-js library. ie, the result of require('graphql')
* @arg {GraphQLSchema} schema - The schema you create for your GraphQL API.
* See http://graphql.org/graphql-js/type/#graphqlschema
* @arg {Object} options=
* @arg {Object} options.rootValue - The optional root value argument for graphqljs
* @arg {Object} options.context - Optional context for the graphql query
* @return {Promise<GraphQLResult>} - The result of the GraphQL query
*/
const createLocalInterface = (graphql, schema, {rootValue = null, context = null} = {}) => {
const {execute} = graphql
return {
query: ({query, variables, operationName, debugName}) => {
const start = new Date()
let result
// execute() can throw if its arguments aren't valid, so we need to wrap with try/catch
// See http://graphql.org/graphql-js/execution/#execute
try {
result = execute(schema, query, rootValue, context, variables, operationName)
} catch (err) {
debug(`${operationName} failed to execute (${err.message})`)
return Promise.reject(err)
}
return Promise.resolve(result).then(data => {
debug(`${operationName} (${(new Date() - start)}ms)`)
return data
})
}
}
}
module.exports = {createLocalInterface}