德尔福与Graph.cool API?
问题描述:
如何将接口Graph.cool用Delphi?德尔福与Graph.cool API?
我想建立一个Win32/Win64的/ OSX客户
https://Graph.cool/有GraphQL(REST)API端点。
实施例: https://api.graph.cool/simple/v1/ENDPOINT
GraphQL执行与Facebook GraphQL的已知的例子是在: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/REST_Client_Library
我的示例性查询:
mutation {
createUser(
authProvider: {
email: { email: "[email protected]", password: "hello"
}}
)
{
id
}
}
来创建用户帐户。
我试着用TRestClient,但似乎没有办法把一个非结构化的查询字符串。
Snipplet从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: \"[email protected]\", password: \"hello\" } }) { id } }" }';
ContentType := ctAPPLICATION_JAVASCRIPT;
end;
with RESTRequest1.Params.Add do begin
name := ' id ';
Options := [poAutoCreated];
end;
RESTRequest1.Response := RESTResponse1;
RESTRequest1.SynchronizedEvents := False;
我有:1)错误的请求,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: \"[email protected]\", password: \"hello\"'+
' }}'+
' )' +
' {' +
' id' +
' }' +
'}" }'));
end;
你将如何使用这个回购 - https://github.com/paolo-rossi/delphi-jose-jwt获得智威汤逊和索赔使用Graph.cool进行身份验证? – buttercup
例如,如果它登录: Memo1.Lines.Add(SendHttp('{“query”:“mutation'+ '{signinUser('+ 'email:{email:\”[email protected] \ “,密码:\”hello \“}){token}}”}')); – buttercup
我做了一些更多的工作。如果你得到一个JWT身份验证令牌,您可以一)用Delphi-JWT令牌进行解码,或传回的JWT身份验证令牌原样回网站。它会工作。 – buttercup