Hi there,
I am trying to implement a GraphQL function which is using a parameter within a field within a type. See below example; I want to be able to query Customers with their Orders on a specific date:
type Customer {
id: ID!
name: String
orders(order_date: String): [Order]
}
type Order {
id: ID!
date: String
amount: Float
}
type Query {
customer: Customer
}
I have created the following unit to handle the queries, but at this point there is no place where the parameter ("order_date") is being used. How can I achieve this?
unit GraphQL.ParamTest;
interface
uses
Generics.Collections;
type
TOrder = class
private
FID: Integer;
FOrderDate: String;
FAmount: Double;
public
property ID: Integer read FID;
property OrderDate: String read FOrderDate;
property Amount: Double read FAmount;
end;
TCustomer = class
private
FID: Integer;
FName: String;
FOrders: TArray<TOrder>;
function GetOrder(OrderDate: String): TArray<TOrder>;
public
property ID: Integer read FID;
property Name: String read FName;
property Orders[OrderDate: String]: TArray<TOrder> read GetOrder;
end;
TQuery = class
public
function Customer: TCustomer;
end;
implementation
var
OrderList: TObjectList<TOrder>;
{ TQuery }
function TQuery.Customer: TCustomer;
var
LOrder: TOrder;
LCustomer: TCustomer;
begin
// Replace this part with a database query to read all orders with a specific order date...
LOrder := TOrder.Create;
LOrder.FID := 5001;
LOrder.FOrderDate := '2023-07-20';
LOrder.FAmount := 190.50;
OrderList.Clear;
OrderList.Add(LOrder);
LCustomer := TCustomer.Create;
LCustomer.FID := 1001;
LCustomer.FName := 'Jansen International B.V.';
LCustomer.FOrders := OrderList.ToArray;
Result := LCustomer;
end;
{ TCustomer }
function TCustomer.GetOrder(OrderDate: String): TArray<TOrder>;
begin
//
end;
initialization
OrderList := TObjectList<TOrder>.Create(True);
finalization
OrderList.Free;
end.
When running the following GraphQL query, I get an error message ("ERttiFieldResolverException with message 'Could not find field TCustomer.orders'."):
query {
customer {
id
name
orders(order_date: "2023-07-20") {
id
date
amount
}
}
}
How can I fix my code?
Thank you in advance for your help.
Kind Regards,
Stefan