From 6ac351cc6c2f46c8de1836460f61929185b7981c Mon Sep 17 00:00:00 2001 From: Assaf Milman Date: Mon, 5 Dec 2016 13:22:36 +0200 Subject: [PATCH] added directive to add time offset to unixtime --- src/directives/date.js | 33 +++++++++++++++++++++++++++++++++ src/index.js | 6 ++++-- test/directives/date.js | 10 +++++++++- test/utils.js | 6 +++--- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/directives/date.js b/src/directives/date.js index 88190cd..b5d86b1 100644 --- a/src/directives/date.js +++ b/src/directives/date.js @@ -6,6 +6,14 @@ const moment = require('moment'); const DEFAULT_DATE_FORMAT = 'DD MMM YYYY HH:mm'; +const extractOffset = (context, offsetLocation) => { + let tempContext = Object.assign({}, context); + for (let path of offsetLocation.split('.')){ + tempContext = tempContext[path]; + } + return tempContext; +} + exports.GraphQLDateDirective = new GraphQLCustomDirective({ name: 'date', description: @@ -41,3 +49,28 @@ exports.GraphQLDateDirective = new GraphQLCustomDirective({ } }); + +exports.GraphQLTimeOffsetDirective = new GraphQLCustomDirective({ + name: 'timeOffset', + description: 'Format the date from resolving the field by moment module', + locations: [DirectiveLocation.FIELD], + args: { + offsetLocation: { + type: GraphQLString, + description: 'Path of offset in context object. e.g - "req.shop.utcOffset"' + } + }, + resolve: function resolve(_resolve, source, _ref, context, info) { + var offsetLocation = _ref.offsetLocation; + var offsetMinutes = extractOffset(context, offsetLocation); + var offsetMilliseconds = offsetMinutes * 60 * 1000; + + return _resolve().then(function (input) { + + if (('' + input).length === 13) { + input = Number(input) + offsetMilliseconds; + return input; + } + }); + } +}); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 67363a8..60d6ba9 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,8 @@ import { } from './custom'; import { - GraphQLDateDirective + GraphQLDateDirective, + GraphQLTimeOffsetDirective } from './directives/date'; import { @@ -47,4 +48,5 @@ exports.GraphQLTrimDirective = GraphQLTrimDirective; exports.GraphQLDefaultToDirective = GraphQLDefaultToDirective; exports.GraphQLToLowerDirective = GraphQLToLowerDirective; exports.GraphQLToUpperDirective = GraphQLToUpperDirective; -exports.GraphQLTemplateDirective = GraphQLTemplateDirective; \ No newline at end of file +exports.GraphQLTemplateDirective = GraphQLTemplateDirective; +exports.GraphQLTimeOffsetDirective = GraphQLTimeOffsetDirective; \ No newline at end of file diff --git a/test/directives/date.js b/test/directives/date.js index 11d9609..71e5be4 100644 --- a/test/directives/date.js +++ b/test/directives/date.js @@ -1,4 +1,4 @@ -import { GraphQLDateDirective } from '../../src/index' +import { GraphQLDateDirective, GraphQLTimeOffsetDirective } from '../../src/index' import { testEqual } from '../utils'; import { expect } from 'chai'; @@ -84,5 +84,13 @@ describe('directives/date', () => { }); + it('expected directive to add offset to 13 digit unixtime, and return 13 digit unixtime ', (done) => { + const query = `{ value(input: "1479964283000") @timeOffset(offsetLocation: "req.shop.utcOffset") }`, + directives = [ GraphQLTimeOffsetDirective], + expected = { value: "1479975083000" }, + context = {req: {shop: {utcOffset: 180}}}; + testEqual({ directives, query, expected, done ,context}); + + }); }); diff --git a/test/utils.js b/test/utils.js index 9a89630..810bff3 100644 --- a/test/utils.js +++ b/test/utils.js @@ -5,12 +5,12 @@ import { expect } from 'chai'; const DEFAULT_TEST_SCHEMA = `type Query { value(input: String): String } schema { query: Query }`; -exports.testEqual = function({ directives, query, schema, input, passServer = false, expected, done }) { +exports.testEqual = function({ directives, query, schema, input, passServer = false, expected, done, context }) { let executionSchema = buildSchema(schema || DEFAULT_TEST_SCHEMA); if (!schema) { - executionSchema._queryType._fields.value.resolve = (source, { input }) => input; + executionSchema._queryType._fields.value.resolve = (source, { input, context }) => input; if (passServer) { executionSchema._queryType._fields.value.directives = { duplicate: {by: 2} }; } @@ -20,7 +20,7 @@ exports.testEqual = function({ directives, query, schema, input, passServer = fa applySchemaCustomDirectives(executionSchema); - graphql(executionSchema, query, input) + graphql(executionSchema, query, input, context) .then(({data, errors }) => { if (errors) { console.error(errors);