x-graphql / codegen
Generating PHP code for executing GraphQL
Installs: 9 945
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: >=8.2
- nette/php-generator: ^3.6 || ^4.0
- symfony/console: ^6.4 || ^7.0
- symfony/event-dispatcher: ^6.4 || ^7.0
- symfony/string: ^6.4 || ^7.0
- webonyx/graphql-php: ^15.9
- x-graphql/delegate: ^0.1.0
Requires (Dev)
- phpunit/phpunit: ^11.0
- symfony/filesystem: ^6.4 || ^7.0
- symplify/easy-coding-standard: ^12.1
README
Generating PHP code for executing GraphQL
Getting Started
Install this package via Composer
composer require x-graphql/codegen
Usages
After install, you need to generate config file with command bellow:
./vendor/bin/x-graphql-codegen x-graphql:codegen:init-config
Your config file x-graphql-codegen.php
initialized look like:
<?php return [ 'default' => [ /// PSR 4 namespace, classes and traits generated will use. 'namespace' => 'App\GraphQL\Codegen', /// Where storing GraphQL queries files. 'sourcePath' => __DIR__ . '/', /// Where storing PHP generated code with namespace above. 'destinationPath' => __DIR__ . '/', /// Generated query class name. 'queryClassName' => 'GraphQLQuery', ] ];
Edit it for suitable with your environment and create some graphql query files in sourcePath
then generate PHP code with command:
./vendor/bin/x-graphql-codegen x-graphql:codegen:generate
Example
Add x-graphql/http-schema
package for executing schema over http:
composer require x-graphql/http-schema
Init config file:
./vendor/bin/x-graphql-codegen x-graphql:codegen:init-config
Add example.graphql
to your current working directory with content:
query getCountries { countries { name } }
Run command to generate PHP code:
./vendor/bin/x-graphql-codegen x-graphql:codegen:generate
Use generated query class:
<?php require __DIR__ . '/vendor/autoload.php'; $delegator = new \XGraphQL\HttpSchema\HttpDelegator('https://countries.trevorblades.com/'); $schema = \XGraphQL\HttpSchema\HttpSchemaFactory::createFromIntrospectionQuery($delegator); $query = new \App\GraphQL\Codegen\GraphQLQuery($schema); $result = $query->getCountries(); var_dump($result->toArray());
Credits
Created by Minh Vuong