如何使用PHP发布到GraphQL
P粉916760429
P粉916760429 2024-01-10 17:42:38
0
1
419

我向 GraphQL 发出以下卷曲请求。它工作得很好,但在生产中 shell_exex 是不允许的。如何用有效的 PHP 重写这篇curl 帖子?

 $curl_string = 'curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer "' . AIRTABLE_API_KEY;
  $curl_second_string = ' -d \'{"query": "{fullCapaReview (id: \"' . $id . '\") {proposedRuleName submissionDate agencyContactName statusLawDept}}"}\' https://api.baseql.com/airtable/graphql/appXXXzzzzzzzzzz';
  $curl_complete_string = "$curl_string $curl_second_string";
  $result = shell_exec($curl_complete_string); 

编辑:抱歉,我输入了错误的查询。我想到的查询是:

' -d \'{"query": "{dMsAggency (agencyAcronym: \"' . $_agency . '\") {agencyAcronym fullCapaReview { id }}}"}\'

我打了两个类似的电话。我会将原件留在那里,因为有人据此回答。

这是我到目前为止所拥有的:

$curl = curl_init($url);
$query = 'query dMsAgencies($agencyAcronym: String) {agencyAcronym fullCapaReview { id }} ';
$variables = ["agencyAcronym" => $id ];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['query' => $query, 'variables' => $variables])); 
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Authorization: Bearer ' . AIRTABLE_API_KEY]);
$response = curl_exec($curl);
curl_close($curl);


console_log("Response : " . $response);

这是我收到的错误消息。我只是想看看我的语法是否符合要求。

Response : {"errors":[{"message":"Cannot query field \"agencyAcronym\" on type \"Query\".","locations":[{"line":1,"column":44}],"stack":["GraphQLError: Cannot query field \"agencyAcronym\" on type \"Query\"."," at Object.Field (/var/app/current/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:46:31)"," at Object.enter (/var/app/current/node_modules/graphql/language/visitor.js:323:29)"," at Object.enter (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:370:25)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]},{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}],"stack":["GraphQLError: Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\"."," at Object.leave (/var/app/current/node_modules/graphql/validation/rules/NoUnusedVariablesRule.js:38:33)"," at Object.leave (/var/app/current/node_modules/graphql/language/visitor.js:344:29)"," at Object.leave (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:390:21)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]}]}

message":"无法在类型\"Query\" 上查询字段\"agencyAcronym\"

{"message":"变量 \"$agencyAcronym\" 从未在操作 \"dMsAggency\" 中使用。","locations":[{"line":1,"column":19}]

P粉916760429
P粉916760429

全部回复(1)
P粉805535434

查询并不相同,但假设您知道,您的 PHP 示例也存在语法问题。

query dMsAgencies($agencyAcronym: String) {
    agencyAcronym 
    fullCapaReview { 
        id 
    }
}

如果将此与 docs 中的示例(使用变量时)进行比较,您会发现可以看到您当前没有在任何地方使用 $agencyAcronym 变量(并且您的架构中可能没有名为 agencyAcronym 的查询)。这是一个示例(使用第一个代码片段中的查询):

query dMsAgencies($agencyAcronym: String) {
    fullCapaReview (id: $agencyAcronym) {
        proposedRuleName 
        submissionDate
        agencyContactName
        statusLawDept
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板