Skip to content

Commit

Permalink
added directive to add time offset to unixtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Assaf Milman authored and Assaf Milman committed Dec 5, 2016
1 parent c6d33d8 commit 6ac351c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
33 changes: 33 additions & 0 deletions src/directives/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
});
}
});
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
} from './custom';

import {
GraphQLDateDirective
GraphQLDateDirective,
GraphQLTimeOffsetDirective
} from './directives/date';

import {
Expand Down Expand Up @@ -47,4 +48,5 @@ exports.GraphQLTrimDirective = GraphQLTrimDirective;
exports.GraphQLDefaultToDirective = GraphQLDefaultToDirective;
exports.GraphQLToLowerDirective = GraphQLToLowerDirective;
exports.GraphQLToUpperDirective = GraphQLToUpperDirective;
exports.GraphQLTemplateDirective = GraphQLTemplateDirective;
exports.GraphQLTemplateDirective = GraphQLTemplateDirective;
exports.GraphQLTimeOffsetDirective = GraphQLTimeOffsetDirective;
10 changes: 9 additions & 1 deletion test/directives/date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLDateDirective } from '../../src/index'
import { GraphQLDateDirective, GraphQLTimeOffsetDirective } from '../../src/index'
import { testEqual } from '../utils';

import { expect } from 'chai';
Expand Down Expand Up @@ -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});

});
});
6 changes: 3 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} };
}
Expand All @@ -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);
Expand Down

0 comments on commit 6ac351c

Please sign in to comment.