Giter VIP home page Giter VIP logo

graphql-custom-datetype's Introduction

Custom Date Type for GraphQL

This is a custom date type implementation for GraphQL. GraphQL does not contain a native date type but it does allow you to specify custom scalar types that serializes to strings, or other scalar types, but conforms to certain standards.

This date type accepts and outputs this format: '2015-07-24T13:15:34.814Z' which is commonly used in JSON since it is the default format used by JavaScript when serializing dates to JSON.

npm version Build Status

Usage

To use this type you in your GraphQL schema you simply install this module with npm install --save graphql-custom-datetype and use it. In the minimal example below we expose the query now that simply returns the current date and time.

The important part is that your resolve function needs to return a JavaScript Date object.

// examples/now.js

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
} from 'graphql';
import CustomGraphQLDateType from '..';

let schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      now: {
        type: CustomGraphQLDateType,
        // Resolve fields with the custom date type to a valid Date object
        resolve: () => new Date()
      }
    }
  })
});

graphql(schema, "{ now }")
  .then(console.log)
  .catch(console.error);

Running this prints the current date:

$ babel-node examples/now.js
{ data: { now: '2015-07-24T13:23:15.580Z' } }

graphql-custom-datetype's People

Contributors

gabrielf avatar tothandras avatar glavin001 avatar

Watchers

James Cloos avatar Pablo Saucedo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.