简体   繁体   English

如何在php中构造graphql突变查询请求

[英]How to construct a graphql mutation query request in php

I am having the hardest time figuring out how to properly format a graphql api mutation POST request in php.我最难弄清楚如何在 php 中正确格式化 graphql api 突变 POST 请求。

If I hard code the string and use it as the data in my POST request it works like this: '{"query":"mutation{addPlay(input: {title: \"two\"}){ properties { title } } }"}'如果我对字符串进行硬编码并将其用作 POST 请求中的数据,它的工作方式如下: '{"query":"mutation{addPlay(input: {title: \"two\"}){ properties { title } } }"}'

But if I have a php array of the input values:但是,如果我有一个输入值的 php 数组:

$test_data = array(
  'title' => 'two'
);

I can't seem to format it correctly.我似乎无法正确格式化它。 json_encode also puts double quotes around the keys which graphql is rejecting with the error Syntax Error GraphQL request (1:26) Expected Name, found String . json_encode 还在 graphql 拒绝的键周围加上双引号,错误是Syntax Error GraphQL request (1:26) Expected Name, found String

I ultimately need a solution that will convert a larger more complex array to something usable.我最终需要一个解决方案,将更大更复杂的数组转换为可用的东西。

Reformatting the query allowed me to use JSON directly.重新格式化查询允许我直接使用 JSON。

So my new query looks like this:所以我的新查询如下所示:

$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);

Then the $request_data_json is used in a POST http request.然后$request_data_json在 POST http 请求中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM