0

0

详解PHP中如何安装和使用GraphQL

青灯夜游

青灯夜游

发布时间:2021-05-07 18:38:12

|

4510人浏览过

|

来源于掘金社区

转载

本篇文章带大家了解一下graphql,并详细介绍php中安装和使用graphql的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

详解PHP中如何安装和使用GraphQL

关于 GraphQL

GraphQL 是一种现代化的 HTTP API 接口构建方式,客户端可以按需查询需要的数据。
GraphQL 可以提升 API 调用的灵活性,我们可以像写数据库查询语句一样来请求 API 来获取所需要的数据,这对构建复杂的 API 查询来说非常有用。

与REST对比

REST的核心思想就是资源,每个资源都能用一个URL来表示,你能通过一个GET请求访问该URL从而获取该资源。根据当今大多数API的定义,你很有可能会得到一份JSON格式的数据响应,整个过程大概是这样:

GET /user/1
{
    "username":"姓名",
    "age":20,
    "sex":"男"
}
GET /book/1
{
    "book":"书名",
    "author":"作者",
    "country":"中国"
}

从上面的示例可以看出,如果前端需要user/1book/1的时候需要调用2次接口,并且如果前端只需要user/1里面的username,而上面的接口获取了username以外的数据,那么对于前端而言,除 username 之外的数据无处可用,造成了资源的浪费。

立即学习PHP免费学习笔记(深入)”;

如果我们使用GraphQL来进行查询的话,与REST方式相比,只需要调用一次并且可以查询我们指定的字段,避免了资源的浪费,并且更加高效。

query {
 user(id:1) {
     username
 }
 book(id:1){
     book,
     author,
     country
 }
}

推荐学习:《PHP视频教程

安装graphql-php包

composer require webonyx/graphql-php

开始

1、安装完成之后,我们先编写一个简单示例,来看看graphql-php怎么用,具体代码如下:这段代码中,我们定义了一个名为phoneNumber的字段,然后通过postman来调用我们编写的代码。

<?php
require_once __DIR__ . '/vendor/autoload.php';
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;

$queryType = new ObjectType([
    'name' => 'Query',
    'fields' => [
        'phoneNumber' => [
            'type' => Type::int(),
            'resolve' => function () {
                return 1875555555;
            }
        ]
    ],
]);

$schema = new Schema([
    'query' => $queryType,
]);



$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);

$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;

