PHPackages                             almservices/attributed-graphql-model-types - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. almservices/attributed-graphql-model-types

ActiveLibrary[API Development](/categories/api)

almservices/attributed-graphql-model-types
==========================================

Attribute based graphql model/types

1.1.1(3y ago)0322MITPHPPHP ^7.4 || ^8.0

Since Mar 24Pushed 3y agoCompare

[ Source](https://github.com/almservices/attributed-graphql-model-types)[ Packagist](https://packagist.org/packages/almservices/attributed-graphql-model-types)[ RSS](/packages/almservices-attributed-graphql-model-types/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (4)Used By (0)

Attributed GraphQL model types
==============================

[](#attributed-graphql-model-types)

[![Latest stable version](https://camo.githubusercontent.com/b1f7deb5fda744f31289e837ca744d1060416952aab96b2e3d4774bc9fc4683d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c6d73657276696365732f617474726962757465642d6772617068716c2d6d6f64656c2d74797065732e7376673f6c6162656c3d63757272656e7425323076657273696f6e)](https://packagist.org/packages/almservices/attributed-graphql-model-types)[![CI Status](https://github.com/almservices/attributed-graphql-model-types/workflows/CI/badge.svg?branch=main)](https://github.com/almservices/attributed-graphql-model-types/actions)[![Coverage Status](https://camo.githubusercontent.com/435bc4ce658f61a8dafc9025a23d7b278581743577e8b36dfa4ca82290a6cb4f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f616c6d73657276696365732f617474726962757465642d6772617068716c2d6d6f64656c2d74797065732f62616467652e737667)](https://coveralls.io/github/almservices/attributed-graphql-model-types)[![PHP version](https://camo.githubusercontent.com/0885e093a47f527c3cda15647724116dfa7fdc563c9af8b2f441f94ae6775ebe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616c6d73657276696365732f617474726962757465642d6772617068716c2d6d6f64656c2d74797065732e737667)](https://php.net)[![License](https://camo.githubusercontent.com/ba113bc1dc3c8ff6512a4d65376800cd0c2808259aed7da0a43c15daab43af86/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616c6d73657276696365732f617474726962757465642d6772617068716c2d6d6f64656c2d74797065732e737667)](LICENSE)

Requirements
============

[](#requirements)

- Composer
- PHP &gt;=7.4

Installation
============

[](#installation)

`composer require almservices/attributed-graphql-model-types`

Usage
=====

[](#usage)

Given we have such model

```
#[Model(name: "AnimalAlias")]
class Animal
{
    #[ID]
    #[Field]
    public int $id;

    #[Field]
    public string $name;
}
```

or with php7.4

```
/**
 * @Model(name: "AnimalAlias")
 */
class Animal
{
    /**
     * @ID
     * @Field
     */
    public int $id;

    /**
     * @Field
     */
    public string $name;
}
```

We can create GraphQL type by

```
class AnimalType extends ModelType
{
    public function __construct(bool $isProd)
    {
        parent::__construct(Animal::class, new TypeContainer($isProd), $isProd);
    }
}
```

or directly by

```
new ModelType(Animal::class, new TypeContainer($isProd), $isProd);
```

which will be equivalent to

```
class AnimalType extends \GraphQL\Type\Definition\ObjectType {
    public function __construct() {
        parent::__construct([
            'name' => 'AnimalAlias',
            'fields' => static fn () => [
                'id' => [
                    'resolve' => static fn (Animal $animal) => $animal->id,
                    'type' => self::nonNull(self::id()),
                ],
                'name' => [
                    'resolve' => static fn (Animal $animal) => $animal->name,
                    'type' => self::nonNull(self::string()),
                ],
            ],
        ]);
    }
}
```

Model Examples
==============

[](#model-examples)

Field
-----

[](#field)

```
#[Model(name: "Foo")]
class Foo
{
    #[Field]
    public int $bar;

    #[Field]
    public function baz(): string
    {
        return 'baz';
    }
}
```

Alias
-----

[](#alias)

```
#[Model(name: "AnimalAlias")]
class Foo
{
    #[Alias("bar")]
    #[Field]
    public int $foo;
}
```

Enum
----

[](#enum)

PHP 8.1

```
#[Model(name: "Family")]
enum Family
{
    case SEAL;
    case BEAR;
}
```

inline version for PHP &lt; 8.1

```
#[Model(name: "Foo")]
class InlineEnum
{
    #[Field]
    #[Enum("SingleEnum", "A", "B", "C", "D")]
    public string $single;

    #[Field]
    #[ListOf("ListEnum")]
    #[Enum("ListEnum", "A", "B", "C", "D")]
    public array $list;
}
```

other options are:

```
#[Model("SomeEnum")]
class SomeEnum: string
{
    case FOO = 'foo'; // FOO
}
```

```
#[Model("SomeEnum")]
class SomeEnum: int
{
    case FOO = 1; // FOO
}
```

```
#[Model("SomeEnum")]
class SomeEnum: string
{
    #[Alias("foo")]
    case FOO = "bar"; // foo
}
```

```
#[Model("SomeEnum")]
class SomeEnum: int
{
    #[Alias("foo")]
    case FOO = 1; // foo
}
```

Ignoring specific fields:

```
#[Model("SomeEnum")]
class SomeEnum: int
{
    case FOO;
    case BAR;
    #[Ignore]
    case BAZ;
}
```

Lists
-----

[](#lists)

```
#[Model("Foo")]
class Foo
{
    #[Field]
    #[ListOf(type: "string")]
    public \Traversable $test; // [String]!

    #[Field]
    #[ListOf(type: "string")]
    public array $foo; // [String]!

    #[Field]
    #[ListOf(type: self::class)]
    public iterable $bar; // [Foo]!

    #[Field]
    #[ListOf(type: OtherModel::class)]
    public array $baz; // [OtherModel]!

    #[Field]
    #[ListOf(type: OtherModel::class)]
    public \Doctrine\Common\Collections\Collection $qux; // [OtherModel]!
}
```

for non-nullable items use NonNull

```
#[Model("Foo")]
class Foo {
    #[Field]
    #[NonNull]
    #[ListOf("string")]
    public array $list; // [String!]!
}
```

Value Object
------------

[](#value-object)

Legacy or custom Value Objects that can be cast to string, can be used as model property

```
class FooBar implements Stringable
{
    private string $type;

    private function __construct(string $type)
    {
        $this->type = $type;
    }

    public static function foo(): self
    {
        return new self('foo');
    }

    public static function bar(): self
    {
        return new self('foo');
    }

    public function __toString(): string
    {
        return $this->type;
    }
}
```

example:

```
#[Model(name: "Foo")]
class Foo {
    #[ID]
    #[Field]
    public readonly FooBar $id;
}
```

but if value is more complex it can become resolved on demand

```
#[Model(name: "Foo")]
class Foo {
    public function __construct(
        private string $foo,
        private string $bar,
    ) {}

    #[ID]
    #[Field]
    public function id(): string
    {
        return $this->foo . $this->bar;
    }
}
```

Deprecated
----------

[](#deprecated)

```
#[Model(name: "Foo")]
class Foo {
    #[Field]
    #[Deprecated("Do not use Foo.foo, use Foo.bar instead")]
    public function foo(): string
    {
        return 'foo';
    }

    #[Field]
    public function bar(): string
    {
        return 'bar';
    }
}
```

Description
-----------

[](#description)

```
#[Description("Foo is Bar")]
#[Model(name: "Foo")]
class Foo {
    #[Field]
    #[Description("Foo foo?")]
    public function foo(): string
    {
        return 'foo';
    }
}
```

More on resolvers
-----------------

[](#more-on-resolvers)

```
#[Model("Foo")]
class Foo {
    #[Field]
    #[Argument(name: "bar", type: "string", nullable: false)]
    #[Argument(name: "baz", type: "[String!]!")]
    #[Argument(name: "qux", type: "[string]", nullable: false)]
    public function bar(
        #[ArrayShape([
            "baz" => "string[]"
        ])]
        array $args
    ): string
    {
        return implode(", ", $args['baz']);
    }
}
```

Example with objective input type

```
class MyInput extends InputObjectType {
    public function __construct()
    {
        parent::__construct("MyInput", $this->fields());
    }

    /**
     * @return \Generator
     */
    private function fields(): \Generator
    {
        yield new Field("foo", Type::nonNull(Type::string()));
    }
}

// we need to register MyInput into map
$typeContainer = new TypeContainer();
$typeContainer->set(MyInput::class, new MyInput());
$typeContainer->set("MyInput", new MyInput());

#[Model("Foo")]
class Foo {
    #[Field]
    #[Argument(name: "baz", type: MyInput::class, nullable: false)]
    public function bar(
        #[ArrayShape([
            "baz" => [
                "foo" => "string"
            ]
        ])]
        array $args
    ): string
    {
        return $args['baz']['foo'];
    }
}
```

Example of fully qualified resolver

```
#[Model("Foo")]
class Foo {
    #[Field]
    public function bar(
        #[ArrayShape([])] array $args,
        Context $context,
        ResolveInfo $resolveInfo
    ): string
    {
        return '';
    }
}
```

Demo
====

[](#demo)

To run demo execute

`php -S 127.0.0.1:8000 demo/index.php`

`curl 127.0.0.1:8000 -d '{"query": "{myUser{id firstName lastName}}"}' -H "Content-Type: application/json" -H 'Authorization: dev'`

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~66 days

Total

3

Last Release

1384d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/43e5f3c381362868a775f822f2f0bd6d32d77d0226a2d2716e63c65311923ff5?d=identicon)[kubawerlos](/maintainers/kubawerlos)

![](https://www.gravatar.com/avatar/ce7fa0b3229749a77e0a4793fb5c6039a85029ed97a59c3d9adcf41d59b5df0e?d=identicon)[grzegorz.stachniuk](/maintainers/grzegorz.stachniuk)

---

Top Contributors

[![grzegorzstachniukalm](https://avatars.githubusercontent.com/u/41567377?v=4)](https://github.com/grzegorzstachniukalm "grzegorzstachniukalm (7 commits)")

---

Tags

graphqlphpwebonyxgraphqlmodelannotationattribute

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/almservices-attributed-graphql-model-types/health.svg)

```
[![Health](https://phpackages.com/badges/almservices-attributed-graphql-model-types/health.svg)](https://phpackages.com/packages/almservices-attributed-graphql-model-types)
```

###  Alternatives

[nuwave/lighthouse

A framework for serving GraphQL from Laravel

3.5k10.7M93](/packages/nuwave-lighthouse)[overblog/graphql-bundle

This bundle provides tools to build a GraphQL server in your Symfony App.

8027.9M28](/packages/overblog-graphql-bundle)[aimeos/ai-admin-graphql

Aimeos Admin GraphQL API extension

944100.0k4](/packages/aimeos-ai-admin-graphql)[mll-lab/graphql-php-scalars

A collection of custom scalar types for usage with https://github.com/webonyx/graphql-php

1394.2M28](/packages/mll-lab-graphql-php-scalars)[ivome/graphql-relay-php

A PHP port of GraphQL Relay reference implementation

271632.4k5](/packages/ivome-graphql-relay-php)[overblog/graphql-php-generator

GraphQL types generator

29518.9k](/packages/overblog-graphql-php-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
