Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Muatation namespace #475

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use TheCodingMachine\GraphQLite\Annotations\Input;
use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotationInterface;
use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotations;
use TheCodingMachine\GraphQLite\Annotations\MuatationNamespace;
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotationInterface;
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations;
use TheCodingMachine\GraphQLite\Annotations\SourceFieldInterface;
Expand Down Expand Up @@ -279,6 +280,16 @@ public function getTypeAnnotation(ReflectionClass $refClass): ?TypeInterface
return $type;
}

/**
* @param ReflectionClass<T> $refClass
*
* @template T of object
*/
public function getNamespaceAnnotation(ReflectionClass $refClass): ?MuatationNamespace
{
return $this->getClassAnnotation($refClass, MuatationNamespace::class);
}

/**
* @param ReflectionClass<T> $refClass
*
Expand Down
38 changes: 38 additions & 0 deletions src/Annotations/MuatationNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace TheCodingMachine\GraphQLite\Annotations;

use Attribute;

/**
* @Annotation
* @Target({"CLASS"})
* @Attributes({
* @Attribute("name", type = "string"),
* })
*/
#[Attribute(Attribute::TARGET_CLASS)]
class MuatationNamespace
Lappihuan marked this conversation as resolved.
Show resolved Hide resolved
{

/** @var string|null */
private $name;

/**
* @param mixed[] $attributes
*/
public function __construct(array $attributes = [], ?string $name = null)
{
$this->name = $name ?? $attributes['name'] ?? null;
}

/**
* Returns the GraphQL name for this type.
*/
public function getName(): ?string
{
return $this->name;
}
}
24 changes: 22 additions & 2 deletions src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,27 @@ public function getQueries(object $controller): array
*/
public function getMutations(object $controller): array
{
return $this->getFieldsByAnnotations($controller, Mutation::class, false);
$refClass = new ReflectionClass($controller);
$namespaceAnnotation = $this->annotationReader->getNamespaceAnnotation($refClass);
if(!$namespaceAnnotation){
return $this->getFieldsByAnnotations($controller, Mutation::class, false);
}

$namespaceFieldName = str_replace("Controller", "", $refClass->getShortName());
$namespaceName = $namespaceAnnotation->getName() ?? $namespaceFieldName."Mutations";

$namespaceType = new MutableObjectType([
'name' => $namespaceName,
'fields' => function () use ($controller) {
return $this->getFieldsByAnnotations($controller, Mutation::class, false);
}
]);
$namespaceType->freeze();
$namespaceField = FieldDefinition::create([
'name' => strtolower($namespaceFieldName),
'type' => $namespaceType
]);
return [$namespaceFieldName => $namespaceField];
}

/**
Expand Down Expand Up @@ -362,7 +382,7 @@ private function getFieldsByMethodAnnotations($controller, ReflectionClass $refC
$name = $queryAnnotation->getName() ?: $this->namingStrategy->getFieldNameFromMethodName($methodName);

if (! $description) {
$description = $docBlockObj->getSummary() . "\n" . $docBlockObj->getDescription()->render();
$description = rtrim($docBlockObj->getSummary() . "\n" . $docBlockObj->getDescription()->render());
}

$fieldDescriptor->setName($name);
Expand Down
49 changes: 49 additions & 0 deletions tests/Fixtures/MutationNamespaces/Controllers/RenameController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace TheCodingMachine\GraphQLite\Fixtures\MutationNamespaces\Controllers;


use TheCodingMachine\GraphQLite\Annotations\MuatationNamespace;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Types\ID;


/**
* @MuatationNamespace(name="CustomNS")
*/
class RenameController
{
/**
* @Query()
*/
public function custom(ID $id): ID
{
return new ID("myID");
}

/**
* @Mutation()
*/
public function create(ID $id): ID
{
return new ID("myID");
}

/**
* @Mutation()
*/
public function update(ID $id): ID
{
return new ID("myID");
}

/**
* @Mutation()
*/
public function delete(ID $id): ID
{
return new ID("myID");
}
}
49 changes: 49 additions & 0 deletions tests/Fixtures/MutationNamespaces/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace TheCodingMachine\GraphQLite\Fixtures\MutationNamespaces\Controllers;


use TheCodingMachine\GraphQLite\Annotations\MuatationNamespace;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Types\ID;


/**
* @MuatationNamespace()
*/
class UserController
{
/**
* @Query()
*/
public function user(ID $id): ID
{
return new ID("myID");
}

/**
* @Mutation()
*/
public function create(ID $id): ID
{
return new ID("myID");
}

/**
* @Mutation()
*/
public function update(ID $id): ID
{
return new ID("myID");
}

/**
* @Mutation()
*/
public function delete(ID $id): ID
{
return new ID("myID");
}
}
19 changes: 19 additions & 0 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
use GraphQL\GraphQL;
use GraphQL\Utils\SchemaPrinter;
use Mouf\Picotainer\Picotainer;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -2045,4 +2046,22 @@ public function testEndToEndInputTypeValidation(): void
$result = GraphQL::executeQuery($schema, $queryString);
$result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS);
}

public function testMuatationNamespaces(): void
{
$arrayAdapter = new ArrayAdapter();
$arrayAdapter->setLogger(new ExceptionLogger());
$schemaFactory = new SchemaFactory(new Psr16Cache($arrayAdapter), new BasicAutoWiringContainer(new EmptyContainer()));
$schemaFactory->addControllerNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\MutationNamespaces\\Controllers');
$schemaFactory->addTypeNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Types');

$schema = $schemaFactory->createSchema();
// $schemaExport = SchemaPrinter::doPrint($schema);
$errors = $schema->validate();
$this->assertSame([], $errors);
$this->assertSame("UserMutations",$schema->getMutationType()->getField("user")->getType()->name);
$this->assertCount(3, $schema->getMutationType()->getField("user")->getType()->config["fields"]());
$this->assertSame("CustomNS",$schema->getMutationType()->getField("rename")->getType()->name);
$this->assertCount(3, $schema->getMutationType()->getField("rename")->getType()->config["fields"]());
}
}