feat(joyent-cp-gql-mock-server): add support for nested queries

This commit is contained in:
Sérgio Ramos 2017-06-07 22:15:48 +01:00 committed by Judit Greskovits
parent 3c1763ceb4
commit def5b7e54d
2 changed files with 81 additions and 64 deletions

View File

@ -34,7 +34,7 @@
"id": "081a792c-47e0-4439-924b-2efa9788ae9e", "id": "081a792c-47e0-4439-924b-2efa9788ae9e",
"slug": "nginx", "slug": "nginx",
"name": "Nginx", "name": "Nginx",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401", "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401",
"connections": [ "connections": [
"be227788-74f1-4e5b-a85f-b5c71cbae8d8" "be227788-74f1-4e5b-a85f-b5c71cbae8d8"
] ]
@ -43,7 +43,7 @@
"id": "be227788-74f1-4e5b-a85f-b5c71cbae8d8", "id": "be227788-74f1-4e5b-a85f-b5c71cbae8d8",
"slug": "wordpress", "slug": "wordpress",
"name": "Wordpress", "name": "Wordpress",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401", "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401",
"connections": [ "connections": [
"6a0eee76-c019-413b-9d5f-44712b55b993", "6a0eee76-c019-413b-9d5f-44712b55b993",
"6d31aff4-de1e-4042-a983-fbd23d5c530c", "6d31aff4-de1e-4042-a983-fbd23d5c530c",
@ -54,125 +54,125 @@
"id": "6a0eee76-c019-413b-9d5f-44712b55b993", "id": "6a0eee76-c019-413b-9d5f-44712b55b993",
"slug": "nfs", "slug": "nfs",
"name": "NFS", "name": "NFS",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "id": "6d31aff4-de1e-4042-a983-fbd23d5c530c",
"slug": "memcached", "slug": "memcached",
"name": "Memcached", "name": "Memcached",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "4ee4103e-1a52-4099-a48e-01588f597c70", "id": "4ee4103e-1a52-4099-a48e-01588f597c70",
"slug": "percona", "slug": "percona",
"name": "Percona", "name": "Percona",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "9572d367-c4ae-4fb1-8ad5-f5e3830e7034", "id": "9572d367-c4ae-4fb1-8ad5-f5e3830e7034",
"slug": "primary", "slug": "primary",
"name": "Primary", "name": "Primary",
"parent": "4ee4103e-1a52-4099-a48e-01588f597c70", "parent": "4ee4103e-1a52-4099-a48e-01588f597c70",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "c8411ef0-ab39-42cb-a704-d20b170eff31", "id": "c8411ef0-ab39-42cb-a704-d20b170eff31",
"slug": "secondaries", "slug": "secondaries",
"name": "Secondaries", "name": "Secondaries",
"parent": "4ee4103e-1a52-4099-a48e-01588f597c70", "parent": "4ee4103e-1a52-4099-a48e-01588f597c70",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "97c68055-db88-45c9-ad49-f26da4264777", "id": "97c68055-db88-45c9-ad49-f26da4264777",
"slug": "consul", "slug": "consul",
"name": "Consul", "name": "Consul",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "primary-consul", "id": "primary-consul",
"slug": "consul", "slug": "consul",
"name": "Consul", "name": "Consul",
"deploymentGroup": "9fcb374d-a267-4c2a-9d9c-ba469b804639" "deploymentGroupId": "9fcb374d-a267-4c2a-9d9c-ba469b804639"
}, },
{ {
"id": "primary-nginx", "id": "primary-nginx",
"slug": "nginx", "slug": "nginx",
"name": "Nginx", "name": "Nginx",
"deploymentGroup": "9fcb374d-a267-4c2a-9d9c-ba469b804639" "deploymentGroupId": "9fcb374d-a267-4c2a-9d9c-ba469b804639"
}, },
{ {
"id": "primary-mongodb", "id": "primary-mongodb",
"slug": "mongodb", "slug": "mongodb",
"name": "MongoDB", "name": "MongoDB",
"deploymentGroup": "9fcb374d-a267-4c2a-9d9c-ba469b804639" "deploymentGroupId": "9fcb374d-a267-4c2a-9d9c-ba469b804639"
} }
], ],
"instances": [ "instances": [
{ {
"id": "309ecd9f-ac03-474b-aff7-4bd2e743296c", "id": "309ecd9f-ac03-474b-aff7-4bd2e743296c",
"name": "wordpress_01", "name": "wordpress_01",
"service": "be227788-74f1-4e5b-a85f-b5c71cbae8d8", "serviceId": "be227788-74f1-4e5b-a85f-b5c71cbae8d8",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "0db6db53-de6f-4378-839e-5d5b452fbaf2", "id": "0db6db53-de6f-4378-839e-5d5b452fbaf2",
"name": "nfs_01", "name": "nfs_01",
"service": "6a0eee76-c019-413b-9d5f-44712b55b993", "serviceId": "6a0eee76-c019-413b-9d5f-44712b55b993",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "250c8a6c-7d02-49a9-8abd-e1c22773041d", "id": "250c8a6c-7d02-49a9-8abd-e1c22773041d",
"name": "consul", "name": "consul",
"service": "97c68055-db88-45c9-ad49-f26da4264777", "serviceId": "97c68055-db88-45c9-ad49-f26da4264777",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "2c921f3a-8bc3-4f57-9cd7-789ebae72061", "id": "2c921f3a-8bc3-4f57-9cd7-789ebae72061",
"name": "memcache_01", "name": "memcache_01",
"service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "68d3046e-8e34-4f5d-a0e5-db3795a250fd", "id": "68d3046e-8e34-4f5d-a0e5-db3795a250fd",
"name": "memcache_02", "name": "memcache_02",
"service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "2ea99763-3b44-4179-8393-d66d94961051", "id": "2ea99763-3b44-4179-8393-d66d94961051",
"name": "memcache_03", "name": "memcache_03",
"service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "25f6bc62-63b8-4959-908e-1f6d7ff6341d", "id": "25f6bc62-63b8-4959-908e-1f6d7ff6341d",
"name": "memcache_04", "name": "memcache_04",
"service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "8be01042-0281-4a77-a357-25979e87bf3d", "id": "8be01042-0281-4a77-a357-25979e87bf3d",
"name": "memcache_05", "name": "memcache_05",
"service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "3d652e9d-73e8-4a6f-8171-84fa83740662", "id": "3d652e9d-73e8-4a6f-8171-84fa83740662",
"name": "nginx", "name": "nginx",
"service": "081a792c-47e0-4439-924b-2efa9788ae9e", "serviceId": "081a792c-47e0-4439-924b-2efa9788ae9e",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "c3ec7633-a02b-4615-86a0-9e6faeaae94b", "id": "c3ec7633-a02b-4615-86a0-9e6faeaae94b",
"name": "percona-primary", "name": "percona-primary",
"service": "4ee4103e-1a52-4099-a48e-01588f597c70", "serviceId": "4ee4103e-1a52-4099-a48e-01588f597c70",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
}, },
{ {
"id": "c2b5fec2-31e2-41a7-b7fc-cd0bb1822e76", "id": "c2b5fec2-31e2-41a7-b7fc-cd0bb1822e76",
"name": "percona-secondary", "name": "percona-secondary",
"service": "4ee4103e-1a52-4099-a48e-01588f597c70", "serviceId": "4ee4103e-1a52-4099-a48e-01588f597c70",
"deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401"
} }
] ]
} }

View File

@ -11,42 +11,59 @@ const find = (query = {}) => item =>
const cleanQuery = (q = {}) => JSON.parse(JSON.stringify(q)); const cleanQuery = (q = {}) => JSON.parse(JSON.stringify(q));
const getServiceInstances = s => const getInstances = query =>
Object.assign({}, s, { Promise.resolve(instances.filter(find(cleanQuery(query))));
instances: instances.filter(find({ service: s.id })).map(s =>
Object.assign({}, s, { const getServices = query => {
slug: s.name const instancesResolver = ({ id }) => query =>
getInstances(
Object.assign({}, query, {
serviceId: id
}) })
) );
});
const getDeploymentGroupServices = dg => const addNestedResolvers = service =>
Object.assign({}, dg, { Object.assign({}, service, {
services: services instances: instancesResolver(service)
.filter(find({ deploymentGroup: dg.id })) });
.map(getServiceInstances)
});
const getDeploymentGroups = query => return Promise.resolve(
deploymentGroups services.filter(find(cleanQuery(query))).map(addNestedResolvers)
.filter(find(cleanQuery(query))) );
.map(getDeploymentGroupServices); };
const getDeploymentGroups = query => {
const servicesResolver = ({ id }) => query =>
getServices(
Object.assign({}, query, {
deploymentGroupId: id
})
);
const addNestedResolvers = dg =>
Object.assign({}, dg, {
services: servicesResolver(dg)
});
return Promise.resolve(
deploymentGroups.filter(find(cleanQuery(query))).map(addNestedResolvers)
);
};
const getPortal = () => const getPortal = () =>
Object.assign({}, portal, { Promise.resolve(
datacenter, Object.assign({}, portal, {
deploymentGroups: getDeploymentGroups() datacenter,
}); deploymentGroups: getDeploymentGroups
})
const getServices = query => );
services.filter(find(query)).map(getDeploymentGroupServices);
module.exports = { module.exports = {
portal: (options, request, fn) => fn(null, getPortal()), portal: getPortal,
deploymentGroups: (options, request, fn) => deploymentGroups: getDeploymentGroups,
fn(null, getDeploymentGroups(options)), deploymentGroup: query => getDeploymentGroups(query),
deploymentGroup: (options, request, fn) => services: getServices,
fn(null, getDeploymentGroups(options).shift()), service: query => getServices(query).then(services => services.shift()),
services: (options, request, fn) => fn(null, getServices()), instances: getInstances,
service: (options, request, fn) => fn(null, getServices(options).shift()) instance: query => getInstances(query).then(instances => instances.shift())
}; };