forked from orteth01/relay-mock-network-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (65 loc) · 2.9 KB
/
index.js
File metadata and controls
77 lines (65 loc) · 2.9 KB
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
'use strict';
const { makeExecutableSchema, addMockFunctionsToSchema } = require('graphql-tools');
const { graphql, printSchema, buildClientSchema } = require('graphql');
const RelayMockNetworkLayerError = require('./RelayMockNetworkLayerError');
const defaultSchemaDefinitionOptions = {
resolverValidationOptions: {
// Silence error regarding missing Relay `Node` type
// https://github.com/este/este/issues/1513#issuecomment-385752066
requireResolversForResolveType: false,
},
};
/**
* @param {Object} networkConfig - The configuration for the mock network layer.
* @param {(string|Object)} networkConfig.schema - If string, the graphql schema SDL. If object, the JSON introspection query.
* @param {Object} networkConfig.mocks - Mock primative resolvers, passed directly to addMockFunctionsToSchema.
* @param {Object} networkConfig.resolvers - Default resolvers for a schema.
* @param {Function} [networkConfig.resolveQueryFromOperation] - If relay operation query text does not exist, used to resolve the query from the operation. Useful for persisted query support.
*/
module.exports = function getNetworkLayer({
schema,
mocks,
resolvers,
resolveQueryFromOperation,
/** https://www.apollographql.com/docs/graphql-tools/mocking.html#Mocking-interfaces */
preserveResolvers = false,
/**
* https://github.com/apollographql/graphql-tools/blob/master/src/Interfaces.ts#L125
* @type IExecutableSchemaDefinition
*/
...schemaDefinitionOptions
}) {
schemaDefinitionOptions = {
...defaultSchemaDefinitionOptions,
...schemaDefinitionOptions,
};
return function fetchQuery(operation, variableValues) {
if (typeof schema === 'object' && schema.data) {
schema = printSchema(buildClientSchema(schema.data));
}
const executableSchema = makeExecutableSchema({
typeDefs: schema,
resolvers,
...schemaDefinitionOptions,
});
// Add mocks, modifies schema in place
addMockFunctionsToSchema({ schema: executableSchema, mocks, preserveResolvers });
const query =
(resolveQueryFromOperation && resolveQueryFromOperation(operation)) || operation.text;
if (!query) {
throw new Error(
'Could not find query, ensure operation.text exists or pass resolveQueryFromOperation.'
);
}
return graphql(executableSchema, query, null, null, variableValues).then(
// Trigger Relay error in case of GraphQL errors (or errors in mutation response)
// See https://github.com/facebook/relay/issues/1816
result => {
if (result.errors && result.errors.length > 0) {
return Promise.reject(new RelayMockNetworkLayerError(result.errors));
}
return Promise.resolve(result);
}
);
};
}