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

[Bug]: mutation must have unique named fields from objectbrick #894

Open
LeonNoBears opened this issue Oct 11, 2024 · 2 comments
Open

[Bug]: mutation must have unique named fields from objectbrick #894

LeonNoBears opened this issue Oct 11, 2024 · 2 comments
Labels

Comments

@LeonNoBears
Copy link

Expected behavior

Mutation is possible by calling the fields by either only their code e.g. foo or with the objec obrick as a prefix e.g. ObjectBrickFoo.foo

Actual behavior

gives an error because the label is picked and set in the scema.

Steps to reproduce

  1. Create a Class (MyClass)
  2. Create a object brick (ObjectBrickFoo), with a datatype table (code: foo, label: table)
  3. Create another object brick (ObjectBrickBar), with a datatype table (code: bar, label: table)
  4. link both the object bricks to the class.
  5. open mutation schema and setup the fields of the class (ObjectBrickFoo.foo and ObjectBrickBar.bar)
  6. fire a mutation and recieve error:
{
  "errors": [
    {
      "message": "Schema must contain unique named types but contains multiple types named \"TableInput\" (see [Introduction - graphql-php](https://webonyx.github.io/graphql-php/type-definitions/#type-registry).) "
    }
  ]
}
@br33f
Copy link

br33f commented Oct 24, 2024

Does anyone have a workaround for this one?

@br33f
Copy link

br33f commented Oct 25, 2024

@LeonNoBears
Here is how I fixed this problem (at least temporarly):

In file \Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectMutationFieldConfigGenerator\Table::getGraphQlMutationFieldConfig

public function getGraphQlMutationFieldConfig($nodeDef, $class, $container = null, $params = [])
    {
        $fieldName = $nodeDef['attributes']['attribute'];
        $tableDef = $this->getGraphQlService()->getObjectFieldHelper()->getFieldDefinitionFromKey($class, $fieldName);
        $inputItems = [];
        $numCols = 0;

        if ($tableDef instanceof Data\Table) {
            $numCols = (int) $tableDef->getCols();
        }

        $this->getProcessors($processors, $tableDef);

        $processor = new \Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectInputProcessor\Table($nodeDef, $processors);
        $processor->setGraphQLService($this->getGraphQlService());

        foreach (range(0, $numCols - 1) as $i) {
            $inputItems['col' . $i] = Type::string();
        }
       // Here the changes start

        $dataTypes = $this->getGraphQlService()->getDataObjectDataTypes();

        $rowInput = array_key_exists('row_input', $dataTypes)
            ? $dataTypes['row_input']
            : new InputObjectType([
                'name' => 'RowInput',
                'fields' => $inputItems,
            ]);

        $inputType = array_key_exists('table_input', $dataTypes)
            ? $dataTypes['table_input']
            : new InputObjectType([
                'name' => 'TableInput',
                'fields' => [
                    'replace' => [
                        'type' => Type::boolean(),
                        'description' => 'if true then the entire table will be overwritten',
                    ],
                    'rows' => [
                        'type' => Type::listOf($rowInput),
                    ],
                ],
            ]);

        if (!array_key_exists('row_input', $dataTypes) && !array_key_exists('table_input', $params) ) {
            $newDataTypes = [
                'row_input' => $rowInput,
                'table_input' => $inputType
            ];

            $this->getGraphQlService()->registerDataObjectDataTypes($dataTypes + $newDataTypes);
        }

        return [
            'arg' => $inputType,
            'processor' => [$processor, 'process'],
        ];
    }

This problem is also present in "Image Gallery" and can be fixed analogically.

Basically, if you reuse the same "name" you should make sure to also use the exact same instance. Originally for each new field of type "Table" or "Image gallery" new instance of InputObjectType was created. And if different instances share the same "name", then: \GraphQL\Utils\TypeInfo::extractTypes throws an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants