JSPM

graphql-scalars

0.1.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1109901
  • Score
    100M100P100Q190454F
  • License MIT

Custom scalars for GraphQL

Package Exports

  • graphql-scalars

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (graphql-scalars) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

graphql-scalars

Custom scalars for GraphQL.

Currently available scalars:

Installation

npm install graphql-scalars --save

GraphQLDate

This custom scalar for GraphQL parses any integer, float, string or date value to javascript dates.

GraphQLDate uses new Date() to parse values so, please refer to the documentation for more details.

GraphQLDate serializes dates to ISO 8601 strings.

Usage

import {
  graphql,
  GraphQLObjectType,
  GraphQLSchema
} from 'graphql'

import { GraphQLDate } from 'graphql-scalars'

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQuery',
    fields: {
      createdAt: {
        type: GraphQLDate,
        resolve () {
          // Resolver can return an integer, string or date value.
          // The following return values all resolve to the same date.
          // `return 262915200000`
          // `return '1978-05-02'`
          // `return '1978-05-02T00:00:00.000Z'`
          return new Date('1978-05-02')
        }
      }
    }
  }),
  mutation: new GraphQLObjectType({
    name: 'RootMutation',
    fields: {
      setCreatedAt: {
        type: GraphQLDate,
        args: {
          createdAt: {
            type: GraphQLDate
          }
        },
        resolve (source, { createdAt }) {
          // `createdAt` is a javascript date
          return createdAt
        }
      }
    }
  })
})

const query = `
  {
    createdAt
  }
`

graphql(schema, query)
  .then(result => {
    const isoString = result.data.createdAt
    console.log(isoString) // 1978-05-02T00:00:00.000Z
  })

// literals for GraphQLDate can be any integer or string value
// `setCreatedAt(createdAt:262915200000)`
// `setCreatedAt(createdAt:"1978-05-02")`
// `setCreatedAt(createdAt:"1978-05-02T00:00:00.000Z")`
const mutation = `
  mutation {
    setCreatedAt(createdAt:"1978-05-02")
  }
`

graphql(schema, query)
  .then(result => {
    const isoString = result.data.createdAt
    console.log(isoString) // 1978-05-02T00:00:00.000Z
  })

GraphQLEmailAddress

GraphQLEmailAddress uses the regular expression as per HTML5 specification to validate input email addresses.

License

graphql-scalars is MIT-licensed.