PHP GraphQL入门 安装php GraphQL 要开始使用PHP GraphQL,您需要先安装它。您可以使用Composer来安装PHP GraphQL: composer require graphql/graphql 创建Gr
要开始使用PHP GraphQL,您需要先安装它。您可以使用Composer来安装PHP GraphQL:
composer require graphql/graphql
GraphQL模式定义了您的api中可以查询的数据和操作。要创建GraphQL模式,可以使用PHP GraphQL中的SchemaBuilder类:
use GraphQLTypeDefinitionType;
use GraphQLTypeSchema;
$schema = new Schema([
"query" => new ObjectType([
"name" => "Query",
"fields" => [
"hello" => [
"type" => Type::string(),
"description" => "Says hello",
"resolve" => function () {
return "Hello, world!";
}
],
],
]),
]);
要创建GraphQL服务器,可以使用PHP GraphQL中的GraphQLServer类:
use GraphQLServerServer;
$server = new Server($schema);
$server->listen();
要使用GraphQL客户端查询API,可以使用PHP GraphQL中的GraphQLClient类:
use GraphQLClient;
$client = new Client("Http://localhost:8080/graphql");
$query = "query { hello }";
$result = $client->query($query);
print_r($result->getData());
输出:
Array
(
[hello] => Hello, world!
)
GraphQL Mutations允许您对数据进行修改,例如创建、更新或删除。要使用GraphQL Mutations,可以使用PHP GraphQL中的MutationBuilder类:
use GraphQLTypeDefinitionType;
use GraphQLTypeSchema;
$schema = new Schema([
"mutation" => new ObjectType([
"name" => "Mutation",
"fields" => [
"createPost" => [
"type" => Type::string(),
"description" => "Creates a new post",
"args" => [
"title" => Type::nonNull(Type::string()),
"content" => Type::nonNull(Type::string()),
],
"resolve" => function ($rootValue, $args) {
--结束END--
本文标题: 探索PHP GraphQL的无限可能:从入门到精通
本文链接: https://www.lsjlt.com/news/560411.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0