phonetworks/pho-lib-graphql-parser

PHP中通用目的的GraphQL解析库

3.0 2019-05-27 02:02 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:42:45 UTC


README

PHP中通用目的的GraphQL模式解析库。这个库**不**解析GraphQL查询或响应。

安装

安装pho-lib-graphql-parser的推荐方法是通过composer。这很简单,如下所示

composer require phonetworks/pho-lib-graphql-parser

使用方法

假设您有一个名为BasicSchema.graphql的以下GraphQL模式文件

type Basic implements Something {
  id: ID!,
  context: String!
  user: User @the_directive(the_argument: "TheValue")
}

然后,您可以通过以下方式解析它

$parsed = new Pho\Lib\GraphQL\Parser\Parse("BasicSchema.graphql");
foreach($parsed as $entity) {
  $entity_name = $entity->name(); // Basic
  $implementations = $entity->implementations(); // an array with a single element
  $implementation_name = $entity->implementations(0)->name(); // Something
  $directives = $entity->directives(); // no root level directive. But we would have one if it was type Basic @is_a { ...
  $fields = $entity->fields(); // we have three; id, context and user.
  $field_1["name"] = $entity->field(0)->name(); // id
  $field_1["type"] = $entity->field(0)->type(); // ID
  $field_1["nullable"] = $entity->field(0)->nullable(); // false (due to !)
  $field_3["directives"][0] = $entity->field(0)->directive(0)->name(); // the_directive
  $field_3["directives"][0]["arguments"][0]["name"] = $entity->field(0)->directive(0)->name(); // the_argument
  $field_3["directives"][0]["arguments"][0]["value"] = $entity->field(0)->directive(0)->value(); // TheValue 
}

请注意,对于参数值,我们目前只支持String类型。

关于多重继承的说明

截至3.0版本,这是一个纯PHP库。但是,如果您需要“多重继承”,而纯PHP模式目前不支持,则可能需要启用C扩展。

对于多重继承,在安装pho-lib-graphql-parser之前必须满足以下依赖关系。请注意,显示的补丁必须应用,因为模式支持仍然是实验性的。

  1. Facebook的LibGraphQLParser库。确保通过此补丁启用了实验性的模式支持。
  2. graphql-parser-php PHP扩展。确保通过此补丁启用了实验性的模式支持。

要在库侧启用它,您需要在运行此程序时设置一个环境变量,如下所示

LIBGRAPHQL_ON=true

完成此操作后,您可以通过在tests/SchemaWithMultipleInheritanceTest.php文件中注释掉"return"语句来测试多重继承。

许可证

在宽松的MIT许可证条款下发布。