学习 GraphQL 时候遇到的极小极小的问题

在学习 GraphQL 的时候,使用 GraphIQL Feen 进行接口数据请求。

按照 php 版 GraphQL 的实现之一Getting Started 步骤进行学习的时候,无论如何都会报错。无法正常获取到服务器端定义的接口数据。

原因如下:
graphiql feen 默认会传入 variableValues。按照手册中的代码
$variableValues = isset($input['variables']) ? $input['variables'] : null;
获取 $variableValues 会出现 php 服务器端代码报如下错误。

PostMan 进行如下设置
请求方式 POST

Headers: 添加 Content-Type:application/json
file

Body: 修改为 raw 请求方式
file

代码中打印传入 dd($variableValues);
可看到数据不为空。但传入的不是需要的数据
file

file

将会获得如下报错:
file

解决办法:多种多样 我选择将无法处理的设置为 null

$variableValues = empty($input['variables']) ? null : !in_array($input['variables'], ['{', '{}']) ? $input['variables'] : null;

代码就可以正常运行了:
file

Perfect, congratulations to you. :)

本作品采用《CC 协议》,转载必须注明作者和本文链接
Study hard and make progress every day. Study hard and make progress every day.
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

这是我看到第4个GraphQL文章

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!