@dellstorage/mock-server
v1.1.2
Published
Mock server is used to response on queries with mocked real schema that was received from the real graph backend server.
Downloads
12
Readme
Mock server
Mock server is used to response on queries with mocked real schema that was received from the real graph backend server.
Usage
| Command | Description | | :---------------- | :--------------------------------------------------------------------------------------------------------------------- | | --update | will update the schema from the real graphql backend. Server should have the next format http://localhost:8080/graphql |
Without flags will run the mocked server.
Under mocks
folder mock files can be found. Each file represents
a field of the graphql schema. The value of them should be a function
the returns data(object/array/fields with functions).
schema.graphql - should be commited, it provides opportunities to see backend changes and run the mock server at CI without running the real graphql server.
Contribution
It has faker library. If you need to generate some kind of data then
don't forget to use faker.seek
to preset random. It is required when you cover some tests for UI it will allow
you to get the same values between different launches.
Also be aware you are able to generate amount of data instead of write it manually to test UI with more data.
Arguments such as first, count and so on are passed as the sencond arguments and can be handled to prepare results.
For example:
const faker = require("faker");
// Generate data
// See that faker is used to fill data
const edges = new Array(50).fill(0).map(() => ({
node: () => ({
symptomID: () => faker.random.number(),
type: () => faker.random.arrayElement(["NormalEvent", "WarningEvent"]),
message: () => faker.lorem.words(),
createdOn: () => faker.date.past(10, new Date(2020, 0, 1)).toISOString(),
}),
}));
module.exports = (_, {first}) => {
// <- accept first argument
faker.seed(1); // <- here we set seed(1) to have the same random results between queries
return {
edges: () => edges.slice(0, first), // use first to get first results
};
};
To add mocks for a mutation you need to place your resolvers to src/mocks/mutations.js
.
For example:
module.exports = () => ({
acknowledgeEvent: () => ({
description: "my-description",
}),
});
This mock will be used as RootMutation field in the schema. To see all available mocks you need to search
RootMutation in src/schema.graphql
or in Playground.
State of server
To be able to share data between mutations and queries you have to use a storage. The storage is passed through whole schema from the main executable function.
Example:
module.exports = store => ({
RootMutation: RootMutation(store),
Viewer: Viewer(store),
String: () => "Apollo-mock",
});
To use that you have to keep the store in a clojure function, fill the store and use that one to modify and return the data.
The idea behind the store is to have separated data for different types.
If you use managerConfig query then use data at { managerConfig: [data] }
For example of using in a mutation and a query:
// A mutation
// Be aware we keep the store in clojure.
module.exports = store => () => ({
createEventRule: (_, {input}) => {
const {eventrule} = input;
// Here we update the store to save the incoming data.
store.update({
managerConfig: {
eventRules: [...store.getStore().managerConfig.eventRules, eventrule],
},
});
return eventrule;
},
});
// A query
// Pay attention we accept the store in query mock function.
module.exports = store => {
faker.seed(1);
// this function will be invoked when the schema will be initialized
// and this store also is already shared with the mutation.
// Just fill the structure with preset data.
store.update({
managerConfig: {
version: faker.system.semver(),
eventRules: [], //...,
defaultEventRules: [], ...,
},
});
// Return a function that will return the managerConfig from the store.
return () => store.getStore().managerConfig;
};
See more examples at
- Mutation: src/mocks/mutations.js
- Query: src/mocks/managerConfig.js
Packaging mock-server
$ # to create tarball that can be referenced as dependency from other project
$ # move to directory where Makefile exists and run
$ make pack
$ # you can reference in other project like: "mock-server": "http://my_package_server/packages/mock-server-0.17.0.tgz"
$ # you can also package mock server as docker image
$ make image
$ # run docker image with
$ sudo docker run -d --rm --name mock-server harbor.lss.emc.com/ecs/mock-server:latest
$ # note: using --name flag allows container to be linked to vsphere plugin if you desire
Example of usage
$ # install utils package
$ yarn install ecs-flex-ui-utils # if this package is not registered then use <path to ecs-flex-ui-utils>/ecs-flex-ui-utils
$ # if you want to install just mock-server without additional packages
$ # yarn install ../ecs-flex-ui-utils/packages/mock-server
$ # run the mock-server
$ yarn mock-server
$ # run your tests, you have the running server like the real backend
$ yarn test
For development purpose you also are free to use yarn link.
$ # go to the mock-server package
$ yarn link
$ # go to the needed project
$ yarn link mock-server
$ yarn mock-server
$ # modify mock-server package and get the changes without reinstalling package
$ # don't forget to unlink the mock-server package when you don't need it
$ yarn unlink mock-server
$ # reinstall dependencies after linking
$ yarn install --force
$ # go to the mock-server package
$ yarn unlink