Delphi 与 Graph.cool API?
Delphi with Graph.cool API?
我如何使用 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?
我想建立一个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;