首頁  >  問答  >  主體

如何使用PHP發佈到GraphQL

我向 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粉916760429P粉916760429306 天前399

全部回覆(1)我來回復

  • P粉805535434

    P粉8055354342024-01-11 11:00:12

    查詢並不相同,但假設您知道,您的 PHP 範例也存在語法問題。

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

    如果將此與docs 中的範例(使用變數時)進行比較,您會發現可以看到您目前沒有在任何地方使用$agencyAcronym 變數(並且您的架構中可能沒有名為agencyAcronym 的查詢)。這是一個範例(使用第一個程式碼片段中的查詢):

    query dMsAgencies($agencyAcronym: String) {
        fullCapaReview (id: $agencyAcronym) {
            proposedRuleName 
            submissionDate
            agencyContactName
            statusLawDept
        }
    }

    回覆
    0
  • 取消回覆