Last active
September 23, 2017 21:25
-
-
Save alundiak/fca768083d43c7792965394de2e7c02c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const apiUrl = 'https://api.github.com/graphql'; | |
export default function getInfo(TOKEN, myArrayOfRepositories) { | |
let queryBody = createRepositoriesQueryBody(myArrayOfRepositories); | |
let options = { | |
method: 'post', | |
headers: { | |
'Authorization': `token ${TOKEN}`, | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
query: queryBody | |
}) | |
}; | |
fetch(apiUrl, options) | |
.then(response => { | |
return response.json(); | |
}) | |
.then(data => { | |
console.log(data.data); // GitHub API v4 returns in parent data object. | |
}); | |
} | |
function createRepositoriesQueryBody(myData) { | |
let fragmentString = ` | |
fragment repositoryFragment on Repository { | |
name | |
nameWithOwner | |
description | |
createdAt | |
updatedAt | |
pushedAt | |
isFork | |
forks { | |
totalCount | |
} | |
viewerHasStarred | |
viewerSubscription | |
owner { | |
login | |
__typename | |
} | |
stargazers { | |
totalCount | |
} | |
watchers { | |
totalCount | |
} | |
homepageUrl | |
url | |
projectsUrl | |
} | |
`; | |
// updatedAt - @deprecated | |
let strings = []; | |
myData.forEach(function(repo, index) { | |
let lineTemplate = `repo${++index}: repository(owner: "${repo.owner}", name: "${repo.name}") { ...repositoryFragment }`; | |
strings.push(lineTemplate); | |
}); | |
let queryString = ` | |
{ | |
${strings.join('\n')} | |
} | |
`; | |
let queryBody = fragmentString + queryString; | |
return queryBody; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment