2017-10-18 132 views
1

如何將接口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?

回答

2

簡單的辦法將是直接使用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; 
+0

你將如何使用這個回購 - https://github.com/paolo-rossi/delphi-jose-jwt獲得智威湯遜和索賠使用Graph.cool進行身份驗證? – buttercup

+0

例如,如果它登錄: Memo1.Lines.Add(SendHttp('{「query」:「mutation'+ '{signinUser('+ 'email:{email:\」[email protected] \ 「,密碼:\」hello \「}){token}}」}')); – buttercup

+0

我做了一些更多的工作。如果你得到一個JWT身份驗證令牌,您可以一)用Delphi-JWT令牌進行解碼,或傳回的JWT身份驗證令牌原樣回網站。它會工作。 – buttercup