Graphql query by count - apollo-server

I have grphql query with count for detail table.
Artist
Album
Track
Here is my query
query Artists($take: Int, $skip: Int, $orderBy: [ArtistOrderByWithRelationInput!]) {
artists(take: $take, skip: $skip, orderBy: $orderBy) {
id
name
profile_picture
_count {
albums
tracks
}
}
aggregateArtist {
_count {
id
}
}
}
What I want is I only want to get artists who has at least on song tracks. I can list the tracks count but I can't query. May I know how to query by count?

Related

How to add filter to nested objects in GraphQL the GraphQL way?

Say I want to query an object tree like this:
query UsersWithOrgs {
users {
nodes {
firstName
lastName
orgMemberships {
nodes {
joinedAt
organization {
name
foundedAt
location {
city
country
}
}
}
}
}
}
}
Now say I want to filter for only the users where the org they joinedAt <= 6 months ago, or only the users where the org they joined is in England.
query UsersWhereJoinedRecently {
users {
nodes {
firstName
lastName
orgMemberships(joinedAt: { lte: "2022/03/12" }) {
nodes {
joinedAt
organization {
name
foundedAt
location {
city
country
}
}
}
}
}
}
}
query UsersWhereOrgInEngland {
users {
nodes {
firstName
lastName
orgMemberships {
nodes {
joinedAt
organization {
name
foundedAt
location(country: "England") {
city
country
}
}
}
}
}
}
}
Is this the "GraphQL way" of defining filters on nested objects? Or should the filters be hoisted up somehow?
Based on the definitions above, I would instead expect UsersWhereJoinedRecently to return all users in the app, but orgMemberships would be filtered to only include those after the joinedAt date. Likewise, I would expect UsersWhereOrgInEngland to return all users with all their orgs, but only return the location for the org if the country is "England". That is not at all what I want, I want only the users where if that deeply nested condition is false, not to return the user. What is the appropriate way people handle this situation?
To me there are two passes in the overall query:
Joins and Wheres.
Projection.
First, we must fetch all users where the deeply nested condition is true. Then given those top-level IDs for the user, we fetch the projected schema.
So first:
// userIds =
select id from users
inner join memberships on memberships.userId = users.id
where memberships.joinedAt <= "2022/03/12"
Then:
select firstName, lastName from users where id in userIds
select joinedAt, orgId from orgMemberships where userId in userIds and joinedAt <= "2022/03/12"
... select from each of the other tables.
... then build into tree and return to client.
Same with the other query.
So because of these two seemingly completely separate phases, it makes me think that all filtering should be passed in at the top level in some fashion in GraphQL. Like:
query UsersWhereJoinedRecently {
users(membershipJoinedAt: { lte: "2022/03/12" }) {
nodes {
firstName
lastName
orgMemberships {
nodes {
joinedAt
organization {
name
foundedAt
location {
city
country
}
}
}
}
}
}
}
Is that correct, is that the recommended practice? Or what is recommended practice in these sorts of "filter by nested objects" scenarios?

how to run subquery on results of initial query

I am new to graphql and have a simple query returning results. i then want to loop the retuned results and run another query. I am after the data returned from the second query.
1st query
{
queryOne (where: {deployer: "id"}) {
id
...
state {
constants
}
}
constants will contain an array of 2 rows. which i can ref as result.data.state.constants[0] and result.data.state.constants[1]. On these 2 I would like to run the following
queryTwo (where: {id: result.data.state.constants[0]}) {
id
...
}

Could I use few queries in another query in GraphQL?

For example, I have two queries:
query FirstOne {
city {
id
name
}
}
and
query SecondOne {
person {
id
name
}
}
Can I do something like this?
query CombinedQuery {
FirstOne
SecondOne
}
I want to reduce query duplications because if I can't create a query like CombinedQuery I will need to create it in this way:
query CombinedQuery {
city {
id
name
}
person {
id
name
}
}
From my perspective - this query is bad.

Dynamically pass required fields into GraphQL query

I have a query where my required fields inside the query may change. User is allowed to generate the query on fly and user can select the fields in the query. Is there a way to pass the required fields into query based on user's selection from a dropdown. Eg: in below query id, traveller, visaApplication can be replaced by anyother fields. So my queries has to dynamic.
{
Travels{
id
traveller {
nationality
firstName
}
visaApplication {
nationality
city
}
}
}
Fields can be added dynamically using string interpolation:
const otherFields = `
traveller {
nationality
firstName
}
`
const query = gql`
{
Travels {
id
${otherFields}
}
}
`
You can also utilize the #skip or #include directives, combined with variables, to control whether to skip/include particular fields for a specific request while utilizing a single query:
const query = gql`
query (
$includeTraveller: Boolean!
$includeVisa: Boolean!
) {
Travels {
id
traveller #include(if: $includeTraveller) {
nationality
firstName
}
visaApplication #include(if: $includeVisa) {
nationality
city
}
}
}
`

Can I use a property from a higher level field as input parameter for a lower level field in a GraphQL query?

Can I use a property from a higher level field as input parameter for a lower level field in a GraphQL query?
For example, a soccer club has players and seasons. In each season, a subset of the club's players play games for the club. The players of the club can have a different squad number each season.
I want to query the club's players for a season, along with their squad number for that season, and for each player I want a list of the games they have played in that season.
I can get a list of all the games (in all seasons) a player has played for the club. But I want to filter that list so only the games in the queried season are included in the list.
query {
club {
players {
fullName
}
seasons {
id <-- I want to use this value as input parameter $seasonId below
players {
edges {
squadNumber
node {
fullName
games(seasonId: $seasonId) { <-- how to use the value here?
homeTeam
awayTeam
}
}
}
}
games {
players {
edges {
goalsCount
node {
fullName
games(seasonId: $seasonId) { <-- how to use the value here?
homeTeam
awayTeam
}
}
}
}
}
}
}
}
If this is not possible, should I introduce a SeasonPlayer GraphQL type (containing the squad number of the player for a season)? Or should I use a query parameter containing the season ID?

Resources