如何从PHP发布到GraphQl

发布于 2025-02-11 18:27:02 字数 3114 浏览 2 评论 0 原文

我对GraphQl有以下卷曲请求。它效果很好,但是在生产 shell_exex 中不允许使用。如何在有效的PHP中重写此卷曲帖子?

 $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(agentagen normenty:\”。

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

这就是我到目前为止的:

$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)"]}]}

消息“:” type \“ query \” QUERY field \“ agentionalagon

\” \“。”,“位置”:[{“ line”:1,“列”:19}]

I have the following curl request to GraphQL. It works great, but in production shell_exex is not allowed. How do I re-write this curl post in valid PHP?

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

edit: I'm sorry, I put the wrong query. The query I had in mind was:

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

I make two similar calls. I will leave the original there because someone answered based on that.

This is what I have so far:

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

This is the error message I am getting. I just want to see if I am in the ballpark with my syntax.

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}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2025-02-18 18:27:02

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

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

如果将其与 $ agentryamonymonymanty action>变量(并且可能没有询问 agentionalnonymonymy 在您的架构中)。这是一个示例(使用第一个片段中的查询):

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

The queries are not the same, but assuming that you are aware, your PHP example also has a syntax issue.

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

If you compare this with the example in the docs (when using variables) you can see that you are currently not using the $agencyAcronym variable anywhere (and there probably isn't a query named agencyAcronym in your schema). Here is one example (using the query from your first snippet):

query dMsAgencies($agencyAcronym: String) {
    fullCapaReview (id: $agencyAcronym) {
        proposedRuleName 
        submissionDate
        agencyContactName
        statusLawDept
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文