Graphqlbundle: Need more informations for using custom scalar type :)

Created on 19 Sep 2018  路  2Comments  路  Source: overblog/GraphQLBundle

| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | no
| BC Break report? | no
| RFC? | no
| Version/Branch | 0.11.5

Hello :)

First of all, a big thanks for that beautiful bundle :)

I choose this one over Yoshido because it's easier to create the schema based on entity's annotations than on Yoshido :)

Anyway, i got a problem to understand how to create a custom scalar type. I need to create 2 customs scalar type: LargeText (based on String) and a DateTimeType

To generate the schema, i generate a Yaml file which is a representation of the schema based on the documentation available on this repo :)

Query:
    type: object
    config:
        fields:
            user:
                type: '[UserType]'
                argsBuilder:
                    builder: Read
                    config:
                        entity: User
            role:
                type: '[RoleType]'
                argsBuilder:
                    builder: Read
                    config:
                        entity: Role
Mutation:
    type: object
    config:
        fields:
            create_user:
                type: UserType
                argsBuilder:
                    builder: Create
                    config:
                        entity: User
            update_user:
                type: UserType
                argsBuilder:
                    builder: Update
                    config:
                        entity: User
            remove_user:
                type: String
                argsBuilder:
                    builder: Remove
                    config:
                        entity: User
            create_role:
                type: RoleType
                argsBuilder:
                    builder: Create
                    config:
                        entity: Role
            update_role:
                type: RoleType
                argsBuilder:
                    builder: Update
                    config:
                        entity: Role
            remove_role:
                type: String
                argsBuilder:
                    builder: Remove
                    config:
                        entity: Role
            login:
                type: UserType
                argsBuilder: Login
# Don't know why DateTime type isn't loaded... i can't see it on GraphiQL when i introspect the schema
DateTime:
    type: custom-scalar
    config:
        serialize: ["App\\GraphQL\\Type\\DateTimeType", "serialize"]
        parseValue: ["App\\GraphQL\\Type\\DateTimeType", "parseValue"]
        parseLiteral: ["App\\GraphQL\\Type\\DateTimeType", "parseLiteral"]
UserType:
    type: object
    config:
        fields:
            id:
                type: ID
                description: 'The user id'
            username:
                type: String
            password:
                type: String
                description: 'Encrypted password'
            key:
                type: String
                description: 'The JWT token used to make request to the api'
            role:
                type: RoleType
                description: 'The role ttributed to the user'
UserInput:
    type: input-object
    config:
        fields:
            username:
                type: String!
            password:
                type: String!
                description: 'Encrypted password'
            key:
                type: String
                description: 'The JWT token used to make request to the api'
            role:
                type: ID!
                description: 'The role ttributed to the user'
UserSort:
    type: input-object
    config:
        fields:
            id:
                type: String
                description: 'The user id'
            username:
                type: String
            password:
                type: String
                description: 'Encrypted password'
            key:
                type: String
                description: 'The JWT token used to make request to the api'
UserCriteria:
    type: input-object
    config:
        fields:
            id:
                type: '[ID]'
                description: 'The user id'
            username:
                type: String
            password:
                type: String
                description: 'Encrypted password'
            key:
                type: String
                description: 'The JWT token used to make request to the api'
RoleType:
    type: object
    config:
        fields:
            id:
                type: ID
            users:
                type: '[UserType]'
            role:
                type: String
RoleInput:
    type: input-object
    config:
        fields:
            users:
                type: '[ID]'
            role:
                type: String!
RoleSort:
    type: input-object
    config:
        fields:
            id:
                type: String
            role:
                type: String
RoleCriteria:
    type: input-object
    config:
        fields:
            id:
                type: '[ID]'
            role:
                type: String

I see all the queries , mutations, and custom type (input-object, object), but i can't see the DateTime type. Here is my config for overblogbundle:

overblog_graphql:
    security:
        handle_cors: true
    definitions:
        config_validation: true
        builders:
            args:
                Read: App\Builder\ReadBuilder
                Update: App\Builder\UpdateBuilder
                Remove: App\Builder\RemoveBuilder
                Create: App\Builder\CreateBuilder
                Login: App\Builder\LoginBuilder
        schema:
            resolver_maps:
                - App\Manager\FieldResolverManager
            query: Query
            mutation: Mutation
        mappings:
            types:
                - { type: yaml, dir: "%kernel.project_dir%/config/graphql/types", suffix: ~ }

So i have several questions:

1) Why DateTime type isn't loaded since i declare him in the yaml schema and created the class ?
2) Should i see custom scalar when i introspect the schema via GraphiQL ? I suppose yes :)
3) Is my config right ? it's work for evetything else, except for Custom Scalar type :)
4) Do you need any further information in order to help me ? ^^

So here it is, again, thanks for the bundle, it's just awesome :)

Best regards

question

Most helpful comment

Hey :)

Thx for the quick answer :)

I manage to get this work with your advice :)

so Here is the finished DateTimeType object for other who need this answer:

<?php

namespace App\GraphQL\Type;

use GraphQL\Language\AST\Node;
use GraphQL\Type\Definition\ScalarType;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;

class DateTimeType extends ScalarType implements AliasedInterface
{
    public $name = 'DateTime';

    /**
     * {@inheritdoc}
     */
    public function serialize($value)
    {
        return $value->format('Y-m-d H:i:s');
    }

    /**
     * @param mixed $value
     *
     * @return mixed
     */
    public function parseValue($value)
    {
        return new \DateTime($value);
    }

    /**
     * @param Node $valueNode
     *
     * @return string
     */
    public function parseLiteral($valueNode, $variables = NULL)
    {
        return new \DateTime($valueNode->value);
    }

    /**
     * {@inheritdoc}
     */
    public static function getAliases()
    {
        return ['DateTime'];
    }
}

I think it would be great to replace the DateTime example in the doc with this one :)
And maybe explain in further detail the auto_mapping and auto_discover option :)

Again, thx a lot for this bundle, it's very nice to work with it.

Best regards

All 2 comments

Hi thank you for using this bundle! Your issue is a normal behavior since DateTime type is never used in your Schema so it is never loaded, if you want to make it public anyway add it to types entry

overblog_graphql:
        schema:
            resolver_maps:
                - App\Manager\FieldResolverManager
            query: Query
            mutation: Mutation
            types: ["DateTime"]

see for more details

Hey :)

Thx for the quick answer :)

I manage to get this work with your advice :)

so Here is the finished DateTimeType object for other who need this answer:

<?php

namespace App\GraphQL\Type;

use GraphQL\Language\AST\Node;
use GraphQL\Type\Definition\ScalarType;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;

class DateTimeType extends ScalarType implements AliasedInterface
{
    public $name = 'DateTime';

    /**
     * {@inheritdoc}
     */
    public function serialize($value)
    {
        return $value->format('Y-m-d H:i:s');
    }

    /**
     * @param mixed $value
     *
     * @return mixed
     */
    public function parseValue($value)
    {
        return new \DateTime($value);
    }

    /**
     * @param Node $valueNode
     *
     * @return string
     */
    public function parseLiteral($valueNode, $variables = NULL)
    {
        return new \DateTime($valueNode->value);
    }

    /**
     * {@inheritdoc}
     */
    public static function getAliases()
    {
        return ['DateTime'];
    }
}

I think it would be great to replace the DateTime example in the doc with this one :)
And maybe explain in further detail the auto_mapping and auto_discover option :)

Again, thx a lot for this bundle, it's very nice to work with it.

Best regards

Was this page helpful?
0 / 5 - 0 ratings