定制 morphql/morphql-symfony 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

morphql/morphql-symfony

Composer 安装命令:

composer require morphql/morphql-symfony

包简介

Symfony integration for MorphQL — YAML config, autowiring, and .morphql file convention

README 文档

README

Symfony integration for MorphQL — transform data with declarative queries.

YAML configuration · Autowiring · Twig-style file convention · PHP 8.1+

Installation

composer require morphql/morphql-symfony

With Symfony Flex (Currently waiting for submission to symfony/recipes-contrib), this will automatically:

  • Registers MorphQLBundle in bundles.php
  • Creates config/packages/morphql.yaml with sensible defaults
  • Scaffolds the morphql-queries/ directory at your project root

Without Flex, you could have to register the bundle manually in config/bundles.php (depends on your Symfony version):

MorphQL\SymfonyBundle\MorphQLBundle::class => ['all' => true],

The bundle is fully zero-config. All settings have sensible defaults — no YAML file is needed to get started. Just install, drop a .morphql file in morphql-queries/, and you're ready to go.

Configuration (optional)

To customize behavior, create or edit config/packages/morphql.yaml:

morphql:
  # Execution provider: "cli" (bundled Node.js engine) or "server" (remote REST API)
  # provider: cli

  # Directory containing .morphql query files
  # query_dir: '%kernel.project_dir%/morphql-queries'

  # Server provider settings (uncomment if using provider: server)
  # server_url: '%env(MORPHQL_SERVER_URL)%'
  # api_key: '%env(MORPHQL_API_KEY)%'

All options have sensible defaults — zero-config works out of the box.

Full Configuration Reference

Option Default Description
provider cli cli (bundled Node.js) or server (remote REST API)
cli_path (auto) Override CLI binary path
node_path node Path to Node.js binary
cache_dir %kernel.cache_dir%/morphql Compiled query cache
query_dir %kernel.project_dir%/morphql-queries .morphql file directory
server_url http://localhost:3000 MorphQL server URL
api_key API key for server auth
timeout 30 Execution timeout (seconds)

Usage

1. The morphql-queries/ directory

Like Twig templates in templates/, you can store your MorphQL queries in morphql-queries/. Use the .morphql extension for syntax highlighting in supported IDEs.

morphql-queries/
├── invoices/
│   └── to_xml.morphql        # Identifier: 'invoices/to_xml'
└── api_response.morphql      # Identifier: 'api_response'

2. Using the Transformation Registry

Inject the TransformationRegistry to run queries stored in files:

use MorphQL\SymfonyBundle\TransformationRegistry;

class InvoiceService
{
    public function __construct(
        private readonly TransformationRegistry $registry
    ) {}

    public function process(array $data): string
    {
        // Resolves to morphql-queries/invoices/to_xml.morphql
        return $this->registry->transform('invoices/to_xml', $data);
    }
}

3. Direct MorphQL usage

For ad-hoc queries, you can inject the base MorphQL service:

use MorphQL\MorphQL;

class MyController
{
    public function __construct(
        private readonly MorphQL $morphql
    ) {}

    public function index(string $json): string
    {
        return $this->morphql->run('from json to json transform set x = 1', $json);
    }
}

Features

  • Symfony Flex Recipe: Automatic bundle registration, config scaffolding, and query directory creation.
  • Filesystem Discovery: Use standard paths like 'sub/folder/query' — no dot-notation required.
  • Pre-configured: The MorphQL service is automatically configured from your YAML settings.
  • Isomorphic: Switch between cli and server providers via config without changing your code.

Symfony Flex Recipe

The recipe files are staged in the recipe/ directory and need to be submitted to symfony/recipes-contrib for Flex to pick them up automatically.

To submit the recipe:

  1. Fork symfony/recipes-contrib
  2. Create morphql/morphql-symfony/0.1/ in the fork
  3. Copy the contents of recipe/ into that directory
  4. Open a Pull Request following the recipe contribution guide

Until the recipe is accepted, users can manually:

  • Create config/packages/morphql.yaml (copy from recipe/config/packages/morphql.yaml)
  • Create the morphql-queries/ directory at their project root

Troubleshooting

"node" not found

If you use a version manager like NVM, asdf, or Volta, the node binary might not be in the default PATH used by your web server or PHP process.

Solution 1: Set the path in .env

MORPHQL_NODE_PATH=/usr/local/bin/node # Use 'which node' to find yours

Solution 2: Create a system symlink

sudo ln -s $(which node) /usr/local/bin/node

Prerequisites

  • PHP 8.1+
  • Node.js 18+ (for the cli provider)
  • Symfony 5.4+, 6.x, 7.x, or 8.x

License

MIT

morphql/morphql-symfony 适用场景与选型建议

morphql/morphql-symfony 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「transform」 「data-transformation」 「morphql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 morphql/morphql-symfony 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 morphql/morphql-symfony 我们能提供哪些服务?
定制开发 / 二次开发

基于 morphql/morphql-symfony 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 44
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-12