【问题标题】:How to construct a graphql mutation query request in php如何在php中构造graphql突变查询请求
【发布时间】:2017-11-07 23:02:23
【问题描述】:

我很难弄清楚如何在 php 中正确格式化 graphql api 变异 POST 请求。

如果我对字符串进行硬编码并将其用作 POST 请求中的数据,它的工作方式如下: '{"query":"mutation{addPlay(input: {title: \"two\"}){ properties { title } } }"}'

但如果我有一个输入值的 php 数组:

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

我似乎无法正确格式化它。 json_encode 还在 graphql 拒绝的键周围加上双引号,错误为Syntax Error GraphQL request (1:26) Expected Name, found String

我最终需要一个解决方案,将更大更复杂的数组转换为可用的东西。

【问题讨论】:

    标签: php graphql apollo apollo-server


    【解决方案1】:

    重新格式化查询允许我直接使用 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);
    

    然后$request_data_json 用于 POST http 请求。

    【讨论】:

    • 感谢@sagannotcarl,我为此苦苦挣扎了 2 天。
    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2019-11-14
    • 2017-08-24
    • 2019-05-24
    • 2019-02-05
    • 2022-06-23
    • 2022-11-25
    • 2022-08-13
    相关资源
    最近更新 更多