Giter VIP home page Giter VIP logo

Comments (1)

PowerKiKi avatar PowerKiKi commented on June 13, 2024

Here is a fully working example that can not reproduce your issue. In particular see the $schema->assertValid(). I suggest you integrate that method call in your automated test suites.

Also notice that SchemaValidator will load all PHP files recursively, starting from the folder where the example file lives. So you should probably isolate it in its own folder to avoid scanning your entire project (and vendor) for nothing.

Let me know here if you are able to modify this working example and break it according to your real use-case.

<?php

require '../vendor/autoload.php';

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\ORM\Tools\Setup;
use GraphQL\Doctrine\Types;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;

/**
 * @ORM\Entity
 */
class User
{
    /**
     * @var null|int
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    public function getId(): ?int
    {
        return $this->id;
    }
}

/**
 * @ORM\Entity
 */
class RankMonitor
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var User
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="userId", referencedColumnName="id", nullable=false)
     */
    private $user;

    /**
     * @return User
     */
    public function getUser(): User
    {
        return $this->user;
    }

    /**
     * @param User $user
     *
     * @return RankMonitor
     */
    public function setUser(User $user): RankMonitor
    {
        $this->user = $user;

        return $this;
    }
}

// Create the entity manager
$config = Setup::createAnnotationMetadataConfiguration([__DIR__], true, null, null, false);
$connection = ['url' => 'sqlite:///:memory:'];
$entityManager = EntityManager::create($connection, $config);

// Configure the type registry
$types = new Types($entityManager);

// Build your Schema
$schema = new Schema([
    'query' => new ObjectType([
        'name' => 'query',
        'fields' => [
            'test' => [
                'type' => Type::int(),
                'resolve' => function ($root, $args) use ($types): int {
                    return 1;
                },
            ],
        ],
    ]),
    'mutation' => new ObjectType([
        'name' => 'mutation',
        'fields' => [
            'createRankMonitor' => [
                'type' => Type::nonNull($types->getOutput(RankMonitor::class)),
                'args' => [
                    'input' => Type::nonNull($types->getInput(RankMonitor::class)),
                ],
                'resolve' => function ($root, $args, $context) {
                    return new RankMonitor();
                },
            ],
        ],
    ]),
]);

// check everything is OK in Doctrine
$validator = new SchemaValidator($entityManager);
$errors = $validator->validateMapping();
var_dump($errors);

// check everything is OK in GraphQL
$schema->assertValid();

echo 'OK' . PHP_EOL;

from graphql-doctrine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.