graphql-date-scalars
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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

Dependencies (0)

    Dev Dependencies (17)

    Package Sidebar

    Install

    npm i graphql-date-scalars

    Weekly Downloads

    6,211

    Version

    0.2.0

    License

    MIT

    Unpacked Size

    25.7 kB

    Total Files

    21

    Last publish

    Collaborators

    • neo-ife-ojikutu
    • neo-will-parker
    • neo-paula-grangeiro
    • gaulford
    • danielafekhume-neo
    • jashish_m
    • neo-lior-ben-shahar
    • neo-diego-nunes
    • tim-neo2
    • antonio.canabrava
    • neo.eduardoyuidy
    • neoricardogcolombo
    • kirstyrobertson
    • neo-oleksandr-yanchenko
    • neo-barry-li
    • desmond-maloney
    • harmeetsalech
    • neo-elgiz-abbasov
    • nikita-syrotenko
    • neo-bclausi
    • dustinn1235
    • neo-prerakkumar-patel
    • neo-peter-spangler
    • neo-rivan-mota
    • neo-daniel-brennan
    • neo-matt-felice
    • neo-rameez-virji
    • matt-armstrong
    • rajanatneo
    • josephma
    • neo-fredy
    • neo-zhaoquan-zhang
    • alejandrogarbi
    • ronellgarcia
    • gui.martins
    • edward.nazarov
    • annakumova
    • leah-is-offline
    • neo-shruti-goyal
    • felipe.minetto
    • neo-kevin-hillard
    • neo-ahmed-jama
    • olgavozniuk
    • tunde-neo
    • neo-shubham-patel
    • biancaballena
    • neo-sacha-cuna
    • jadonduby
    • msalama
    • ani-samajpati-neo
    • ishmeet.rayat
    • neo-bryce-brandford
    • neo-raheel-junaid
    • neo-ricardo-gomez
    • neo-lawrence-liu
    • edwardkruger
    • neobot1
    • neo-dakota-chatt
    • lucasjohannson
    • neo-travis-friesen
    • mike.himbeault-neo
    • raul.ribeiro
    • neo-shraddha
    • anhtranneo
    • lucachamecki
    • amievishwa
    • neo-matias-wengiel
    • ini-a
    • neo-ahmed-seifelnasr
    • rhullyam
    • das-boss
    • kennethcolina
    • feminachacko
    • marvindeleon
    • neo-miguel-castro
    • neo-dione-silva
    • hanna.tkachenko
    • nico-connor
    • roman.mnatsakanian
    • riley-pickerl
    • thomas-llewellyn-neo
    • briancruz
    • amjad-mahfoud-neo
    • neoauderesantos
    • nitin.madan
    • mohpor
    • cghenneneo
    • punit.patel
    • brayden_sc_neo
    • anna_benemanskaia
    • hrishikeshkale
    • kyle-elyk-neo
    • michelepiperni
    • neo-youre_pena
    • neo-michal-kowal
    • niconeiman
    • neodiegobeltran
    • stburke
    • marcelo.matos
    • matheusicaro
    • divanshu
    • maiahneo
    • kimberlytranneo
    • rjberry
    • neo-carlos-pieren
    • mohamedalhammoud
    • neo-hasanpreetsingh
    • irakitnykh
    • jcavalcanti
    • stephanie-wang-neo
    • kaelanneofinancial
    • vinicius-barcelos
    • benx-neo
    • mhowitt-neo
    • malcolm-neo
    • neo-seol-kim
    • brentcam
    • teena_
    • deankim
    • herculesjr
    • matthew.belford
    • rishbar
    • neo-baonguyen
    • caio.andrade
    • scott.wiedemann
    • thomawstrahn
    • luan.le
    • dennisbarthel
    • lavanya.mohan
    • leosbelsotomayor
    • dhruvang
    • neo-rodrigoraval
    • joeljwood
    • nahom-neo
    • stefan.amyotte
    • timothy.couch.neofinancial
    • scottkayeneo
    • omolara
    • ar-ies
    • cjnewman
    • vidalrmrz
    • rnetto46
    • iansu
    • darrenpicard25
    • maxgoo
    • stschmaltz
    • usernameseb
    • eric.zaporzan.neo
    • lucasparreira
    • lewishorwood
    • craigsiemens
    • diegobmy
    • joelsimpson
    • jooh-lee
    • kasrababaei
    • mattlabarre
    • u2v22
    • johnclendvoy
    • demaestro
    • sudeep.sidhu
    • daylan.law
    • neo-stephen
    • neogibson
    • beraki
    • vrochamaaia
    • d_fenniak
    • mato-gallardo
    • adamtranquilla
    • nehasingh1801
    • lucaslongarini
    • erozonachi
    • faddoul
    • lovekesh1
    • jeffrey-dugas-neo
    • pichatelli
    • npoltz-neo