/
attachConnectorsToContext.ts
73 lines (68 loc) · 2.63 KB
/
attachConnectorsToContext.ts
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
import { deprecated } from 'deprecated-decorator';
import { GraphQLSchema, GraphQLFieldResolver, isSchema } from 'graphql';
import { IConnectors, IConnector, IConnectorCls } from '../Interfaces';
import addSchemaLevelResolver from './addSchemaLevelResolver';
// takes a GraphQL-JS schema and an object of connectors, then attaches
// the connectors to the context by wrapping each query or mutation resolve
// function with a function that attaches connectors if they don't exist.
// attaches connectors only once to make sure they are singletons
const attachConnectorsToContext = deprecated<Function>(
{
version: '0.7.0',
url: 'https://github.com/apollostack/graphql-tools/issues/140',
},
(schema: GraphQLSchema, connectors: IConnectors): void => {
if (!schema || !isSchema(schema)) {
throw new Error(
'schema must be an instance of GraphQLSchema. ' +
'This error could be caused by installing more than one version of GraphQL-JS',
);
}
if (typeof connectors !== 'object') {
const connectorType = typeof connectors;
throw new Error(
`Expected connectors to be of type object, got ${connectorType}`,
);
}
if (Object.keys(connectors).length === 0) {
throw new Error('Expected connectors to not be an empty object');
}
if (Array.isArray(connectors)) {
throw new Error('Expected connectors to be of type object, got Array');
}
if (schema['_apolloConnectorsAttached']) {
throw new Error(
'Connectors already attached to context, cannot attach more than once',
);
}
schema['_apolloConnectorsAttached'] = true;
const attachconnectorFn: GraphQLFieldResolver<any, any> = (
root,
_args,
ctx,
) => {
if (typeof ctx !== 'object') {
// if in any way possible, we should throw an error when the attachconnectors
// function is called, not when a query is executed.
const contextType = typeof ctx;
throw new Error(
`Cannot attach connector because context is not an object: ${contextType}`,
);
}
if (typeof ctx.connectors === 'undefined') {
ctx.connectors = {};
}
Object.keys(connectors).forEach((connectorName) => {
const connector: IConnector = connectors[connectorName];
if (connector.prototype != null) {
ctx.connectors[connectorName] = new (connector as IConnectorCls)(ctx);
} else {
throw new Error('Connector must be a function or an class');
}
});
return root;
};
addSchemaLevelResolver(schema, attachconnectorFn);
},
);
export default attachConnectorsToContext;