Fetch data after given createdAt date

Viewed 678

I want to fetch data of last one hour.

I have doubt how to add > && < in postgraphile?

if I add current_timestamp in double quotation still getting error.

Here is postgraphile screen.

enter image description here'hour in postgraphile

1 Answers

You can read about filtering in the PostGraphile docs. To achieve what you've specified you could use the postgraphile-plugin-connection-filter plugin, and issue a query such as this:

query SessionsSince($timestamp: DateTime!) {
  allSessions(filter: {
    createdAt: { greaterThan: $timestamp }
    deviceid: { equalTo: "123" }
  }) {
    ...
  }
}

However, please keep in mind that a GraphQL API is not intended to expose all the functionality of your database to your users. Rather than exposing this raw interface, I'd personally use a custom query which would expose a much neater API; something like:

create function recent_sessions(deviceid text) returns setof sessions as $$
  select *
  from sessions
  where sessions.deviceid = recent_sessions.deviceid
  and created_at > NOW() - interval '1 hour';
$$ language sql stable;

You'd then query it like this:

{
  recentSessions(deviceid: "123") {
    nodes {
      deviceid
    }
  }
}
Related