Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/simpod/graphql-utils

Set of utilities to power up your development with webonyx/graphql
https://github.com/simpod/graphql-utils

graphql graphql-php graphql-schema graphql-tools graphql-utils php schema-builder schema-builders

Last synced: about 1 month ago
JSON representation

Set of utilities to power up your development with webonyx/graphql

Awesome Lists containing this project

README

        

# PHP GraphQL Utils for graphql-php

[![GitHub Actions][GA Image]][GA Link]
[![Code Coverage][Coverage Image]][CodeCov Link]
[![Downloads][Downloads Image]][Packagist Link]
[![Packagist][Packagist Image]][Packagist Link]
[![Infection MSI][Infection Image]][Infection Link]

## Installation

Add as [Composer](https://getcomposer.org/) dependency:

```sh
composer require simpod/graphql-utils
```

## Features

### Schema Builders

Instead of defining your schema as an array, use can use more objective-oriented approach. This library provides set of strictly typed builders that help you build your schema:

- EnumBuilder
- FieldBuilder
- InputFieldBuilder
- InputObjectBuilder
- InterfaceBuilder
- ObjectBuilder
- TypeBuilder
- UnionBuilder

#### ObjectBuilder and FieldBuilder

✔️ Standard way with `webonyx/graphql-php`

```php
'User',
'description' => 'Our blog visitor',
'fields' => [
'firstName' => [
'type' => Type::string(),
'description' => 'User first name'
],
'email' => Type::string()
],
'resolveField' => static function(User $user, $args, $context, ResolveInfo $info) {
switch ($info->fieldName) {
case 'name':
return $user->getName();
case 'email':
return $user->getEmail();
default:
return null;
}
}
]);
```

✨ The same can be produced in objective way

```php
setDescription('Our blog visitor')
->setFields([
FieldBuilder::create('firstName', Type::string())
->setDescription('User first name')
->build(),
FieldBuilder::create('email', Type::string())->build(),
])
->setFieldResolver(
static function(User $user, $args, $context, ResolveInfo $info) {
switch ($info->fieldName) {
case 'name':
return $user->getName();
case 'email':
return $user->getEmail();
default:
return null;
}
}
)
->build()
);
```

#### EnumBuilder

✔️ Standard way with `webonyx/graphql-php`

```php
'Episode',
'description' => 'One of the films in the Star Wars Trilogy',
'values' => [
'NEWHOPE' => [
'value' => 4,
'description' => 'Released in 1977.'
],
'EMPIRE' => [
'value' => 5,
'description' => 'Released in 1980.'
],
'JEDI' => [
'value' => 6,
'description' => 'Released in 1983.'
],
]
]);
```

✨ The same can be produced in objective way

```php
setDescription('One of the films in the Star Wars Trilogy')
->addValue(4, 'NEWHOPE', 'Released in 1977.')
->addValue(5, 'EMPIRE', 'Released in 1980.')
->addValue(6, 'JEDI', 'Released in 1983.')
->build()
);
```

#### InterfaceBuilder

✔️ Standard way with `webonyx/graphql-php`

```php
'Character',
'description' => 'A character in the Star Wars Trilogy',
'fields' => [
'id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the character.',
],
'name' => [
'type' => Type::string(),
'description' => 'The name of the character.'
]
],
'resolveType' => static function ($value) : object {
if ($value->type === 'human') {
return MyTypes::human();
}

return MyTypes::droid();
}
]);
```

✨ The same can be produced in objective way

```php
setDescription('A character in the Star Wars Trilogy')
->setFields([
FieldBuilder::create('id', Type::nonNull(Type::string()))
->setDescription('The id of the character.')
->build(),
FieldBuilder::create('name', Type::string())
->setDescription('The name of the character.')
->build()
])
->setResolveType(
static function ($value) : object {
if ($value->type === 'human') {
return MyTypes::human();
}

return MyTypes::droid();
}
)
->build()
);
```

#### UnionBuilder

✔️ Standard way with `webonyx/graphql-php`

```php
'SearchResult',
'types' => [
MyTypes::story(),
MyTypes::user()
],
'resolveType' => static function($value) {
if ($value->type === 'story') {
return MyTypes::story();
}

return MyTypes::user();
}
]);
```

✨ The same can be produced in objective way

```php
setResolveType(
static function($value) {
if ($value->type === 'story') {
return MyTypes::story();
}

return MyTypes::user();
}
)
->build()
);
```

[GA Image]: https://github.com/simPod/GraphQL-Utils/workflows/CI/badge.svg

[GA Link]: https://github.com/simPod/GraphQL-Utils/actions?query=workflow%3A%22CI%22+branch%3A0.7.x

[Coverage Image]: https://codecov.io/gh/simPod/GraphQL-Utils/branch/0.7.x/graph/badge.svg

[CodeCov Link]: https://codecov.io/gh/simPod/GraphQL-Utils/branch/0.7.x

[Downloads Image]: https://poser.pugx.org/simpod/graphql-utils/d/total.svg

[Packagist Image]: https://poser.pugx.org/simpod/graphql-utils/v/stable.svg

[Packagist Link]: https://packagist.org/packages/simpod/graphql-utils

[Infection Image]: https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FsimPod%2FGraphQL-Utils%2F0.7.x

[Infection Link]: https://dashboard.stryker-mutator.io/reports/github.com/simPod/GraphQL-Utils/0.7.x