byme8 / ZeroQL

C# GraphQL client with Linq-like syntax
MIT License
278 stars 13 forks source link

Query example for queries with pagination #79

Closed everscope closed 1 year ago

everscope commented 1 year ago

Hello,

Could you add example of ZeroQl query for query with pagination to documentation? I can not find any examples in documentation

Thank you!

byme8 commented 1 year ago

The pagination implementation depends more on the server side. The ZeroQL works with whatever server exposes. For example, server graphql schema with pagination:

schema {
  query: Queries
}

type Queries {
  users(page: Int!, size: Int!, sorting: UserSorting!, order: SortOrder!): [User!]
}

enum UserSorting {
  FirstName,
  LastName
}

enum SortOrder {
  DESC,
  ASC
}

type User {
  id: Int!
  firstName: String!
  lastName: String!
}

The ZeroQL call:

var page = 0;
var size = 10;
var top10Users = await client.Query(q => q.Users(page, size, UserSorting.FirstName, SortOrder.Desc,  u => u.Id))

I wrote the example without IDE, so there can be syntax issues.

everscope commented 1 year ago

I have this query

query {
  MyType(first: 10) {
    edges {
      cursor
      node {
        id
        name
      }
    }
  }
}

And I want to rewrite it with ZeroQl, but I cannot select MyType directly, because it is inside node

image

so I suppose it should be like this, but it does not work and I can not find similar examples in google

image image
byme8 commented 1 year ago

Can you post the full schema?

everscope commented 1 year ago

Sure! Example schema looks like this:

type Query {
  users(first: Int after: String last: Int before: String): UsersConnection
}

type UsersConnection {
  pageInfo: PageInfo!
  edges: [UsersEdge!]
  nodes: [User!]
}

type UsersEdge {
  cursor: String!
  node: User!
}

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  startCursor: String
  endCursor: String
}

That's default schema for paging using connection's

https://graphql.org/learn/pagination/#end-of-list-counts-and-connections https://chillicream.com/docs/hotchocolate/v13/fetching-data/pagination

byme8 commented 1 year ago

Oh, I see it. It is a completely different problem than I thought at the beginning.

The anonymous classes support syntax like that new { o.Id } but they expect you to define the property when you call the method like that new { Edges = o.Edges() }

So, define the property:

var siteLocationsQuerry = _cloudServiceGraphQlClient
         .Query(sitelocationsFilter, static (i, q) =>
              q.SiteLocations(
                   where: i.Where, order: null, first: 1000,
                   selector: n => new 
                   {
                       Edges = n.Edges(e => e.Node(n => n.Id))
                   }));
everscope commented 1 year ago

Oh, now I see🥲 Thank you!