-
Notifications
You must be signed in to change notification settings - Fork 222
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
Allow entity id scalar generation #694
Comments
Hi @Vincz, I am already working for couple of months on a feature called Hydrator. It will basically allow you to assotiate a model class to an Mutation:
type: object
config:
fields:
createHero:
resolve: "@=mut('create_hero', [models])"
args:
input: HeroInput!
HeroInput:
type: input-object
model: App\Model\HeroInput
config:
fields:
name:
type: "String!"
partnerId:
type: "HeroId" Model: class Hero
{
public string $name;
/*
* @Hydrator\Field("parentId") - this only required because fields have different names
* @Converter\Entity
*/
public Hero $parent;
} Resolver: public function resolve(Models $models)
{
$heroInput = $models->get('input')
// or
$heroInput = $model->input;
} So this eventually doesn't change args, but introduces There will be some integrated converters, like Hydrator will also be highly integrated with the Validator feature. So eventually what you have to do is just inject Later, if it's conviniet to use, we can make this Hydrator to work with your annotations. Is this what would help you in your situation? |
@murtukov Yes, this would be the kind of thing that could help, but as you said it's kind of the same as the Arguments transformer. $properties[$nameUpdate] = [
'args' => [
'id' => ['type' => \sprintf('%s%s', $idType, $isCreatable ? '' : '!')],
'input' => ['type' => \sprintf('%s!', $inputType)],
],
'description' => \sprintf('Update or create an object of type %s', $type),
'type' => $type,
'resolve' => \sprintf('@=call(service("doctrine").getRepository(service("%s").getEntity("%s")).%s, arguments({id: "%s", input: "%s"}, args))', $resolver, $type, 'update', $idType, $inputType),
]; And then, I have a generic update resolver that looks like that: public function update($id, $input)
{
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$object = new $this->className();
if ($id) {
$object = $this->find($id);
} else {
$this->persist($object);
}
$properties = \get_object_vars($input);
foreach ($properties as $property => $propertyValue) {
$value = $propertyAccessor->getValue($input, $property);
$propertyAccessor->setValue($object, $property, $value);
}
$this->flush();
return $object;
} So, really quick, for each entity, I have:
In 95% of the case, I don't need to customise the default behaviour. The auto-guessing features saves me a lot of time. And regarding the hydrator, it's already working with the Scalar entity Id. If I have a input with a scalar At the end, the key here is the ability to link GraphQL with PHP classes. I think we are trying to solve the same problems, but that we are not going in the same direction, because you go from GraphQL (describe in Yaml) to PHP (like in your example by adding a Maybe we could organise some sort of meeting and discuss a bit about the current state of the project and where we would like to go. Anyway, beside this discussion, I also wanted to thank you for always taking the time to answer constructively to issues and for all the time and efforts you put in the project this days ❤️ ping @mcg-web |
I'm currently working on a pretty big corporate application. So I have many entities with cruds, forms, etc... So a lot of code is "automatic" and it's great. I know feel the same need expressed here : #683
The use case is quite simple. When I create a form for an entity (and I have many), I describe my Input with foreign key id as Int and I have to convert them back to entities in my resolver.
For example:
And in my resolver, I have to do something like :
So, I have to be specific for each different entity (one resolver by entity), preventing me from having a single generic resolve function.
If we could automatically generate scalars to represent each entity, we could save a lot of time.
The Input would become:
and the resolver :
and the generated scalar would simply be a id to entity converter like #683 (comment) for example.
The option could be opt in by simply adding a "generateScalar" boolean option on the
@GQL\Type
annotation. I'm not sure how we could implement this out of annotation.Let me know what you think.
The text was updated successfully, but these errors were encountered: