protected function mergeTypeExtensions(DocumentAST $document): DocumentAST
    {
        $document->objectTypeDefinitions()->each(
            function (ObjectTypeDefinitionNode $objectType) use ($document) {
                $name = $objectType->name->value;

                $objectType = $document
                    ->extensionsForType($name)
                    ->reduce(
                        function (ObjectTypeDefinitionNode $relatedObjectType, ObjectTypeExtensionNode $typeExtension): ObjectTypeDefinitionNode {
                            $relatedObjectType->fields = ASTHelper::mergeUniqueNodeList(
                                $relatedObjectType->fields,
                                $typeExtension->fields
                            );

                            return $relatedObjectType;
                        },
                        $objectType
                    );

                // Modify the original document by overwriting the definition with the merged one
                $document->setDefinition($objectType);
            }
        );

        return $document;
    }