12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- Object.defineProperty(exports, "__esModule", { value: true });
- var graphql_1 = require("graphql");
- var deprecated_decorator_1 = require("deprecated-decorator");
- var _1 = require(".");
- // 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
- var attachConnectorsToContext = deprecated_decorator_1.deprecated({
- version: '0.7.0',
- url: 'https://github.com/apollostack/graphql-tools/issues/140',
- }, function (schema, connectors) {
- if (!schema || !(schema instanceof graphql_1.GraphQLSchema)) {
- 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') {
- var 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;
- var attachconnectorFn = function (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.
- var 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(function (connectorName) {
- var connector = connectors[connectorName];
- if (!!connector.prototype) {
- ctx.connectors[connectorName] = new connector(ctx);
- }
- else {
- throw new Error("Connector must be a function or an class");
- }
- });
- return root;
- };
- _1.addSchemaLevelResolveFunction(schema, attachconnectorFn);
- });
- exports.default = attachConnectorsToContext;
- //# sourceMappingURL=attachConnectorsToContext.js.map
|