try {
    $rootValue = ['prefix' => 'prefix: '];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {

    $output = [
        'errors' => [
            [
                'message' => $e->getMessage()
            ]
        ]
    ];
}
header('Content-Type: application/json');
echo json_encode($output);

2、使用postman来调用我们刚刚编写的代码,以下是我们查询结果的示例

1.png

介绍

从上面的示例中,我们可以看到示例主要引入了4个类

use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;

Schema 类

Schema 是类型层次结构的容器,它接受构造函数中的根类型并向内部 GrahpQL 工具提供接收你的类型信息的方法。

配置选项

包含以下选项的数组:

Option Type Notes
query ObjectType 必须。 读取 API 中包含根级字段的对象类型 (通常命名为 "Query"),用于读取数据
mutation ObjectType 写入 API 中包含根级字段的对象类型  (通常命名为 "Mutation"),数据变更时会用到
subscription ObjectType 保留用于将来的描述实现。目前表现为 graphql-js 自检查询的兼容,用于各种客户端 (如 Relay 或 GraphiQL)
directives Directive[] 默认包含内建指令 @skip@include

如果你传递自定义指令并且依然想使用内建指令,请声明添加它们。例如:

array_merge(GraphQL::getStandardDirectives(), [$myCustomDirective]);
types ObjectType[] 对象类型类表,它在静态 schema 解析期间是不能被 graphql-php 发现的。

大多数情况下,对象类型未曾在字段中被直接引用,但它依然是 schema 的一部分时会用到,因为它实现了一个在 resolveType 中调用解析为此对象类型的接口。

请注意,您在此处无需传递所有类型 ,它只是具体用例的解决方法。
typeLoader callable function($name) 返回给定的类型实例名称。 多次调用情况下,必须返回同样的实例。 查阅下文延迟类型加载部分。

ObjectType类

GraphQL\Type\Definition\ObjectType

对象类型是典型的 GraphQL 应用程序中使用最频繁的基元。

配置选项

Option Type Notes
name string 必须。 Schema 中此对象的唯一名称
fields array or callable 必须。 描述对象字段或可调用返回此类数组的数组。
description string 呈现于客户端的参数文本说明(例如:用于 GraphiQL 自动生成文档 )
interfaces array or callable 此类型实现的接口列表或返回此类列表的可调用接口。

内置标量类型

<?php
use GraphQL\Type\Definition\Type;
// 内置标量类型
Type::string();  // String 类型
Type::int();     // Int 类型
Type::float();   // Float 类型
Type::boolean(); // Boolean 类型
Type::id();      // ID 类型

字段参数

GraphQL 对象类型上的所有字段都有 0 个或多个参数,使用在 args 的字段定义上。每个参数数组参考以下说明:

Option Type Notes
name string 必须。 参数名称。 为空时,使用 args 数组键值
type Type 必须。
description string 呈现于客户端的参数文本说明
defaultValue scalar 当前参数默认值

示例

$queryType = new ObjectType([
    'name' => 'Query',
    'fields' => [
        'phoneNumber' => [
            'type' => Type::int(),
            'resolve' => function () {
                return 1875555555;
            }
        ]
    ],
]);

GraphQL 类

GraphQL类主要在查询的时候用到,我们可以用 GraphQL::executeQuery 方法来执行查询

Krea AI
Krea AI

多功能的一站式AI图像生成和编辑平台

下载

executeQuery 方法的参数说明

参数 类型 说明
schema GraphQL\Type\Schema 必须。  Schema应用实例
queryString string or GraphQL\Language\AST\DocumentNode 必须。 解析,验证并执行现有的 GraphQL 查询字符。 如果在执行之前解析其他查询,则在此处传递相应的 AST 文档节点来避免新的解析。
rootValue mixed 表示数据图结构的基础值。作为Query type 字段解析传递的第一个参数。如果现有该值已被 Query type 解析过,则可忽略或设置为 null 值。
context mixed 字段解析器的共享信息。 常用来传递已登录用户信息,位置详情等。

它将用在所有字段解析器的第 3 个参数。
variableValues array 变量的映射,该值将随同查询字符串一起传递。请查阅 GraphQL官网查询变量的相关
operationName string 指定请求方可执行的操作, 防止条件查询字符包含多级操作。
fieldResolver callable Schema 参数 schema 中未实现的解析器函数。
validationRules array 查询验证规则组,默认所有规则。空数组将跳过查询验证 (对于持久化查询将会比较方便,查询会在持久化之前默认已验证,并在执行期间假设符合规则)。
use GraphQL\GraphQL;

$result = GraphQL::executeQuery(
    $schema, 
    $queryString, 
    $rootValue = null, 
    $context = null, 
    $variableValues = null, 
    $operationName = null,
    $fieldResolver = null,
    $validationRules = null
);

简单示例

我们介绍完GraphQL几个概念之后,用几个简单的示例带大家来体验一下。

普通示例

在这个示例中我们定义了2个字段,分别是phoneNumberecho,其中phoneNumber为 Type::int()类型,echoType::string()类型,同时echo字段带有一个参数为message

<?php

require_once __DIR__ . '/vendor/autoload.php';

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

$queryType = new ObjectType([
    'name' => 'Query',
    'fields' => [
        'phoneNumber' => [
            'type' => Type::int(),
            'resolve' => function () {
                return 1875555555;
            }
        ],

        'echo' => [
            'type' => Type::string(),
            'args' => [
                'message' => Type::string(),
            ],
            'resolve' => function ($root, $args) {
                return 'echo msg result:' . ($args['message'] ?? 'nothing');
            }
        ],
    ],
]);

$schema = new Schema([
    'query' => $queryType
]);


$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);

