digiaonline / graphql-datetime-scalar
Date, Time and DateTime scalar types for our GraphQL implementation
Installs: 7 035
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 5
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- digiaonline/graphql: *
Requires (Dev)
- digiaonline/graphql: dev-master#97cd9ff3720a6c0f57f917165ceedf4052be4cd3
- phpstan/phpstan: ^0.10.3
- phpunit/phpunit: ^7.3
This package is auto-updated.
Last update: 2023-03-12 21:37:48 UTC
README
Date, Time and DateTime scalar types for our GraphQL implementation
Introduction
Most GraphQL APIs need a way to represent dates, times or timestamps of some sort. Since there is no native date type in GraphQL, people have to resort to rolling their own. To reduce the boilerplate required, this library provides a few basic scalar types that should cover most situations.
Requirements
- PHP >= 7.1
- digiaonline/graphql
Usage
Installation the library using Composer:
composer require digiaonline/graphql-datetime-scalar
When wiring up your executable schema, simply pass in the types you need:
$executableSchema = buildSchema($schemaDefinition, [ 'Query' => QueryResolver::class, 'Mutation' => MutationResolver::class, ], [ 'types' => [ new DateScalarType(), new TimeScalarType(), new DateTimeScalarType(), ], ]);
While it's not strictly necessary it might be a good idea to also mention in your schema that these types exist:
scalar Date scalar Time scalar DateTime
You can specify the format by passing a format string to the constructor:
$dateType = new DateScalarType('j.n.Y'); // 31.12.2018 $timeType = new TimeScalarType('H:i'); // 14:35 $dateTimeType = new DateTimeScalarType('U'); // 1537361927
Like the built-in scalar types, if a value cannot be serialized or parsed correctly into a \DateTimeInterface
,
a \Digia\GraphQL\Error\InvalidTypeException
will be thrown.
License
MIT