UNPKG

apollo-mobx

Version:

HOC for querying graphql server

85 lines (84 loc) 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var apollo_cache_inmemory_1 = require("apollo-cache-inmemory"); var apollo_link_1 = require("apollo-link"); var enzyme_1 = require("enzyme"); var graphql_tools_1 = require("graphql-tools"); var mobx_react_1 = require("mobx-react"); var client_1 = require("../client/client"); var mock_link_1 = require("./mock_link"); var spy_link_1 = require("./spy_link"); function configure(schema, resolver) { global.globalTypeDefs = schema; global.globalResolvers = resolver; } exports.configure = configure; function initialiseApolloMocks(_a) { var _b = _a.queries, queries = _b === void 0 ? {} : _b, _c = _a.mutations, mutations = _c === void 0 ? {} : _c, _d = _a.resolvers, resolvers = _d === void 0 ? {} : _d, _e = _a.reducers, reducers = _e === void 0 ? {} : _e, _f = _a.typeDefs, typeDefs = _f === void 0 ? global.globalTypeDefs : _f, context = _a.context, loadingComponent = _a.loadingComponent; var finalMocks = tslib_1.__assign({}, resolvers, { Mutation: function () { return mutations || {}; }, Query: function () { return queries || {}; } }); var schema = graphql_tools_1.makeExecutableSchema({ typeDefs: typeDefs, resolvers: global.globalResolvers }); graphql_tools_1.addMockFunctionsToSchema({ mocks: finalMocks, schema: schema, }); var apolloCache = new apollo_cache_inmemory_1.InMemoryCache(global.__APOLLO_STATE_); var spyLink = new spy_link_1.SpyLink(); var graphqlClient = new client_1.ApolloClient({ cache: apolloCache, context: context, link: apollo_link_1.ApolloLink.from([ spyLink, new mock_link_1.default({ schema: schema }), ]), loadingComponent: loadingComponent }); graphqlClient.spyLink = spyLink; return graphqlClient; } exports.initialiseApolloMocks = initialiseApolloMocks; function initialiseApolloDecorator(_a) { var _b = _a.queries, queries = _b === void 0 ? {} : _b, _c = _a.mutations, mutations = _c === void 0 ? {} : _c, _d = _a.resolvers, resolvers = _d === void 0 ? {} : _d, _e = _a.reducers, reducers = _e === void 0 ? {} : _e, typeDefs = _a.typeDefs, context = _a.context, loadingComponent = _a.loadingComponent; var client = initialiseApolloMocks({ queries: queries, mutations: mutations, resolvers: resolvers, context: context, typeDefs: typeDefs, loadingComponent: loadingComponent }); return { ApolloDecorator: function (_a) { var children = _a.children; return (React.createElement(mobx_react_1.Provider, { context: context, client: client }, React.createElement("div", null, children))); }, client: client, context: context, }; } exports.initialiseApolloDecorator = initialiseApolloDecorator; function mountContainer(component) { return tslib_1.__awaiter(this, void 0, void 0, function () { var wrapper, client; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: wrapper = enzyme_1.mount(component); client = wrapper.find('Provider').prop('client'); return [4, waitForQueries(client)]; case 1: _a.sent(); return [2, wrapper]; } }); }); } exports.mountContainer = mountContainer; function waitForQueries(client) { return tslib_1.__awaiter(this, void 0, void 0, function () { var spyLink; return tslib_1.__generator(this, function (_a) { spyLink = client.spyLink; if (!spyLink) { throw new Error('You need to add SpyLink to your links!'); } return [2, spyLink.wait()]; }); }); } exports.waitForQueries = waitForQueries;