Using GitHub’s GraphQL API, I’m trying to figure out the “right” way to sort my respositories by the number of open PRs:
Here’s my query:
query GetRepositories($after: String) {
search(
type: REPOSITORY
query: "org:TryGhost archived:false fork:false"
first: ${DEFAULT_REPOS}
after: $after
) {
repositoryCount
pageInfo {
endCursor
hasNextPage
}
nodes {
...RepositoryTile
}
}
}
${REPOSITORY_TILE_DATA}
My fragment REPOSITORY_TILE_DATA
fragment RepositoryTile on Repository {
id
nameWithOwner
name
url
pullRequests(states: OPEN, first: 100) {
totalCount
nodes {
isRenovate @client
author {
login
}
url
}
}
}
And then finally my typePolicies
typePolicies: {
Query: {
fields: {
search: {
keyArgs: [],
merge(existing = {}, incoming) {
const existingNodes = existing.nodes || [];
const incomingNodes = incoming.nodes || [];
return {
...existing,
...incoming,
nodes: [...existingNodes, ...incomingNodes],
pageInfo: incoming.pageInfo
};
},
read(existing, {readField, toReference}) {
if (!existing || !existing.nodes) {
return existing;
}
if (existing.nodes.length > 0) {
const sortedNodes = [...existing.nodes].sort((a, b) => {
console.log(readField('name', a));
console.log(readField('pullRequests', a));
// return b.pullRequests.totalCount - a.pullRequests.totalCount;
return 0;
});
return {
...existing,
nodes: sortedNodes
};
}
return existing;
}
}
}
},
Repository: {
keyFields: ['nameWithOwner']
},
PullRequest: {
keyFields: ['number'],
fields: {
isRenovate: {
read(_, {readField}) {
return readField('author').login === 'renovate' ? true : false;
}
}
}
}
}
You can see the sort I’m trying to do on pullRequests.totalCount
This works as expected: console.log(readField('name', a));
But this is undefined: console.log(readField('pullRequests', a));
isReference('pullRequests', a)
returns false
I’m struggling to understand what pullRequests are in this case, or how to access them…