using parameter(s) within a field within a type

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

Dear @Stefan_van_Roosmalen, in further requests, if possible, could you please send us the complete test project you are using? This way we can right away compile, run, and see the issue. And eventually, fix that for you. Otherwise, we will have to take the effort to manually build all the same project you have there to reproduce the problem.

Thus, to save time, we didn't do it yet, so my guess is that because you created Orders as an indexed property. Try to simply declare it as a method and it should work.

Ahhh... No problem!
Sorry for the inconvenience.
I will upload the project code a.s.a.p.

Attached you will find the project code.
Thank you in advance for your help!
testproject.zip (94.6 KB)

Thank you for the project. Here is the updated project, working.

testproject_updated.zip (10.3 KB)

Basically, as I mentioned, the orders GraphQL field should be implemented as a method in the class:

  TCustomer = class
  private
    FID: Integer;
    FName: String;
    FOrders: TArray<TOrder>;
  public
    function Orders(OrderDate: string): TArray<TOrder>;
    property ID: Integer            read FID;
    property Name: String           read FName;
  end;

And then you should bind the Customer GraphQL object to the TCustomer Delphi class:

procedure TWebModule1.GraphQLSchema1InitSchema(Sender: TObject;
  Schema: TSchemaDocument);
begin
  Schema.Bind('Query', TQuery);
  Schema.Bind('Customer', TCustomer);
end;

That's pretty much it.

1 Like

It is working now. Thank you very much for your help!
Actually I was assuming I had to add a method for reading the property, but that was not the right solution I see now.
Again, thanks for your help!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.