Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit GraphQL.Sample;
- interface
- uses
- GraphQL.WebBroker, GraphQL.Http.Handler, GraphQL.Http.Interfaces,
- GraphQL.Schema, GraphQL.Comp.Schema;
- type
- TGraphQL = class
- private
- fHttp: TGraphQLHttpHandler;
- fSchema: TSchemaDocument;
- public
- constructor Create;
- function SendReq(aReq: ansistring): string;
- end;
- TRequest = class(TInterfacedObject, IGraphQLHttpRequest)
- private
- fMethod: string;
- fURL: string;
- fContent: ansistring;
- public
- function GetMethod: string;
- function GetHeader(const Name: string): string;
- function GetContent: TArray<Byte>;
- function GetUrl: string;
- end;
- TResponse = class(TInterfacedObject, IGraphQLHttpResponse)
- private
- fStatus: integer;
- fContent: ansistring;
- public
- procedure SetStatusCode(const Value: Integer);
- procedure SetHeader(const Name: string; const Value: string);
- procedure Close(const Value: TArray<Byte>);
- property Content: ansistring read fContent;
- end;
- type
- TTest = class
- private
- fName: string;
- fId: integer;
- public
- constructor Create;
- property Id: integer read fId;
- property Name: string read fName;
- end;
- TQuery = class
- function Test: TTest;
- end;
- implementation
- uses
- GraphQL.Helpers,
- SysUtils;
- const
- AppSchema =
- 'type Test { ' + sLineBreak +
- ' id: ID! ' + sLineBreak +
- ' name: String! ' + sLineBreak +
- '} ' + sLineBreak +
- ' ' + sLineBreak +
- 'type Query { ' + sLineBreak +
- ' test: Test ' + sLineBreak +
- '} ';
- { TGraphQL }
- constructor TGraphQL.Create;
- begin
- fSchema := TSchemaDocument.Parse(AppSchema);
- fSchema.Init;
- fSchema.Bind('Query', TQuery);
- end;
- function TGraphQL.SendReq(aReq: ansistring): string;
- begin
- aReq := 'query GetTest { ' + #13#10 +
- ' test { ' + #13#10 +
- ' name ' + #13#10 +
- ' } ' + #13#10 +
- '} ';
- var req := TRequest.Create;
- var resp := TResponse.Create;
- req.fMethod := 'POST';
- req.fUrl := 'http://test.com/test';
- req.fContent := aReq;
- fHttp := TGraphQLHttpHandler.Create(req, resp, fSchema);
- fHttp.ProcessRequest;
- result := resp.Content;
- fHttp.Free;
- end;
- { TRequest }
- function TRequest.GetContent: TArray<Byte>;
- begin
- SetLength(result, length(fContent));
- move(fContent[1], result[0], length(result));
- end;
- function TRequest.GetHeader(const Name: string): string;
- begin
- if SameText(name, 'Content-Type') then
- result := 'application/graphql'
- else
- result := '';
- end;
- function TRequest.GetMethod: string;
- begin
- result := fMethod;
- end;
- function TRequest.GetUrl: string;
- begin
- result := fURL;
- end;
- { TResponse }
- procedure TResponse.Close(const Value: TArray<Byte>);
- begin
- SetLength(fContent, length(value));
- move(value[0], fContent[1], length(value));
- end;
- procedure TResponse.SetHeader(const Name, Value: string);
- begin
- end;
- procedure TResponse.SetStatusCode(const Value: Integer);
- begin
- fStatus := value;
- end;
- { TQuery }
- function TQuery.Test: TTest;
- begin
- result := TTest.Create;
- end;
- { TTest }
- constructor TTest.Create;
- begin
- fId := 1;
- fName := 'Hello World';
- end;
- end.
Add Comment
Please, Sign In to add comment