graphql-scalar-unix-time-sec
v1.0.0
Published
A GraphQL scalar for representing unix time seconds
Downloads
1,462
Readme
graphql-scalar-unix-time-sec
A GraphQL scalar for representing unix time seconds
Install
npm i --save graphql-scalar-unix-time-sec
Usage
- Import
import unixTimeSec from 'graphql-scalar-unix-time-sec';
- Use the scalar in the gql schema file
const typeDefs = gql`
type User {
createdAt: ${unixTimeSec.type.name}
input: ${unixTimeSec.type.name}
}
type Query {
user(input: ${unixTimeSec.type.name}): User
}
${unixTimeSec.typedef}
`;
or
type User {
createdAt: UnixTimeSec
input: UnixTimeSec
}
type Query {
user(input: UnixTimeSec): User
}
scalar UnixTimeSec
- Add the resolver
const resolvers = {
...unixTimeSec.resolver,
Query: {
async user(_, input: any) {
return {...returnValues.user, ...input};
}
}
};
Features
This scalar will:
- Accept inputs in either
string
ornumber
format - Validate that the input type has 10 digits (aka is between the years ~1974 to ~2463)
- Will coerce milliseconds to seconds if being sent from the server to client