PHPackages                             infinityloop-dev/graphpinator-constraint-directives - 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. infinityloop-dev/graphpinator-constraint-directives

ActiveLibrary[API Development](/categories/api)

infinityloop-dev/graphpinator-constraint-directives
===================================================

Typesystem directives to declare additional validation on top of GraphQL type system.

v2.1(4mo ago)310.6k↓35.7%2[1 PRs](https://github.com/graphpql/graphpinator-constraint-directives/pulls)4MITPHPPHP &gt;=8.2CI passing

Since Feb 11Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/graphpql/graphpinator-constraint-directives)[ Packagist](https://packagist.org/packages/infinityloop-dev/graphpinator-constraint-directives)[ Docs](https://github.com/graphpql/)[ RSS](/packages/infinityloop-dev-graphpinator-constraint-directives/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (15)Versions (26)Used By (4)

GraPHPinator Constraint directives [![PHP](https://github.com/graphpql/graphpinator-constraint-directives/actions/workflows/php.yml/badge.svg)](https://github.com/graphpql/graphpinator-constraint-directives/actions/workflows/php.yml) [![codecov](https://camo.githubusercontent.com/2a23aaae1ae1b255f90d7e5f28ad1a088618c128e06915ed04b6f8d173381daf/68747470733a2f2f636f6465636f762e696f2f67682f696e66696e6974796c6f6f702d6465762f677261706870696e61746f722d636f6e73747261696e742d646972656374697665732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/infinityloop-dev/graphpinator-constraint-directives)
=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#graphpinator-constraint-directives--)

⚡🌐⚡ Typesystem directives declare additional validation on the top of the GraphQL type system.

Introduction
------------

[](#introduction)

This package allows server to declare additional constraints on accepted values for arguments, fields and input fields. It is also possible for client to declare constraints for variables in request document.

Additional benefit of using constraint directives is that expected values are displayed to client using GraphQL type language in a self-documenting manner.

Installation
------------

[](#installation)

Install package using composer

`composer require infinityloop-dev/graphpinator-constraint-directives`

How to use
----------

[](#how-to-use)

In order to enable constraint directives on your server, the only thing you need to do is to put selected directives to your `Container`. To avoid cyclic dependencies `ConstraintDirectiveAccessor` must be implemented. This step should be automated when using a DI solution.

Here is example configuration for Nette DI:

```
- Graphpinator\ConstraintDirectives\StringConstraintDirective
- Graphpinator\ConstraintDirectives\IntConstraintDirective
- Graphpinator\ConstraintDirectives\FloatConstraintDirective
- Graphpinator\ConstraintDirectives\ListConstraintDirective
- Graphpinator\ConstraintDirectives\ObjectConstraintDirective
- Graphpinator\ConstraintDirectives\ListConstraintInput
- Graphpinator\ConstraintDirectives\ConstraintDirectiveAccessor(
    string: Graphpinator\ConstraintDirectives\StringConstraintDirective
    int: Graphpinator\ConstraintDirectives\IntConstraintDirective
    float: Graphpinator\ConstraintDirectives\FloatConstraintDirective
    list: Graphpinator\ConstraintDirectives\ListConstraintDirective
    object: Graphpinator\ConstraintDirectives\ObjectConstraintDirective
    listInput: Graphpinator\ConstraintDirectives\ListConstraintInput
)
```

### Add constraint to Argument

[](#add-constraint-to-argument)

The most common usage of constraint directives is to validate input from client without having to do it yourself in the resolve function.

```
$intConstraint; // instance of \Graphpinator\ConstraintDirectives\IntConstraintDirective

\Graphpinator\Typesystem\Argument\Argument::create(
    'year'
    \Graphpinator\Typesystem\Container::Int(),
)->addDirective(
    $intConstraint,
    ['min' => 1900, 'max' => 2021],
);
```

### Add constraint to Field

[](#add-constraint-to-field)

Additional usage of constraint directives is to validate output from your resolve functions.

```
$intConstraint; // instance of \Graphpinator\ConstraintDirectives\IntConstraintDirective

\Graphpinator\Typesystem\Field\Field::create(
    'year'
    \Graphpinator\Typesystem\Container::Int(),
)->addDirective(
    $intConstraint,
    ['min' => 1900, 'max' => 2021],
);
```

### Add constraint to Type &amp; Interface &amp; InputType

[](#add-constraint-to-type--interface--inputtype)

Special case is `ObjectConstraint` which declares additional information on which fields must be filled. It is a flexible solution to the input-union problem, but can also be applied on Interface/Type to semantically indicate which values are returned.

```
class DogOrCatInput extends \Graphpinator\Typesystem\InputType
{
    protected const NAME = 'DogOrCatInput';

    public funtion __construct(
        \Graphpinator\ConstraintDirectives\ObjectConstraintDirective $objectConstraint,
    )
    {
        parent::__construct();
        $this->addDirective($objectConstraint, ['exactlyOne' => ['dog', 'cat']]);
    }

    protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet
    {
        return new \Graphpinator\Typesystem\Argument\ArgumentSet([
            \Graphpinator\Typesystem\Argument\Argument::create('dog', \Graphpinator\Typesystem\Container::String()),
            \Graphpinator\Typesystem\Argument\Argument::create('cat', \Graphpinator\Typesystem\Container::String()),
        ]);
    }
}
```

### Variance

[](#variance)

Question of variance comes into play because field, argument, and object constraints can be declared in an interface context and then implemented by the concrete type. Traditional rules apply here.

- Covariance for Field constraints - child can restrict parent's constraint, but may not release it.
- Contravariance for Argument constraints - child can soften parent's constraint, but may not restrict it.
- Invariance for Object constraints - child must contain the same constraint as parent.

### Directive options

[](#directive-options)

- `@stringConstraint`
    - minLength - `Int`
    - maxLength - `Int`
    - regex - `String`
    - oneOf - `[String!]`
- `@intConstraint`
    - min - `Int`
    - max - `Int`
    - oneOf - `[Int!]`
- `@floatConstraint`
    - min - `Float`
    - max - `Float`
    - oneOf - `[Float!]`
- `@listConstraint`
    - minItems - `Int`
    - maxItems - `Int`
    - unique - `Boolean`
    - innerList - object with the same arguments to recursivelly apply constraint to inner list
- `@uploadConstraint`
    - maxSize - `Int`
    - mimeType - `[String!]`
- `@objectConstraint`
    - atLeastOne - `[String!]`
    - atMostOne - `[String!]`
    - exactlyOne - `[String!]`
    - atLeast - `{count: Int!, from: [String!]!}`
    - atMost - `{count: Int!, from: [String!]!}`
    - exactly - `{count: Int!, from: [String!]!}`

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance79

Regular maintenance activity

Popularity30

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 62.8% 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 ~85 days

Recently: every ~198 days

Total

22

Last Release

126d ago

Major Versions

v1.3.2 → v2.02025-12-18

PHP version history (3 changes)v1.2PHP &gt;=8.0.1

v1.3.2PHP &gt;=8.1

v2.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/00654c913028f8d25a226eb2ddf368e1b999bf72fa4f77dbd1449c1df218756c?d=identicon)[peldax](/maintainers/peldax)

---

Top Contributors

[![peldax](https://avatars.githubusercontent.com/u/10790033?v=4)](https://github.com/peldax "peldax (147 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (74 commits)")[![vossik](https://avatars.githubusercontent.com/u/22434658?v=4)](https://github.com/vossik "vossik (11 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![HonzaBejvl](https://avatars.githubusercontent.com/u/23004049?v=4)](https://github.com/HonzaBejvl "HonzaBejvl (1 commits)")

---

Tags

graphqlgraphql-phpphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/infinityloop-dev-graphpinator-constraint-directives/health.svg)

```
[![Health](https://phpackages.com/badges/infinityloop-dev-graphpinator-constraint-directives/health.svg)](https://phpackages.com/packages/infinityloop-dev-graphpinator-constraint-directives)
```

###  Alternatives

[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)[hubspot/api-client

Hubspot API client

23914.2M16](/packages/hubspot-api-client)[botman/driver-telegram

Telegram driver for BotMan

92437.3k6](/packages/botman-driver-telegram)

PHPackages © 2026

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