feat(cloudapi-gql): transform objects on the type resolvers

This commit is contained in:
Sérgio Ramos 2017-10-03 16:04:10 +01:00
parent 3d99fda795
commit d3baebd25f
6 changed files with 36 additions and 27 deletions

View File

@ -1,6 +1,13 @@
const request = require('./request'); const request = require('./request');
module.exports.list = ctx => request('listImages', ctx); const transform = ({ os, type, state, ...rest }) =>
Object.assign(rest, {
type: type ? type.toLowerCase() : type,
os: os ? os.toLowerCase() : os,
state: state ? state.toLowerCase() : state
});
module.exports.list = ctx => request('listImages', transform(ctx));
module.exports.get = ctx => request('getImage', ctx); module.exports.get = ctx => request('getImage', ctx);
module.exports.destroy = uuid => request('deleteImage', uuid); module.exports.destroy = uuid => request('deleteImage', uuid);
module.exports.export = uuid => request('deleteImage', uuid); module.exports.export = uuid => request('deleteImage', uuid);

View File

@ -2,7 +2,7 @@ const forceArray = require('force-array');
const hasha = require('hasha'); const hasha = require('hasha');
module.exports.toKeyValue = r => module.exports.toKeyValue = r =>
Object.keys(r).map(name => ({ r && Object.keys(r).map(name => ({
id: hasha(JSON.stringify({ name, value: r[name] })), id: hasha(JSON.stringify({ name, value: r[name] })),
name, name,
value: r[name] value: r[name]

View File

@ -38,7 +38,7 @@ module.exports.reboot = ctx => request('rebootMachine', ctx);
module.exports.resize = ctx => request('', ctx); module.exports.resize = ctx => request('', ctx);
module.exports.rename = ctx => request('', ctx); module.exports.rename = ctx => request('', ctx);
module.exports.destroy = ctx => request('deleteMachine', ctx); module.exports.destroy = ctx => request('deleteMachine', ctx);
module.exports.audit = ctx => request('machineAudit', ctx); module.exports.audit = ({ id }) => request('machineAudit', id);
module.exports.snapshots = snapshots; module.exports.snapshots = snapshots;
module.exports.metadata = metadata; module.exports.metadata = metadata;

View File

@ -2,7 +2,7 @@ const request = require('./request');
// lists all networks, including fabric networks // lists all networks, including fabric networks
module.exports.list = () => request('listNetworks'); module.exports.list = () => request('listNetworks');
module.exports.get = ctx => request('getNetwork', ctx); module.exports.get = ({ id }) => request('getNetwork', id);
// create fabric network // create fabric network
module.exports.create = () => request(''); module.exports.create = () => request('');
// destroy fabric network // destroy fabric network

View File

@ -1,20 +1,6 @@
const { toKeyValue, fromKeyValue } = require('../api/key-value'); const { toKeyValue, fromKeyValue } = require('../api/key-value');
const api = require('../api'); const api = require('../api');
const transform = {
toImage: ({ os, ...rest }) => {
console.log(rest);
return Object.assign(rest, {
os: os ? os.toUpperCase() : os
});
},
fromImage: ({ os, state, type, ...rest }) => Object.assign(rest, {
os: os ? os.toLowerCase() : os,
state: state ? state.toLowerCase() : state,
type: type ? type.toLowerCase() : type
})
};
const resolvers = { const resolvers = {
Query: { Query: {
account: () => api.account.get(), account: () => api.account.get(),
@ -45,12 +31,10 @@ const resolvers = {
})) }))
), ),
services: () => api.services().then(toKeyValue), services: () => api.services().then(toKeyValue),
images: (root, { id, ...rest }) => id images: (root, { id, ...rest }) =>
? api.images.get({ id }) id
.then(image => [image]) ? api.images.get({ id }).then(image => [image])
.then((imgs) => imgs.map(transform.toImage)) : api.images.list(rest),
: api.images.list(transform.fromImage(rest))
.then((imgs) => imgs.map(transform.toImage)),
image: (root, { id }) => api.images.get({ id }), image: (root, { id }) => api.images.get({ id }),
packages: (root, { id, ...rest }) => packages: (root, { id, ...rest }) =>
id id
@ -59,7 +43,7 @@ const resolvers = {
package: (root, { id }) => api.packages.get({ id }), package: (root, { id }) => api.packages.get({ id }),
machines: (root, { id, brand, state, tags, ...rest }) => machines: (root, { id, brand, state, tags, ...rest }) =>
id id
? api.machines.get({ id }) ? api.machines.get({ id }).then(machine => [machine])
: api.machines.list( : api.machines.list(
Object.assign(rest, { Object.assign(rest, {
brand: brand ? brand.toLowerCase() : brand, brand: brand ? brand.toLowerCase() : brand,
@ -96,6 +80,7 @@ const resolvers = {
api.machines.tags api.machines.tags
.get({ id: machine, tag: name }) .get({ id: machine, tag: name })
.then(value => toKeyValue({ [name]: value }).shift()), .then(value => toKeyValue({ [name]: value }).shift()),
actions: (root, { machine }) => api.machines.audit({ id: machine }),
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
firewall_rules: (root, { machine, id }) => firewall_rules: (root, { machine, id }) =>
id id
@ -118,15 +103,32 @@ const resolvers = {
keys: ({ login }, { name }) => resolvers.Query.keys(null, { login, name }) keys: ({ login }, { name }) => resolvers.Query.keys(null, { login, name })
}, },
Machine: { Machine: {
state: ({ state }) => (state ? state.toUpperCase() : state),
tags: ({ id }, { name }) => tags: ({ id }, { name }) =>
resolvers.Query.tags(null, { machine: id, name }), resolvers.Query.tags(null, { machine: id, name }),
metadata: ({ id }, { name }) => metadata: ({ id }, { name }) =>
resolvers.Query.metadata(null, { machine: id, name }), resolvers.Query.metadata(null, { machine: id, name }),
snapshots: ({ id }, { name }) => snapshots: ({ id }, { name }) =>
resolvers.Query.snapshots(null, { machine: id, name }), resolvers.Query.snapshots(null, { machine: id, name }),
networks: ({ networks }) =>
Promise.all(networks.map(id => resolvers.Query.network(null, { id }))),
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
firewall_rules: ({ id: machine }, { id }) => firewall_rules: ({ id: machine }, { id }) =>
resolvers.Query.firewall_rules(null, { machine, id }) resolvers.Query.firewall_rules(null, { machine, id }),
actions: ({ id }) => resolvers.Query.actions(null, { machine: id })
},
Image: {
os: ({ os }) => (os ? os.toUpperCase() : os),
state: ({ state }) => (state ? state.toUpperCase() : state),
type: ({ type }) => (type ? type.toUpperCase() : type)
},
Action: {
name: ({ action }) => action,
parameters: ({ parameters }) => toKeyValue(parameters)
},
Caller: {
type: ({ type }) => (type ? type.toUpperCase() : type),
key_id: ({ keyId }) => keyId
} }
}; };

View File

@ -109,7 +109,7 @@ type Caller {
# When the authentication type is BASIC, this member will be present and include user login # When the authentication type is BASIC, this member will be present and include user login
ip: String ip: String
# When authentication type is either SIGNATURE or TOKEN, SSH key identifier # When authentication type is either SIGNATURE or TOKEN, SSH key identifier
keyId: String key_id: String
} }
type Action { type Action {