1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.mockServer = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const schema_1 = require("@graphql-tools/schema");
|
6 | const 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 | */
|
19 | function 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 | }
|
39 | exports.mockServer = mockServer;
|