diff --git a/requests/Switcher Resolver Node.postman_collection.json b/requests/Switcher Resolver Node.postman_collection.json index 3c2fd80..f3af62c 100644 --- a/requests/Switcher Resolver Node.postman_collection.json +++ b/requests/Switcher Resolver Node.postman_collection.json @@ -411,7 +411,7 @@ "body": { "mode": "graphql", "graphql": { - "query": "query Domain(\r\n $name: String, \r\n $environment: String, \r\n $_component: String) {\r\n domain(\r\n name: $name, \r\n environment: $environment,\r\n _component: $_component\r\n ) {\r\n name\r\n version\r\n description\r\n activated\r\n group {\r\n name\r\n description\r\n activated\r\n config {\r\n key\r\n description\r\n activated\r\n strategies {\r\n strategy\r\n activated\r\n operation\r\n values\r\n }\r\n relay {\r\n type\r\n method\r\n activated\r\n authPrefix\r\n verifiedByEnv { env value }\r\n authTokenByEnv { env value }\r\n statusByEnv { env value }\r\n endpointByEnv { env value }\r\n }\r\n components\r\n }\r\n }\r\n }\r\n}", + "query": "query Domain(\r\n $name: String, \r\n $environment: String, \r\n $_component: String) {\r\n domain(\r\n name: $name, \r\n environment: $environment,\r\n _component: $_component\r\n ) {\r\n name\r\n version\r\n description\r\n activated\r\n group {\r\n name\r\n description\r\n activated\r\n config {\r\n key\r\n description\r\n activated\r\n strategies {\r\n description\r\n strategy\r\n activated\r\n operation\r\n values\r\n }\r\n relay {\r\n type\r\n method\r\n activated\r\n description\r\n authPrefix\r\n verifiedByEnv { env value }\r\n authTokenByEnv { env value }\r\n statusByEnv { env value }\r\n endpointByEnv { env value }\r\n }\r\n components\r\n }\r\n }\r\n }\r\n}", "variables": "{\r\n \"name\": \"{{domain_name}}\",\r\n \"environment\": \"{{env_name}}\",\r\n \"_component\": \"{{component_name}}\"\r\n}" } }, diff --git a/src/client/configuration-type.js b/src/client/configuration-type.js index 40faaa3..9663afb 100644 --- a/src/client/configuration-type.js +++ b/src/client/configuration-type.js @@ -51,10 +51,12 @@ const commonFieldsType = { export const strategyType = new GraphQLObjectType({ name: 'Strategy', fields: { - ...commonFieldsType, _id: { type: GraphQLString }, + description: { + type: GraphQLString + }, strategy: { type: GraphQLString }, @@ -63,8 +65,20 @@ export const strategyType = new GraphQLObjectType({ }, values: { type: new GraphQLList(GraphQLString) + }, + activated: { + type: GraphQLBoolean, + resolve: (source, _args, { environment }) => { + return source.activated[`${environment}`]; + } + }, + statusByEnv: { + type: new GraphQLList(envStatus), + resolve: (source) => { + return resolveEnvValue(source, 'activated', Object.keys(source.activated)); } } + } }); export const relayType = new GraphQLObjectType({ @@ -76,6 +90,9 @@ export const relayType = new GraphQLObjectType({ method: { type: GraphQLString, }, + description: { + type: GraphQLString + }, activated: { type: GraphQLBoolean, resolve: (source, _args, { environment }) => { @@ -204,9 +221,6 @@ export const domainType = new GraphQLObjectType({ name: { type: GraphQLString }, - description: { - type: GraphQLString - }, owner: { type: GraphQLString }, diff --git a/src/client/resolvers.js b/src/client/resolvers.js index 9481de4..0608363 100644 --- a/src/client/resolvers.js +++ b/src/client/resolvers.js @@ -32,25 +32,31 @@ export async function resolveConfigStrategy(source, _id, strategy, operation, ac if (operation) { args.operation = operation; } let strategies = await ConfigStrategy.find({ config: source._id, ...args }).lean().exec(); - const environment = context.environment ?? EnvType.DEFAULT; + const environment = context.environment; - strategies = strategies.filter(s => s.activated[environment] !== undefined); - if (activated !== undefined) { - strategies = strategies.filter(s => s.activated[environment] === activated); + if (environment) { + strategies = strategies.filter(s => s.activated[environment] !== undefined); + if (activated !== undefined) { + strategies = strategies.filter(s => s.activated[environment] === activated); + } } return strategies; } export async function resolveRelay(source, context) { - const relay = source.relay; - const environment = context.environment ?? EnvType.DEFAULT; - - if (!relay.type || !relay.endpoint[environment] || (relay.activated[environment] == undefined)) { + const { relay } = source; + const { environment } = context; + + if (environment) { + if (relay.activated && relay.activated[environment] !== undefined) { + return relay; + } + return null; } - - return relay; + + return relay.type ? relay : null; } export async function resolveConfig(source, _id, key, activated, context) { diff --git a/tests/client-api.test.js b/tests/client-api.test.js index 7f8c00b..fd20509 100644 --- a/tests/client-api.test.js +++ b/tests/client-api.test.js @@ -379,14 +379,14 @@ describe('Testing domain [GraphQL]', () => { afterAll(setupDatabase); - test('CLIENT_SUITE - Should return the Domain structure', async () => { + test('CLIENT_SUITE - Should return the Domain structure for Environment (default)', async () => { const req = await request(app) .post('/graphql') .set('Authorization', `Bearer ${token}`) - .send(graphqlUtils.domainQuery([['_id', domainId], ['environment', EnvType.DEFAULT]], true, true, true)); + .send(graphqlUtils.domainQuery([['_id', domainId], ['environment', EnvType.DEFAULT]])); expect(req.statusCode).toBe(200); - expect(JSON.parse(req.text)).toMatchObject(JSON.parse(graphqlUtils.expected102)); + expect(JSON.parse(req.text)).toMatchObject(JSON.parse(graphqlUtils.expected102Default)); }); test('CLIENT_SUITE - Should return 2 switchers when NOT filtered by Component', async () => { diff --git a/tests/graphql-utils/index.js b/tests/graphql-utils/index.js index 41c9947..78a293f 100644 --- a/tests/graphql-utils/index.js +++ b/tests/graphql-utils/index.js @@ -12,12 +12,12 @@ export const domainQuery = (where, group, config, strategy) => { { domain(${query}) { name version description activated statusByEnv { env value } - group${elementQuery(group)} { + group${group !== undefined ? elementQuery(group) : ''} { name description activated statusByEnv { env value } - config${elementQuery(config)} { + config${config !== undefined ? elementQuery(config) : ''} { key description activated statusByEnv { env value } - strategies${elementQuery(strategy)} { - strategy activated operation values statusByEnv { env value } + strategies${strategy !== undefined ? elementQuery(strategy): ''} { + strategy activated operation values } relay { type @@ -183,6 +183,101 @@ export const expected102 = ` } }`; +export const expected102Default = ` +{ + "data": { + "domain": { + "name": "Domain", + "version": 5, + "description": "Test Domain", + "activated": true, + "statusByEnv": [{ + "env": "default", + "value": true + }], + "group": [{ + "name": "Group Test", + "description": "Test Group", + "activated": true, + "statusByEnv": [{ + "env": "default", + "value": true + }], + "config": [{ + "key": "TEST_CONFIG_KEY", + "description": "Test config 1", + "activated": true, + "statusByEnv": [{ + "env": "default", + "value": true + }], + "strategies": [{ + "strategy": "VALUE_VALIDATION", + "activated": true, + "operation": "EXIST", + "values": [ + "USER_1", + "USER_2", + "USER_3" + ] + }, { + "strategy": "NETWORK_VALIDATION", + "activated": true, + "operation": "EXIST", + "values": [ + "10.0.0.0/24" + ] + }, { + "strategy": "TIME_VALIDATION", + "activated": false, + "operation": "BETWEEN", + "values": [ + "13:00", + "14:00" + ] + }, { + "strategy": "DATE_VALIDATION", + "activated": false, + "operation": "GREATER", + "values": [ + "2019-12-01T13:00" + ] + }], + "relay": { + "type": "NOTIFICATION", + "method": "POST", + "activated": false, + "endpointByEnv": [{ + "env": "default", + "value": "http://localhost:3000" + }], + "statusByEnv": [ { + "env": "default", + "value": false + }] + }, + "components": ["TestApp"] + }, + { + "key": "TEST_CONFIG_KEY_PRD_QA", + "description": "Test config 2 - Off in PRD and ON in QA", + "activated": false, + "statusByEnv": [{ + "env": "default", + "value": false + },{ + "env": "QA", + "value": true + }], + "strategies": [], + "relay": null, + "components": [] + }] + }] + } + } +}`; + export const expected103 = ` { "data": {