How would I interface Graph.cool using Delphi?
I want to build a Win32/Win64/OSX client
https://Graph.cool/ has a GraphQL (REST) API end-point.
Example: https://api.graph.cool/simple/v1/ENDPOINT
A known example of GraphQL implementation with FaceBook GraphQL is at: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/REST_Client_Library
I have an example query:
mutation {
createUser(
authProvider: {
email: { email: "hello@hello.com", password: "hello"
}}
)
{
id
}
}
to create a user account.
I tried using TRestClient, but there seems to be no way to put an unstructured string query.
Snipplet from 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;
I got: a) Bad request, b) Invalid Json query.
Any ideas how I would interface to Graph.cool API?
Simple way will be to use HttpClient directly, like
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;