Skip to content

Instantly share code, notes, and snippets.

@liamzebedee
Created March 9, 2022 14:29
Show Gist options
  • Select an option

  • Save liamzebedee/caeb34b97d82d52f53e26e2e4a6b842a to your computer and use it in GitHub Desktop.

Select an option

Save liamzebedee/caeb34b97d82d52f53e26e2e4a6b842a to your computer and use it in GitHub Desktop.
async function getProfiles(ids: string[]) {
const res2 = await fetch(`${ANNONCE_SUBGRAPH_URL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
mode: 'cors',
body: JSON.stringify({
query: `
{
profiles( where: { profileId_in: ${JSON.stringify(ids)} } ) {
handle,
profileId
}
}
`
})
})
.then(x => x.json())
return res2.data.profiles
}
async function getProfile(handle: string) {
// 1. Get user profile info.
//
const res1 = await fetch(`${ANNONCE_SUBGRAPH_URL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
mode: 'cors',
body: JSON.stringify({
query: `
{
profiles(where: { handle: "${handle}" }) {
handle,
profileId,
owner,
following {
to {
id
}
},
followers {
from {
id
}
}
}
}
`
})
})
.then(x => x.json())
const profile = res1.data.profiles[0]
// 2. Get following.
//
const following = profile.following
const followers = profile.followers
let followingProfiles = []
let followersProfiles = []
if (following.length > 0) {
const followingProfileIds = following.map((edge: any) => edge.to.id)
followingProfiles = await getProfiles(followingProfileIds)
}
if(followers.length) {
const followersProfileIds = followers.map((edge: any) => edge.from.id)
followersProfiles = await getProfiles(followersProfileIds)
}
const data = {
profile: profile,
following: followingProfiles,
followers: followersProfiles
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment