diff --git a/packages/cp-gql-mock-server/src/data.json b/packages/cp-gql-mock-server/src/data.json index 6b26a93a..f5838ae3 100644 --- a/packages/cp-gql-mock-server/src/data.json +++ b/packages/cp-gql-mock-server/src/data.json @@ -34,7 +34,7 @@ "id": "081a792c-47e0-4439-924b-2efa9788ae9e", "slug": "nginx", "name": "Nginx", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401", "connections": [ "be227788-74f1-4e5b-a85f-b5c71cbae8d8" ] @@ -43,7 +43,7 @@ "id": "be227788-74f1-4e5b-a85f-b5c71cbae8d8", "slug": "wordpress", "name": "Wordpress", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401", "connections": [ "6a0eee76-c019-413b-9d5f-44712b55b993", "6d31aff4-de1e-4042-a983-fbd23d5c530c", @@ -54,125 +54,125 @@ "id": "6a0eee76-c019-413b-9d5f-44712b55b993", "slug": "nfs", "name": "NFS", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "6d31aff4-de1e-4042-a983-fbd23d5c530c", "slug": "memcached", "name": "Memcached", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "4ee4103e-1a52-4099-a48e-01588f597c70", "slug": "percona", "name": "Percona", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "9572d367-c4ae-4fb1-8ad5-f5e3830e7034", "slug": "primary", "name": "Primary", "parent": "4ee4103e-1a52-4099-a48e-01588f597c70", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "c8411ef0-ab39-42cb-a704-d20b170eff31", "slug": "secondaries", "name": "Secondaries", "parent": "4ee4103e-1a52-4099-a48e-01588f597c70", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "97c68055-db88-45c9-ad49-f26da4264777", "slug": "consul", "name": "Consul", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "primary-consul", "slug": "consul", "name": "Consul", - "deploymentGroup": "9fcb374d-a267-4c2a-9d9c-ba469b804639" + "deploymentGroupId": "9fcb374d-a267-4c2a-9d9c-ba469b804639" }, { "id": "primary-nginx", "slug": "nginx", "name": "Nginx", - "deploymentGroup": "9fcb374d-a267-4c2a-9d9c-ba469b804639" + "deploymentGroupId": "9fcb374d-a267-4c2a-9d9c-ba469b804639" }, { "id": "primary-mongodb", "slug": "mongodb", "name": "MongoDB", - "deploymentGroup": "9fcb374d-a267-4c2a-9d9c-ba469b804639" + "deploymentGroupId": "9fcb374d-a267-4c2a-9d9c-ba469b804639" } ], "instances": [ { "id": "309ecd9f-ac03-474b-aff7-4bd2e743296c", "name": "wordpress_01", - "service": "be227788-74f1-4e5b-a85f-b5c71cbae8d8", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "be227788-74f1-4e5b-a85f-b5c71cbae8d8", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "0db6db53-de6f-4378-839e-5d5b452fbaf2", "name": "nfs_01", - "service": "6a0eee76-c019-413b-9d5f-44712b55b993", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "6a0eee76-c019-413b-9d5f-44712b55b993", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "250c8a6c-7d02-49a9-8abd-e1c22773041d", "name": "consul", - "service": "97c68055-db88-45c9-ad49-f26da4264777", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "97c68055-db88-45c9-ad49-f26da4264777", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "2c921f3a-8bc3-4f57-9cd7-789ebae72061", "name": "memcache_01", - "service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "68d3046e-8e34-4f5d-a0e5-db3795a250fd", "name": "memcache_02", - "service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "2ea99763-3b44-4179-8393-d66d94961051", "name": "memcache_03", - "service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "25f6bc62-63b8-4959-908e-1f6d7ff6341d", "name": "memcache_04", - "service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "8be01042-0281-4a77-a357-25979e87bf3d", "name": "memcache_05", - "service": "6d31aff4-de1e-4042-a983-fbd23d5c530c", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "6d31aff4-de1e-4042-a983-fbd23d5c530c", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "3d652e9d-73e8-4a6f-8171-84fa83740662", "name": "nginx", - "service": "081a792c-47e0-4439-924b-2efa9788ae9e", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "081a792c-47e0-4439-924b-2efa9788ae9e", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "c3ec7633-a02b-4615-86a0-9e6faeaae94b", "name": "percona-primary", - "service": "4ee4103e-1a52-4099-a48e-01588f597c70", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "4ee4103e-1a52-4099-a48e-01588f597c70", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" }, { "id": "c2b5fec2-31e2-41a7-b7fc-cd0bb1822e76", "name": "percona-secondary", - "service": "4ee4103e-1a52-4099-a48e-01588f597c70", - "deploymentGroup": "e0ea0c02-55cc-45fe-8064-3e5176a59401" + "serviceId": "4ee4103e-1a52-4099-a48e-01588f597c70", + "deploymentGroupId": "e0ea0c02-55cc-45fe-8064-3e5176a59401" } ] } diff --git a/packages/cp-gql-mock-server/src/resolvers.js b/packages/cp-gql-mock-server/src/resolvers.js index 89270099..5a2f6040 100644 --- a/packages/cp-gql-mock-server/src/resolvers.js +++ b/packages/cp-gql-mock-server/src/resolvers.js @@ -11,42 +11,59 @@ const find = (query = {}) => item => const cleanQuery = (q = {}) => JSON.parse(JSON.stringify(q)); -const getServiceInstances = s => - Object.assign({}, s, { - instances: instances.filter(find({ service: s.id })).map(s => - Object.assign({}, s, { - slug: s.name +const getInstances = query => + Promise.resolve(instances.filter(find(cleanQuery(query)))); + +const getServices = query => { + const instancesResolver = ({ id }) => query => + getInstances( + Object.assign({}, query, { + serviceId: id }) - ) - }); + ); -const getDeploymentGroupServices = dg => - Object.assign({}, dg, { - services: services - .filter(find({ deploymentGroup: dg.id })) - .map(getServiceInstances) - }); + const addNestedResolvers = service => + Object.assign({}, service, { + instances: instancesResolver(service) + }); -const getDeploymentGroups = query => - deploymentGroups - .filter(find(cleanQuery(query))) - .map(getDeploymentGroupServices); + return Promise.resolve( + services.filter(find(cleanQuery(query))).map(addNestedResolvers) + ); +}; + +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 = () => - Object.assign({}, portal, { - datacenter, - deploymentGroups: getDeploymentGroups() - }); - -const getServices = query => - services.filter(find(query)).map(getDeploymentGroupServices); + Promise.resolve( + Object.assign({}, portal, { + datacenter, + deploymentGroups: getDeploymentGroups + }) + ); module.exports = { - portal: (options, request, fn) => fn(null, getPortal()), - deploymentGroups: (options, request, fn) => - fn(null, getDeploymentGroups(options)), - deploymentGroup: (options, request, fn) => - fn(null, getDeploymentGroups(options).shift()), - services: (options, request, fn) => fn(null, getServices()), - service: (options, request, fn) => fn(null, getServices(options).shift()) + portal: getPortal, + deploymentGroups: getDeploymentGroups, + deploymentGroup: query => getDeploymentGroups(query), + services: getServices, + service: query => getServices(query).then(services => services.shift()), + instances: getInstances, + instance: query => getInstances(query).then(instances => instances.shift()) };