Last active
August 3, 2016 15:50
-
-
Save trbngr/ed47363caeb5edb0a98b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLScalarType } from 'graphql'; | |
import { GraphQLError } from 'graphql/error'; | |
import { Kind } from 'graphql/language'; | |
import moment from 'moment'; | |
import tz from 'moment-timezone'; | |
function coerceDate(value) { | |
if(typeof value === 'string') | |
value = moment(value); | |
if (!(value instanceof Object)) { | |
// Is this how you raise a "field error"? | |
throw new Error("Field error: value is not an instance of moment") | |
} | |
if (isNaN(value.seconds())) { | |
throw new Error("Field error: value is an invalid moment") | |
} | |
return value.format() | |
} | |
export {moment}; | |
export default new GraphQLScalarType({ | |
name: 'moment', | |
serialize: coerceDate, | |
parseValue: coerceDate, | |
parseLiteral(ast) { | |
if (ast.kind !== Kind.STRING ) { | |
throw new GraphQLError("Query error: Can only parse strings to dates but got a: " + ast.kind, [ast]) | |
} | |
const result = moment(ast.value); | |
if (isNaN(result.seconds())) { | |
throw new GraphQLError("Query error: Invalid date", [ast]) | |
} | |
if (ast.value != result.format()) { | |
throw new GraphQLError("Query error: Invalid date format, only accepts: YYYY-MM-DDTHH:MM:SS.SSSZ", [ast]) | |
} | |
return result | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
GraphQLBoolean, | |
GraphQLID, | |
GraphQLInt, | |
GraphQLList, | |
GraphQLNonNull, | |
GraphQLObjectType, | |
GraphQLSchema, | |
GraphQLString, | |
} from 'graphql'; | |
import { | |
globalIdField, | |
connectionArgs, | |
cursorForObjectInConnection | |
} from 'graphql-relay'; | |
import GraphQLMoment, {moment} from './GraphQLMoment'; | |
import {EmptyGuid} from './../utils'; | |
export default new GraphQLObjectType({ | |
name: 'SessionSchedule', | |
fields: () => ({ | |
startDateUtc: { | |
type: GraphQLMoment, | |
resolve: schedule => moment.utc(schedule.startDateUtc || 0) | |
}, | |
endDateUtc: { | |
type: GraphQLMoment, | |
resolve: schedule => moment.utc(schedule.endDateUtc || 0) | |
}, | |
startDate: { | |
type: GraphQLMoment, | |
resolve: schedule => moment.utc(schedule.startDateUtc || 0).tz(schedule.timeZone.tzId) | |
}, | |
endDate: { | |
type: GraphQLMoment, | |
resolve: schedule => moment.utc(schedule.endDateUtc || 0).tz(schedule.timeZone.tzId) | |
}, | |
room: { | |
type: require('./../connections/rooms')().RoomEdge, | |
resolve: (schedule, _, {rootValue: {eventLoader}}) => { | |
if (schedule.roomId === EmptyGuid) | |
return null; | |
return eventLoader.load(schedule.eventId) | |
.then(event=>{ | |
const room = event.rooms.find(x=>x.id === schedule.roomId) | |
return { | |
node: room, | |
cursor: cursorForObjectInConnection(event.rooms, room) | |
} | |
}) | |
} | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment