sag*_*arl 2 php apollo graphql apollo-server
我最难弄清楚如何在php中正确格式化graphql api突变POST请求。
如果我对字符串进行硬编码并将其用作 POST 请求中的数据,它的工作方式如下:
'{"query":"mutation{addPlay(input: {title: \"two\"}){ properties { title } } }"}'
但是如果我有一个输入值的 php 数组:
$test_data = array(
'title' => 'two'
);
Run Code Online (Sandbox Code Playgroud)
我似乎无法正确格式化。json_encode 还在 graphql 因错误而拒绝的键周围加上双引号Syntax Error GraphQL request (1:26) Expected Name, found String。
我最终需要一个解决方案,将更大更复杂的数组转换为可用的东西。
重新格式化查询允许我直接使用 JSON。
所以我的新查询如下所示:
$test_data = array(
'title' => 'two'
);
$request_data = array(
'query' => 'mutation ($input: PlayInput) { addPlay(input: $input) { properties { title } }}',
'variables' => array(
'input' => $test_data,
),
);
$request_data_json = json_encode($request_data);
Run Code Online (Sandbox Code Playgroud)
然后$request_data_json在 POST http 请求中使用。
| 归档时间: |
|
| 查看次数: |
3775 次 |
| 最近记录: |