$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;

try {
    $rootValue = ['prefix' => 'prefix: '];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {

    $output = [
        'errors' => [
            [
                'message' => $e->getMessage()
            ]
        ]
    ];
}
header('Content-Type: application/json');
echo json_encode($output);

执行示例代码结果

2.png

我们可以看到,在请求时我们传了phoneNumberecho两个字段,并且messagetest

对象示例

我们在上面说过,对象类型是典型的 GraphQL 应用程序中使用最频繁的基元,一个对象类型里面可以包含宁外一个对象类型,我们可以新定义一个名为$userTypeObjectType,然后在oneUser指定它的类型为$userType,这样我们执行查询的时候,oneUser就会返回一个对象。

<?php
require_once __DIR__ . '/vendor/autoload.php';

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

$userType = new ObjectType([
    'name' => 'userType',
    'description' => '用户详情',
    'fields' => [
        'uid' => [
            'type' => Type::int(),
            'description' => '用户ID'
        ],
        'name' => Type::string()
    ]
]);


$queryType = new ObjectType([
    'name' => 'Query',
    'fields' => [
        'oneUser' => [
            'type' => $userType, // 我们这里指定type为我们上面创建的$userType
            'description' => '用户列表',
            'args' => [
                'uid' => [
                    'type' => Type::int(),
                    'defaultValue' => 222
                ]
            ],
            'resolve' => function($root, $args) {
                return  [
                    "uid" => $args['user_id'] ?? 3,
                    "name" => "xzl",
                ];
            }
        ],
    ]
]);

$schema = new Schema([
    'query' => $queryType
]);

$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;

try {
    $rootValue = ['prefix' => 'prefix: '];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {
    $output = [
        'errors' => [
            [
                'message' => $e->getMessage()
            ]
        ]
    ];
}
header('Content-Type: application/json');
echo json_encode($output);

执行示例代码结果

3.png

列表示例

在平时的开发请求中,我们从后端接口获取数据的时候,大部分都是以列表的形式返回的,我们可以通过Type::listOf方法来指定我们返回的字段是一个列表。

<?php
require_once __DIR__ . '/vendor/autoload.php';

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\GraphQL;
use GraphQL\Type\Schema;

class User
{
    // 模拟从数据库取数据
    public static function getUserLimit($limit)
    {
        $user  = [
            [
                "uid" => 1,
                "name" => "name1"
            ],
            [
                "uid" => 2,
                "name" => "name2"
            ],
            [
                "uid" => 3,
                "name" => "name3"
            ],
            [
                "uid" => 4,
                "name" => "name4"
            ]
        ];
        return array_slice($user, 0, $limit);
    }
}


$userType = new ObjectType([
    'name' => 'userType',
    'description' => '用户详情',
    'fields' => [
        'uid' => [
            'type' => Type::int(),
            'description' => '用户ID'
        ],
        'name' => Type::string()
    ]
]);


$queryType = new ObjectType([
    'name' => 'Query',
    'fields' => [
        'users' => [
            'type' => Type::listOf($userType),
            'description' => '用户列表',
            'args' => [
                'limit' => [
                    'type' => Type::int(),
                    'description' => '限制条数',
                    'defaultValue' => 10
                ]
            ],
            'resolve' => function($root, $args) {
                return User::getUserLimit($args['limit']);
            }
        ]
    ]
]);



$schema = new Schema([
    'query' => $queryType
]);


$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;

try {
    $rootValue = ['prefix' => 'prefix: '];
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {
    $output = [
        'errors' => [
            [
                'message' => $e->getMessage()
            ]
        ]
    ];
}
header('Content-Type: application/json');
echo json_encode($output);

执行示例代码结果

4.png

从上面结果可以看到,我们传了limit参数为2,最终从我们模拟的数据里面取出了2条数据

使用类型语言

在上面的示例中,如果我们代码返回的数据比较复杂时,需要编写大量的代码,通过GraphQL类型语言,我们可以减少代码量,使代码看上去更加简洁,这是一个用 GraphQL 类型语言定义的简单 Schema示例。

<?php

require_once __DIR__ . '/vendor/autoload.php';
use GraphQL\GraphQL;
use GraphQL\Utils\BuildSchema;
// graph.graphql  文件内容
$graph =
<<<GRAPH
schema {
  query: Query
}

type Query {
  graph_test: String
  echo(message: String): String
  show_test: Show
  show_test_arr: [Show]
}


type Show {
    content: String!
    text: String!
}
GRAPH;


$schema = BuildSchema::build($graph);
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;

try {
    $rootValue = [
        'sum' => function($rootValue, $args, $context) {
            return $args['x'] + $args['y'];
        },
        'echo' => function($rootValue, $args, $context) {
            return $rootValue['prefix'] . ($args['message'] ?? 'no echo');
        },
        'show_test' => function($rootValue, $args, $context) {
            return [
                'content' => 'show_content',
                'text' => 'xxxx xxx'
            ];
        },
        'show_test_arr' => function($rootValue, $args, $context) {
            return [
                [
                    'content' => 'show_content',
                    'text' => 'xxxx xxx'
                ],
                [
                    'content' => 'show_content_2',
                    'text' => 'xxxx xxx_2'
                ]

            ];
        },
        'prefix' => 'from test:',
        "graph_test" => "graphql_test"
    ];;
    $result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
    $output = $result->toArray();
} catch (\Exception $e) {

    \GraphQL\Server\StandardServer::send500Error($e);
}
header('Content-Type: application/json');
echo json_encode($output);

执行示例代码结果

5.png

参考

graphql.cn/learn/

learnku.com/docs/graphq…

更多编程相关知识,请访问:编程视频!!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Python GraphQL API 开发实战
Python GraphQL API 开发实战

本专题系统讲解 Python 在 GraphQL API 开发中的实际应用,涵盖 GraphQL 基础概念、Schema 设计、Query 与 Mutation 实现、权限控制、分页与性能优化,以及与现有 REST 服务和数据库的整合方式。通过完整示例,帮助学习者掌握 使用 Python 构建高扩展性、前后端协作友好的 GraphQL 接口服务,适用于中大型应用与复杂数据查询场景。

23

2026.01.21

json数据格式
json数据格式

JSON是一种轻量级的数据交换格式。本专题为大家带来json数据格式相关文章,帮助大家解决问题。

456

2023.08.07

json是什么
json是什么

JSON是一种轻量级的数据交换格式,具有简洁、易读、跨平台和语言的特点,JSON数据是通过键值对的方式进行组织,其中键是字符串,值可以是字符串、数值、布尔值、数组、对象或者null,在Web开发、数据交换和配置文件等方面得到广泛应用。本专题为大家提供json相关的文章、下载、课程内容,供大家免费下载体验。

546

2023.08.23

jquery怎么操作json
jquery怎么操作json

操作的方法有:1、“$.parseJSON(jsonString)”2、“$.getJSON(url, data, success)”;3、“$.each(obj, callback)”;4、“$.ajax()”。更多jquery怎么操作json的详细内容,可以访问本专题下面的文章。

335

2023.10.13

go语言处理json数据方法
go语言处理json数据方法

本专题整合了go语言中处理json数据方法,阅读专题下面的文章了解更多详细内容。

82

2025.09.10

软件测试常用工具
软件测试常用工具

软件测试常用工具有Selenium、JUnit、Appium、JMeter、LoadRunner、Postman、TestNG、LoadUI、SoapUI、Cucumber和Robot Framework等等。测试人员可以根据具体的测试需求和技术栈选择适合的工具,提高测试效率和准确性 。

463

2023.10.13

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

1010

2023.08.02

c语言中null和NULL的区别
c语言中null和NULL的区别

c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

254

2023.09.22

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 13.4万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.3万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号