我对GraphQL有以下curl请求。它工作得很好,但在生产中不允许使用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": "{dMsAgencies (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":"Cannot query field "agencyAcronym" on type "Query"
{"message":"Variable "$agencyAcronym" is never used in operation "dMsAgencies".","locations":[{"line":1,"column":19}]
查询是不一样的,但假设你知道,PHP的例子也有语法问题。
query dMsAgencies($agencyAcronym: String) {
agencyAcronym
fullCapaReview {
id
}
}
如果您将此与文档中的示例(当使用变量时)进行比较,您可以看到您目前没有在任何地方使用$agencyAcronym
变量(并且在您的模式中可能没有名为agencyAcronym
的查询)。这里是一个例子(从你的第一个片段使用查询):
query dMsAgencies($agencyAcronym: String) {
fullCapaReview (id: $agencyAcronym) {
proposedRuleName
submissionDate
agencyContactName
statusLawDept
}
}