gpt4 book ai didi

Delphi 带有 Graph.cool API?

转载 作者:行者123 更新时间:2023-12-01 19:35:18 27 4
gpt4 key购买 nike

如何使用 Delphi 连接 Graph.cool?

我想构建一个 Win32/Win64/OSX 客户端

https://Graph.cool/有一个 GraphQL (REST) API 端点。

示例: https://api.graph.cool/simple/v1/ENDPOINT

使用 FaceBook GraphQL 实现 GraphQL 的已知示例位于: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/REST_Client_Library

我有一个示例查询:

mutation {
createUser(
authProvider: {
email: { email: "hello@hello.com", password: "hello"
}}
)
{
id
}
}

创建用户帐户。

我尝试使用 TRestClient,但似乎无法进行非结构化字符串查询。

来自 DFM 的代码片段:

var
RESTRequest1: TRESTRequest;
RESTRequest1 := TRESTRequest.Create(Self);
RESTRequest1.Name := 'RESTRequest1';
RESTRequest1.AcceptEncoding := ' ';
RESTRequest1.Client := RESTClient1;
RESTRequest1.Method := rmPOST;
with RESTRequest1.Params.Add do begin
name := 'query';
Options := [poAutoCreated];
Value := '{ "query": "mutation { createUser(authProvider: { email: { email: \"hello@hello\", password: \"hello\" } }) { id } }" }';
ContentType := ctAPPLICATION_JAVASCRIPT;
end;
with RESTRequest1.Params.Add do begin
name := ' id ';
Options := [poAutoCreated];
end;
RESTRequest1.Response := RESTResponse1;
RESTRequest1.SynchronizedEvents := False;

我得到:a) 错误的请求,b) 无效的 Json 查询。

有什么想法可以连接 Graph.cool API 吗?

最佳答案

简单的方法是直接使用 HttpClient,例如

function SendHttp(const ARequest: string): string;
var
HttpClient: THttpClient;
Response: IHttpResponse;
ST: TStream;
begin
HttpClient := THttpClient.Create;
try
HttpClient.ContentType := CONTENTTYPE_APPLICATION_JSON;
HttpClient.Accept := CONTENTTYPE_APPLICATION_JSON;
ST := TStringStream.Create(ARequest);
try
Response := HttpClient.Post('https://api.graph.cool/simple/v1/ENDPOINT', ST, nil,
TNetHeaders.Create(
TNameValuePair.Create('Authorization', 'Bearer YOUR_AUTH_TOKEN')
));
Result := Response.ContentAsString();
finally
ST.Free;
end;
finally
HttpClient.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(SendHttp('{"query": "mutation {'+
' createUser('+
' authProvider: {' +
' email: { email: \"hello@hello.com\", password: \"hello\"'+
' }}'+
' )' +
' {' +
' id' +
' }' +
'}" }'));
end;

关于Delphi 带有 Graph.cool API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46820804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com