graphql-date-scalars

v0.2.0
GraphQL scalars for Date, DateTime and Time
graphql scalars date time datetime node

graphql-date-scalars

Build status TypeScript 4.4.4

GraphQL Scalars for Date (YYYY-MM-DD), DateTime (YYYY-MM-DDTHH:MM:SSZ), and Time (HH:MM:SSZ)

Schema Usage

import { gql, ApolloServer } from 'apollo-server';
import { DateScalar, TimeScalar, DateTimeScalar } from 'graphql-date-scalars';

const resolvers = {
  // Must define resolvers for these scalars
  Date: DateScalar,
  Time: TimeScalar,
  DateTime: DateTimeScalar,

  // along with all your other resolvers
  Query: {
    exampleDateQuery: () => {
      // Will serialize to a date string, such as 2007-12-03
      return new Date();
    },
    exampleTimeQuery: () => {
      // Will serialize to a time string at UTC, such as 10:15:30Z
      return new Date();
    },
  },
  Mutation: {
    exampleDateTimeMutation: () => {
      // Will serialize to a date-time string at UTC, such as 2007-12-03T10:15:30Z
      return new Date();
    },
  },
};

const typeDefs = gql`
  scalar Date
  scalar DateTime

  extend type Query {
    exampleDateQuery: Date!
    exampleTimeQuery: Time!
  }

  extend type Mutation {
    exampleDateTimeMutation: DateTime!
  }
`;

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

If you are using @graphql-codegen then you must include these scalars in your codegen yml file under config

schema: './example-schema.graphql'
config:
  scalars:
    Date: Date
    Time: Date
    DateTime: Date
generates:
  src/types/example-schema.d.ts:
    plugins:
      - 'typescript'

Direct Usage

You can also use the parse and serialize methods directly, which is useful if you are making a rest call to a 3rd party vendor.

import { DateTimeScalar } from 'graphql-date-scalars';

const vendorResponse = await restClient.get();

const response = {
  ...vendorResponse,
  createdAt: DateTimeScalar.parseValue(vendorResponse.createdAt),
};
import { DateScalar } from 'graphql-date-scalars';

const args = {
  ...input,
  dateOfBirth: DateScalar.serialize(input.dateOfBirth),
};

const response = await restClient.post(args);

Contributing

  1. Fork this repo
  2. Clone the forked repo
  3. Install dependencies: npm i

Building

npm run build

To clean the build directory run npm run clean

Running Tests

npm run test

Publishing

  1. Update the version in package.json
  2. Add a CHANGELOG.md entry
  3. Commit your changes
  4. Run npm pack to see what will be published then delete the .tgz file that was created
  5. Run npm publish
  6. Create a release on GitHub. Use the version as the tag and release name. For example for version 1.0.0 the tag and release name would be v1.0.0.

Contributors

This project started as a fork of https://github.com/excitement-engineer/graphql-iso-date

npm i graphql-date-scalars

Metadata

  • MIT
  • >=14.0.0
  • Neo Financial Engineering
  • released 11/23/2022

Downloads

Maintainers

neo-miguel-castro, amanjotguru, neo-dione-silva, hanna.tkachenko, nico-connor, neo-andrew-moss, abhishek.arora, roman.mnatsakanian, riley-pickerl, thomas-llewellyn-neo, briancruz, amjad-mahfoud-neo, neoauderesantos, satwinder.singh, pamilerin.ayantoye, dwaine.mitchell, neo-david-bertouille, caiolazareti, nitin.madan, siddharth-chopra, mohpor, cghenneneo, rahulkambojneo, punit.patel, brayden_sc_neo, anna_benemanskaia, hrishikeshkale, kyle-elyk-neo, michelepiperni, neo-youre_pena, neo-michal-kowal, cole.murphy, niconeiman, mateus-cerqueira-neo, neodiegobeltran, stburke, johnzhou2022, marcelo.matos, neoricardogcolombo, carolina-bury-neo, matheusicaro, divanshu, neo-vahid-haghighat, maiahneo, chloe_eunsuk, kimberlytranneo, rjberry, dean.rud, neo-carlos-pieren, mohamedalhammoud, neo-hasanpreetsingh, irakitnykh, ravi-patel, katrinaw, jcavalcanti, dbhalotia, hozefa_neo, stephanie-wang-neo, kaelanneofinancial, slava_korenblit, vinicius-barcelos, neo-ife-ojikutu, arshroop-neo, moushumi-das, benx-neo, mhowitt-neo, malcolm-neo, neo-seol-kim, kevingiang-neo, brentcam, teena_, neo-prince-chavez, seungwan.yuk, christyc, deankim, herculesjr, matthew.belford, rishbar, anandpatel1, neo-baonguyen, caio.andrade, scott.wiedemann, thomawstrahn, luan.le, dennisbarthel, lavanya.mohan, richard.scheid, leosbelsotomayor, dilushaneo, flaviosilva-neo, dhruvang, varunhasteer, neo-rodrigoraval, jordan.brauer, joeljwood, ctataryn, nahom-neo, stefan.amyotte, timothy.couch.neofinancial, rsbarnard, lianafilizola, neo-patrick-weckworth, scottkayeneo, leah-is-offline, rogersnick, omolara, ar-ies, cjnewman, sidney.dizon, vidalrmrz, rnetto46, iansu, darrenpicard25, maxgoo, stschmaltz, usernameseb, eric.zaporzan.neo, lucasparreira, lewishorwood, kimwj0318, craigsiemens, diegobmy, neo.eduardoyuidy, joelsimpson, daniel.neo, jooh-lee, mrjoedang, kasrababaei, heramb-sawant, mattlabarre, dadair-ca, u2v22, hypnocrit, johnclendvoy, demaestro, sudeep.sidhu, daylan.law, bethelh, sean.heintz, andre.campestrini, neo-stephen, neogibson, beraki, vrochamaaia, rmagatti, d_fenniak, mato-gallardo, mbhagat, adamtranquilla, nehasingh1801, lucaslongarini, mitchell-gonzalez, erozonachi, faddoul, lovekesh1, paulohauck, jeffrey-dugas-neo, yash-coded, sean_kenny, pichatelli, dubby20, geff-neo, npoltz-neo, david.tse,