深入解析webonyx/graphql-php核心类与API

深入解析webonyx/graphql-php核心类与API

graphql-php PHP implementation of the GraphQL specification based on the reference implementation in JavaScript graphql-php 项目地址: https://gitcode.com/gh_mirrors/gr/graphql-php

GraphQL作为现代API开发的重要技术,其PHP实现webonyx/graphql-php提供了强大而灵活的工具集。本文将深入解析该库的核心类及其API,帮助开发者更好地理解和运用GraphQL在PHP环境中的实现。

GraphQL核心执行类

GraphQL\GraphQL类是执行GraphQL查询的主要入口点,它提供了多种静态方法来处理GraphQL操作。

executeQuery方法详解

executeQuery是执行GraphQL查询的核心方法,其参数配置非常丰富:

static function executeQuery(
    GraphQL\Type\Schema $schema,
    $source,
    $rootValue = null,
    $contextValue = null,
    ?array $variableValues = null,
    ?string $operationName = null,
    ?callable $fieldResolver = null,
    ?array $validationRules = null
): GraphQL\Executor\ExecutionResult

参数说明:

  • schema: 必填,GraphQL类型系统,用于验证和执行查询
  • source: 可以是字符串或DocumentNode,表示要执行的GraphQL查询
  • rootValue: 作为顶级类型解析器函数的第一个参数
  • contextValue: 在所有解析器函数中共享的上下文信息
  • variableValues: 变量名到运行时值的映射
  • operationName: 当查询包含多个操作时指定要执行的操作名
  • fieldResolver: 自定义字段解析器,不提供则使用默认解析器
  • validationRules: 查询验证规则集合,默认为所有可用规则

promiseToExecute方法

对于异步PHP平台,可以使用promiseToExecute方法,它需要PromiseAdapter并始终返回Promise对象:

static function promiseToExecute(
    GraphQL\Executor\Promise\PromiseAdapter $promiseAdapter,
    GraphQL\Type\Schema $schema,
    $source,
    $rootValue = null,
    $context = null,
    ?array $variableValues = null,
    ?string $operationName = null,
    ?callable $fieldResolver = null,
    ?array $validationRules = null
): GraphQL\Executor\Promise\Promise

类型系统核心类

GraphQL\Type\Definition\Type类是GraphQL类型系统的核心,它既是标准类型的注册表,也是所有其他类型的基类。

基本标量类型

提供了一系列静态方法来获取标准GraphQL标量类型:

static function int(): ScalarType    // 整型
static function float(): ScalarType  // 浮点型
static function string(): ScalarType // 字符串
static function boolean(): ScalarType // 布尔型
static function id(): ScalarType     // ID类型

类型修饰器

GraphQL提供了两种类型修饰器来构建复杂类型:

// 列表类型
static function listOf($type): ListOfType

// 非空类型
static function nonNull($type): NonNull

类型检查方法

提供了一系列类型检查方法,用于确定给定类型的性质:

static function isInputType($type): bool    // 是否为输入类型
static function isOutputType($type): bool   // 是否为输出类型
static function isLeafType($type): bool     // 是否为叶子类型
static function isCompositeType($type): bool // 是否为复合类型
static function isAbstractType($type): bool // 是否为抽象类型

解析信息类

GraphQL\Type\Definition\ResolveInfo类包含了字段解析过程中有用的信息,它作为第四个参数传递给每个字段解析器。

核心属性

public $fieldDefinition;   // 正在解析的字段定义
public $fieldName;        // 正在解析的字段名
public $returnType;       // 字段预期的返回类型
public $fieldNodes;       // 查询中引用此字段的所有节点
public $parentType;       // 字段的父类型
public $path;             // 从根值到该字段的路径(包含别名)
public $unaliasedPath;    // 从根值到该字段的路径(不包含别名)
public $schema;           // 执行使用的模式实例
public $fragments;        // 查询中定义的所有片段
public $rootValue;        // 传递给查询执行的根值
public $operation;        // 操作定义节点(查询/变更)
public $variableValues;   // 传递给查询执行的变量数组

实用方法

getFieldSelection()方法返回查询中为当前字段选择的所有字段名称,最多到指定深度:

function getFieldSelection(int $depth = 0): array

getFieldSelectionWithAliases()方法更加强大,它不仅返回字段名称,还包括参数和别名信息:

function getFieldSelectionWithAliases(int $depth = 0): array

指令位置枚举

GraphQL\Language\DirectiveLocation枚举定义了所有可用的指令位置,分为三类:

  1. 可执行位置(EXECUTABLE_LOCATIONS):

    • QUERY, MUTATION, SUBSCRIPTION
    • FIELD, FRAGMENT_DEFINITION, FRAGMENT_SPREAD, INLINE_FRAGMENT
    • VARIABLE_DEFINITION
  2. 类型系统位置(TYPE_SYSTEM_LOCATIONS):

    • SCHEMA, SCALAR, OBJECT, FIELD_DEFINITION, ARGUMENT_DEFINITION
    • INTERFACE, UNION, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION
  3. 所有位置(LOCATIONS): 包含上述两类所有位置

模式配置类

GraphQL\Type\SchemaConfig类用于配置模式构造选项,提供了灵活的构建方式:

$config = SchemaConfig::create()
    ->setQuery($myQueryType)
    ->setTypeLoader($myTypeLoader);

主要配置选项包括:

  • 查询类型(query)
  • 变更类型(mutation)
  • 订阅类型(subscription)
  • 自定义类型集合(types)
  • 自定义指令集合(directives)
  • 类型加载器(typeLoader)
  • 验证跳过标志(assumeValid)
  • AST节点配置(astNode, extensionASTNodes)

通过深入理解这些核心类和API,开发者可以更高效地构建和维护基于webonyx/graphql-php的GraphQL服务,充分利用GraphQL的强大功能来满足各种API开发需求。

graphql-php PHP implementation of the GraphQL specification based on the reference implementation in JavaScript graphql-php 项目地址: https://gitcode.com/gh_mirrors/gr/graphql-php

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘俭渝Erik

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值