2

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?

marktani
  • 7,578
  • 6
  • 37
  • 60
buttercup
  • 1,096
  • 16
  • 37

1 Answers1

6

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;
EugeneK
  • 2,164
  • 1
  • 16
  • 21
  • How would you use this repo - https://github.com/paolo-rossi/delphi-jose-jwt to get JWT claims and authenticate with Graph.cool? – buttercup Oct 19 '17 at 01:02
  • Example, if it's sign-in: Memo1.Lines.Add(SendHttp('{"query": "mutation ' + ' { signinUser( '+ ' email: { email: \"hello@hello.com\", password: \"hello\" }) { token }}" }')); – buttercup Oct 19 '17 at 08:47
  • I did some more work. If you get a JWT Auth token, you can either a) use Delphi-JWT to decode the token, or pass back the JWT Auth token as-is back to website. It'll work. – buttercup Oct 20 '17 at 05:44