arguments for fields

Hello,

sorry, it’s me again ;-) But I couldn’t find an answer for this question:

How can I put an argument in a field like described in GraphQL

{
  user(id: 4) {
    id
    name
    profilePic(size: 100)
  }
}

It does not work with an object like this:

Tuser = class
private
  Fid: Integer;
  Fname: string;
public
  property id: Integer read Fid write Fid;
  property name: string read Fname write Fname;
  function profilePic(size: Integer): string;   // for simplicity the result type is string - could be some TprofilePic-Object
end;

GraphQL seems to request a property and not a function. But how could I put parameters / arguments to an property. It seems not to work with indexed properties either.

Regards
Harald

Actually that's how it's supposed to work. Yes, GraphQL schema fields can be mapped to properties and methods, especially because they can receive several parameters, not only one.

What is the exact error message you are receiving? Can you share your test project so we know better what's going on?

Hello Wagner,

I have the schema

type Profil {
  id: ID!
  email: String
  profilePic(size: Int): String
}

and the object

  [Entity]
  [Table('profil')]
  [Id('Fid', TIdGenerator.IdentityOrSequence)]
  TProfil = class
  private
    [Column('id', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    Fid: Integer;
    [Column('email', [], 64)]
    Femail: string;
  public
    property id: Integer read Fid;
    property email: string read Femail;
    function profilePic(size: Integer): string;
  end;
...
function TProfil.profilePic(size: Integer): string;
begin
  Result := 'dummy' + size.ToString;
end;

and now the query

query {
  profil(id: 9) {
    id
    profilePic(size:100)
  }
}

and in the playground I get the following result:

{
  "data": {
    "profil": {
      "id": 9,
      "profilePic": null
    }
  },
  "errors": [
    {
      "message": "Could not find field TProfil.profilePic",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "extensions": {
        "code": "RttiFieldResolverException"
      }
    }
  ]
}

It seems that the resolver does not accept the function/method as a property. Or is the schema definition wrong and I have to write something else there?
Do I miss an attribute for profilePic?

Kind regards
Harald

Probably you didn't previously bind the class to the schema. In this case, GraphQL engine tries to get the field value dynamically, on the fly, and indeed in this case it only looks for fields.

But you can simply bind it in advance and it will properly use the method:

  Schema.Bind('Profil', TProfil);

Hello Wagner,

thank You for this advice - it works as expected. I'm really looking foreward in any evolution this product will take.

Kind regards
Harald

1 Like

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