Skip to content

Commit

Permalink
Nested GraphQL mocks (#6)
Browse files Browse the repository at this point in the history
* Dynamic GraphQL mocks

* Recursive generateMocks

* Bump version
  • Loading branch information
keaeriksson committed Sep 19, 2020
1 parent d7c60fa commit d360967
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mocki",
"version": "0.3.0",
"version": "0.3.1",
"bin": "src/cli.js",
"description": "",
"main": "src/index.js",
Expand Down
19 changes: 10 additions & 9 deletions src/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ const { ApolloServer } = require('apollo-server-express');
const supertest = require('supertest');
const { buildClientSchema } = require('graphql');

const generateMocks = mocks => {
const generatedMocks = {};
Object.keys(mocks).forEach(mock => {
generatedMocks[mock] = () => mocks[mock];
});
return generatedMocks;
const generateMocks = obj => {
const result = {};
for (const property in obj) {
result[property] = () =>
typeof obj[property] === 'object' && !Array.isArray(obj[property])
? generateMocks(obj[property])
: obj[property];
}
return result;
};

const graphql = async (endpoint, req) => {
Expand All @@ -25,9 +28,7 @@ const graphql = async (endpoint, req) => {
app.use(bodyParser.json());
server.applyMiddleware({ app, path: '/' });

const request = supertest(app);

const result = await request.post('/').send(req.body);
const result = await supertest(app).post('/').send(req.body);

return result.body;
};
Expand Down
149 changes: 134 additions & 15 deletions test/graphql-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,51 @@
"description": "",
"fields": [
{
"name": "exampleQuery",
"description": "",
"name": "getUser",
"description": "Get a user by id",
"args": [
{
"name": "ExampleInput",
"description": "",
"name": "id",
"description": "The id of the user",
"type": {
"kind": "INPUT_OBJECT",
"name": "ExampleInput",
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "ExampleResponse",
"name": "User",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "getUsers",
"description": "Get all users",
"args": [],
"type": {
"kind": "LIST",
"name": null,
"ofType": { "kind": "Object", "name": "User", "ofType": null }
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "getTodos",
"description": "Get all todos",
"args": [],
"type": {
"kind": "LIST",
"name": null,
"ofType": { "kind": "Object", "name": "Todo", "ofType": null }
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand All @@ -40,7 +64,7 @@
},
{
"kind": "INPUT_OBJECT",
"name": "ExampleInput",
"name": "UpdateTodoInput",
"description": "",
"fields": null,
"inputFields": [
Expand All @@ -49,6 +73,18 @@
"description": "",
"type": { "kind": "SCALAR", "name": "String", "ofType": null },
"defaultValue": null
},
{
"name": "description",
"description": "",
"type": { "kind": "SCALAR", "name": "String", "ofType": null },
"defaultValue": null
},
{
"name": "done",
"description": "",
"type": { "kind": "SCALAR", "name": "Boolean", "ofType": null },
"defaultValue": null
}
],
"interfaces": null,
Expand All @@ -57,17 +93,100 @@
},
{
"kind": "OBJECT",
"name": "ExampleResponse",
"name": "User",
"description": "",
"fields": [
{
"name": "data",
"name": "id",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "name",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "email",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "todos",
"description": "",
"args": [],
"type": {
"kind": "LIST",
"name": null,
"ofType": { "kind": "SCALAR", "name": "String", "ofType": null }
"ofType": { "kind": "Todo", "name": "Todo", "ofType": null }
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "Todo",
"description": "",
"fields": [
{
"name": "id",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "description",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "done",
"description": "",
"args": [],
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
Expand Down Expand Up @@ -124,18 +243,18 @@
"description": "",
"fields": [
{
"name": "exampleMutation",
"name": "updateTodo",
"description": "",
"args": [
{
"name": "input",
"description": "",
"description": "Todo input",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "ExampleInput",
"name": "UpdateTodoInput",
"ofType": null
}
},
Expand All @@ -144,7 +263,7 @@
],
"type": {
"kind": "OBJECT",
"name": "ExampleResponse",
"name": "Todo",
"ofType": null
},
"isDeprecated": false,
Expand Down

0 comments on commit d360967

Please sign in to comment.