2

我将如何使用 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 交互?

4

1 回答 1

6

简单的方法是直接使用 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;
于 2017-10-19T00:50:26.427 回答