Types can be define 3 different ways:
-
The configuration way
Creating this file extension .types.yml or .types.xml in
src/*Bundle/Resources/config/graphql
orapp/config/graphql
. See the different possible types:You can also define custom dirs using config:
overblog_graphql: definitions: mappings: # auto_discover: false # to disable bundles and root dir auto discover types: - type: yaml # or xml or graphql null dir: "%kernel.root_dir%/.../mapping" # sub directories are also searched # suffix: .types # use to change default file suffix - types: [yaml, graphql] # to include different types from the same dir dir: "%kernel.root_dir%/.../mapping"
-
The PHP way
You can also declare PHP types (any subclass of
GraphQL\Type\Definition\Type
) insrc/*Bundle/GraphQL
orapp/GraphQL
they will be auto discover (thanks to auto mapping). Auto map classes are accessible by service id (example:AppBunble\GraphQL\Type\DateTimeType
), you can also alias a type by implementingOverblog\GraphQLBundle\Definition\Resolver\AliasedInterface
that returns an array of aliases.here an example:
<?php namespace App\GraphQL\Type; use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface; use GraphQL\Type\Definition\ScalarType; class DateTimeType extends ScalarType implements AliasedInterface { /** * {@inheritdoc} */ public static function getAliases() { return ['DateTime', 'Date']; } // ... }
You can also define custom dirs using config:
overblog_graphql: definitions: auto_mapping: directories: - "%kernel.root_dir%/src/*Bundle/CustomDir" - "%kernel.root_dir%/src/AppBundle/{foo,bar}"
If using Symfony 3.3+ disabling auto mapping can be a solution to leave place to native DI
autoconfigure
:overblog_graphql: definitions: auto_mapping: false
Here an example of how this can be done with DI
autoconfigure
:services: _instanceof: GraphQL\Type\Definition\Type: tags: ['overblog_graphql.type'] App\Type\: resource: '../src/Type'
Note:
- Types are lazy loaded so when using Symfony DI
autoconfigure
or this bundle auto mapping, the only access to type is FQCN (or aliases if implements the aliases interface). - When using service id as FQCN in yaml definition, backslashes must be correctly escaped,
- Types are lazy loaded so when using Symfony DI
-
The service way
Creating a service tagged
overblog_graphql.type
services: AppBundle\GraphQL\Type\DateTime: # only for sf < 3.3 #class: AppBundle\GraphQL\Type\DateTime tags: - { name: overblog_graphql.type, alias: DateTime }