https://github.com/adriano-di-giovanni/graphql-scalars
Custom scalars for GraphQL
https://github.com/adriano-di-giovanni/graphql-scalars
custom-scalar graphql graphql-js graphql-scalars graphql-schema scalar
Last synced: about 2 months ago
JSON representation
Custom scalars for GraphQL
- Host: GitHub
- URL: https://github.com/adriano-di-giovanni/graphql-scalars
- Owner: adriano-di-giovanni
- License: mit
- Created: 2016-09-22T16:15:45.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-02-02T07:08:43.000Z (over 7 years ago)
- Last Synced: 2025-04-26T04:56:05.877Z (2 months ago)
- Topics: custom-scalar, graphql, graphql-js, graphql-scalars, graphql-schema, scalar
- Language: JavaScript
- Size: 13.7 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# graphql-scalars
> Custom [scalars](http://graphql.org/learn/schema/#scalar-types) for GraphQL.
Currently available scalars:
* [GraphQLDate](#GraphQLDate)
* [GraphQLEmailAddress](#GraphQLEmailAddress)
* [GraphQLURL](#GraphQLURL)## Installation
```bash
npm install graphql-scalars --save
```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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for more details.
`GraphQLDate` serializes dates to ISO 8601 strings.
### Usage
```javascript
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` uses the regular expression as per HTML5 specification to
validate input email addresses.`GraphQLURL` uses the [regular expression](https://gist.github.com/dperini/729294) by [Diego Perini](https://github.com/dperini).
## License
`graphql-scalars` is [MIT-licensed](https://github.com/adriano-di-giovanni/graphql-scalars/blob/master/LICENSE).