UNPKG

1.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.mockServer = void 0;
4const graphql_1 = require("graphql");
5const schema_1 = require("@graphql-tools/schema");
6const addMocksToSchema_js_1 = require("./addMocksToSchema.js");
7/**
8 * A convenience wrapper on top of addMocksToSchema. It adds your mock resolvers
9 * to your schema and returns a client that will correctly execute your query with
10 * variables. Note: when executing queries from the returned server, context and
11 * root will both equal `{}`.
12 * @param schema The schema to which to add mocks. This can also be a set of type
13 * definitions instead.
14 * @param mocks The mocks to add to the schema.
15 * @param preserveResolvers Set to `true` to prevent existing resolvers from being
16 * overwritten to provide mock data. This can be used to mock some parts of the
17 * server and not others.
18 */
19function mockServer(schema, mocks, preserveResolvers = false) {
20 const mockedSchema = (0, addMocksToSchema_js_1.addMocksToSchema)({
21 schema: (0, graphql_1.isSchema)(schema)
22 ? schema
23 : (0, schema_1.makeExecutableSchema)({
24 typeDefs: schema,
25 }),
26 mocks,
27 preserveResolvers,
28 });
29 return {
30 query: (query, vars) => (0, graphql_1.graphql)({
31 schema: mockedSchema,
32 source: query,
33 rootValue: {},
34 contextValue: {},
35 variableValues: vars,
36 }),
37 };
38}
39exports.mockServer = mockServer;