From 783cbcb4783b6e5d0d405a0199bfb3615b29ccbd Mon Sep 17 00:00:00 2001 From: Seth Date: Fri, 16 Jul 2021 14:48:50 +0100 Subject: [PATCH 1/6] Readme updated to new query method --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0fef143..d48e17c 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ fetch('https://api.github.com/graphql', { 'Content-Type': 'application/json', 'Authorization': `bearer ${accessCode}` }, - body: JSON.stringify({ query: githubApiv4.queries.Viewer }), + body: JSON.stringify({ query: githubApiv4.queries.ViewerQuery("bio login twitterUsername email company avatarUrl" ) }), }) .then(res => res.json()) .then(res => console.log(res.data)); @@ -61,7 +61,7 @@ fetch('https://api.github.com/graphql', { 'Content-Type': 'application/json', 'Authorization': `bearer ${accessCode}` }, - body: JSON.stringify({ query: githubApiv4.queries.Viewer }), + body: JSON.stringify({ query: githubApiv4.queries.ViewerQuery("bio login twitterUsername email company avatarUrl" )}), }) .then(res => res.json()) .then(res => { From 6c980e75d7c448df08ba53794fc2ce13031b622a Mon Sep 17 00:00:00 2001 From: Seth Date: Fri, 16 Jul 2021 14:54:19 +0100 Subject: [PATCH 2/6] ViewQuery changed to Viewer --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d48e17c..aca6ed1 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ fetch('https://api.github.com/graphql', { 'Content-Type': 'application/json', 'Authorization': `bearer ${accessCode}` }, - body: JSON.stringify({ query: githubApiv4.queries.ViewerQuery("bio login twitterUsername email company avatarUrl" ) }), + body: JSON.stringify({ query: githubApiv4.queries.Viewer("bio login twitterUsername email company avatarUrl" ) }), }) .then(res => res.json()) .then(res => console.log(res.data)); @@ -61,7 +61,7 @@ fetch('https://api.github.com/graphql', { 'Content-Type': 'application/json', 'Authorization': `bearer ${accessCode}` }, - body: JSON.stringify({ query: githubApiv4.queries.ViewerQuery("bio login twitterUsername email company avatarUrl" )}), + body: JSON.stringify({ query: githubApiv4.queries.Viewer("bio login twitterUsername email company avatarUrl" )}), }) .then(res => res.json()) .then(res => { From 9b37df78b8a02857f4e4afbd7a5e9ac8c19312c3 Mon Sep 17 00:00:00 2001 From: sectur1 Date: Fri, 16 Jul 2021 16:04:51 +0100 Subject: [PATCH 3/6] 1 query and 2 mutations added --- README.md | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 191 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index aca6ed1..7443b94 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ fetch('https://api.github.com/graphql', { 'Content-Type': 'application/json', 'Authorization': `bearer ${accessCode}` }, - body: JSON.stringify({ query: githubApiv4.queries.Viewer("bio login twitterUsername email company avatarUrl" ) }), + body: JSON.stringify({ query: githubApiv4.queries.Viewer }), }) .then(res => res.json()) .then(res => console.log(res.data)); @@ -54,23 +54,202 @@ import * as githubApiv4 from 'github-apiv4'; import * as fetch from 'isomorphic-fetch'; const accessCode = 'YOUR_GITHUB_ACCESS_CODE'; +const query = queries.Viewer(`bio ${queries.Following({first:10, fields:"login"})}`) -fetch('https://api.github.com/graphql', { +fetch('https://api.github.com/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `bearer ${accessCode}` }, - body: JSON.stringify({ query: githubApiv4.queries.Viewer("bio login twitterUsername email company avatarUrl" )}), -}) - .then(res => res.json()) - .then(res => { - if((res.data as githubApiv4.types.Viewer).viewer){ - console.log(res.data) - }else{ - console.log('Type Mismatch!'); + body: JSON.stringify({ query: query}), +}).then(res => res.json().then(res => { + if(res.data){ + const response:types.Viewer = res.data + console.log(response.viewer.bio ) + console.log(response.viewer.following) + + }else{ + console.log("An error occurred",res) + console.log(query) + } + })) + .catch(err => console.log(err)); + +``` + +**Typescript Example - query Repository:** + +Save file as **repositoryTS.ts** +```ts +import * as githubApiv4 from 'github-apiv4'; +import * as fetch from 'isomorphic-fetch'; + +const accessCode = 'YOUR_GITHUB_ACCESS_CODE'; +const user = queries.User("bio") +const commitCommentField = ` + body ${queries.Author("login")} + ${queries.ReactionGroups("createdAt")} + ${queries.Reactions({first: 10, fields: `${queries.Reactable("id")}`})} + ${queries.UserContentEdits({first:10, fields:"createdAt"})} +` +const fields = ` + ${queries.AssignableUsers({first:10,fields:"bio name"})} + ${queries.BranchProtectionRules({first: 10, fields:"allowsDeletions"})} + ${queries.CodeOfConduct("id url body")} + ${queries.Collaborators({first: 10, fields:"name url login", affiliation:"OUTSIDE"})} + ${queries.CommitComments({first: 10, fields:commitCommentField})} + contactLinks {name} + defaultBranchRef{id name} + ${queries.DeployKeys({first: 10, fields:"id title "})} + ${queries.Deployments({first: 10, fields:"environment description"})} + ${queries.Forks({first: 10, fields: "description createdAt id", })} + ${queries.Issue("22","id body createdAt")} + ${queries.Label("severe: new feature","description")} + ${queries.Labels({first:10, fields:"description"})} + ${queries.Languages({first: 10, fields:"color name"})} + ${queries.LatestRelease("description")} + ${queries.LicenseInfo("body")} + ${queries.MentionableUsers({first:10,fields:"login"})} + ${queries.Milestones({first:10, fields:"description"})} + ${queries.Milestone(3, "description")} + ${queries.Packages({first: 10, fields:"name"})} + ${queries.PinnedIssues({first:10, fields:"databaseId"})} + ${queries.Projects({first: 10,fields:"number body"})} + ${queries.Project(14, "body number")} + ${queries.Refs({refPrefix:"refs/",first:10,fields:"prefix name"})} + ${queries.Ref("refs/heads/dwds-integration","name ")} + ${queries.Releases({first:10, fields:"name" })} + ${queries.Release("v0.4.0","name description")} + ${queries.RepositoryTopics({first:10, fields: `${queries.Topic(`name ${queries.RelatedTopics(10,"name")}`)}`})} + ${queries.Stargazers({first:10, fields:"login"})} + ${queries.Submodules({first:10, fields:"name"})} + ${queries.VulnerabilityAlerts({first: 10,fields:"createdAt"})} + ${queries.Watchers({first:10,fields:"login"})} +` +const query = queries.RepositoryQuery("fundhillapi","Sectur1",fields) +// const query = queries.RepositoryQuery("react","facebook",fields) + +fetch('https://api.github.com/graphql', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `bearer ${accessCode}` + }, + body: JSON.stringify({ query: query}), +}).then(res => res.json().then(res => { + if(res.data){ + const response:types.Repository = res.data + console.log(response.repository.assignableUsers.nodes[4].name ) + console.log(response.repository.branchProtectionRules ) + console.log(response.repository.codeOfConduct.body) + console.log(response.repository.collaborators.nodes[0].login) + console.log(response.repository.commitComments.nodes[9].author) + console.log(response.repository.commitComments.nodes[9].reactionGroups) + console.log(response.repository.commitComments.nodes[9].reactions.nodes) + console.log(response.repository.commitComments.nodes[9].userContentEdits.nodes) + console.log(response.repository.contactLinks[0]) + console.log(response.repository.defaultBranchRef) + console.log(response.repository.deployments.nodes[0].description) + console.log(response.repository.forks.nodes) + console.log(response.repository.issue.body) + console.log(response.repository.label) + console.log(response.repository.labels) + console.log(response.repository.languages.nodes[5].name) + console.log(response.repository.latestRelease) + console.log(response.repository.licenseInfo) + console.log(response.repository.mentionableUsers) + console.log(response.repository.milestones.nodes[4].description) + console.log(response.repository.milestone.description) + console.log(response.repository.packages) + console.log(response.repository.projects.nodes) + console.log(response.repository.project) + console.log(response.repository.refs.nodes[5]) + console.log(response.repository.ref) + console.log(response.repository.releases.nodes) + console.log(response.repository.release) + console.log(response.repository.repositoryTopics.nodes[2].topic.relatedTopics[5].name) + console.log(response.repository.stargazers) + console.log(response.repository.submodules.nodes) + console.log(response.repository.vulnerabilityAlerts); + }else{ + console.log(query) + for(let error of res.errors){ + console.log(error.message, error.locations) } - }) + console.log("An error occurred",res) + + + } + })) + .catch(err => console.log(err)); + +``` +**Typescript Example - creating a Repository:** + +Save file as **createRepo.ts** +```ts +import { types } from "github-apiv4" +import fetch from "isomorphic-fetch"; +import { mutation } from "./mutations" + +const mutation = mutations.CreatePullRequest({baseRefName:"master", body:"Merge Mutation with Master", headRefName:"Awesome", title:"Mutations", repositoryId:"MDEwOlJlcG9zaXRvcnkzNzAzNDg4NDU=",},queries.PullRequest("title body id")), + +fetch('https://api.github.com/graphql', { + method: 'POST', "headers": { + 'Content-Type': 'application/json', + 'Authorization': `bearer ${accessCode}`, + "Accept": "application/vnd.github.update-refs-preview+json " + "application/vnd.github.hawkgirl-preview+json " + + "application/vnd.github.starfox-preview+json " + "application/vnd.github.merge-info-preview+json " + + "application/vnd.github.corsair-preview+json " + "application/vnd.github.flash-preview+json " + + "application/vnd.github.bane-preview+json " + "application/vnd.github.package-deletes-preview+json " + + "application/vnd.github.slothette-preview+json " + "application/vnd.github.stone-crop-preview+json" + }, + body: JSON.stringify({ query: mutation }), +}).then(res => res.json().then(res => { + if (res.data) { + if(res.errors)console.log(res.errors) + const data: types.CreatePullRequest = res.data + console.log(data.createPullRequest.pullRequest) + } else { + console.log("An error occurred", res) + } +})) + .catch(err => console.log(err)); + +``` +**Typescript Example - creating a Project:** + +Save file as **createRepo.ts** +```ts +import { types } from "github-apiv4" +import fetch from "isomorphic-fetch"; +import { mutation } from "./mutations" + +const mutation = mutations.mutations.CreateProject({name: "GitHubv4", ownerId:"MDQ6VXNlcjIyODE3NDky",body:"This is the test project for Githubv4", repositoryIds:[`"MDEwOlJlcG9zaXRvcnkzNjgxMzc4NjU="`]}, queries.Project("body url")), + +fetch('https://api.github.com/graphql', { + method: 'POST', "headers": { + 'Content-Type': 'application/json', + 'Authorization': `bearer ${accessCode}`, + "Accept": "application/vnd.github.update-refs-preview+json " + "application/vnd.github.hawkgirl-preview+json " + + "application/vnd.github.starfox-preview+json " + "application/vnd.github.merge-info-preview+json " + + "application/vnd.github.corsair-preview+json " + "application/vnd.github.flash-preview+json " + + "application/vnd.github.bane-preview+json " + "application/vnd.github.package-deletes-preview+json " + + "application/vnd.github.slothette-preview+json " + "application/vnd.github.stone-crop-preview+json" + }, + body: JSON.stringify({ query: mutation }), +}).then(res => res.json().then(res => { + if (res.data) { + if(res.errors)console.log(res.errors) + const data: types.CreateProject = res.data + console.log(data.CreateProject.project) + } else { + console.log("An error occurred", res) + } +})) + .catch(err => console.log(err)); + ``` Github API response (res.data is) of type [githubApiv4.types.Viewer](https://github-apiv4.netlify.com/interfaces/_types_.viewer.html) @@ -140,7 +319,7 @@ node apolloClient.js # Resources - [API Documentation](https://github-apiv4.netlify.com/) -- [Examples](#usage) +- [Examples](https://github.com/Sectur1/github-test) # Contribute ### Contributing to Github-APIv4 From ea5fe6844cfc04003ac8dfc212e35a26a804e52c Mon Sep 17 00:00:00 2001 From: sectur1 Date: Fri, 16 Jul 2021 17:13:52 +0100 Subject: [PATCH 4/6] Mutations and Comments added --- README.md | 623 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 461 insertions(+), 162 deletions(-) diff --git a/README.md b/README.md index 7443b94..93f6ce1 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,55 @@ # Github-APIv4 + + ![travis build](https://api.travis-ci.org/ioedeveloper/github-apiv4.svg?branch=master) [![npm version](https://badge.fury.io/js/github-apiv4.svg)](https://badge.fury.io/js/github-apiv4) [![Open Source Helpers](https://www.codetriage.com/ioedeveloper/github-apiv4/badges/users.svg)](https://www.codetriage.com/ioedeveloper/github-apiv4) + ###### [API](https://github-apiv4.netlify.com/) | [Contribute](#contribute) | [FAQ](#faq) + > Github-APIv4 is a Node library that provides a list of Github graphql (APIv4) queries and their respective return types. + ## Getting Started ### Installation + To use Github-APIv4, run: + ```bash npm i github-apiv4 # or "yarn add github-apiv4" ``` ### Usage + **Javascript Example - query Viewer:** Save file as **exampleJS.js** -```js -const githubApiv4 = require('github-apiv4'); -const fetch = require('isomorphic-fetch'); -const accessCode = 'YOUR_GITHUB_ACCESS_CODE'; -fetch('https://api.github.com/graphql', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `bearer ${accessCode}` - }, - body: JSON.stringify({ query: githubApiv4.queries.Viewer }), +```js +const githubApiv4 = require("github-apiv4"); +const fetch = require("isomorphic-fetch"); +const accessCode = "YOUR_GITHUB_ACCESS_CODE"; + +//Make the quest here + +fetch("https://api.github.com/graphql", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `bearer ${accessCode}`, + }, + body: JSON.stringify({ query: githubApiv4.queries.Viewer }), }) - .then(res => res.json()) - .then(res => console.log(res.data)); + .then((res) => res.json()) + .then((res) => console.log(res.data)); ``` + Github API response (res.data is) of type [githubApiv4.types.Viewer](https://github-apiv4.netlify.com/interfaces/_types_.viewer.html) Execute script on the command line @@ -48,151 +60,374 @@ node exampleJS.js **Typescript Example - query Viewer:** +Save file as **exampleJS.js** + +```js +const { githubApiv4 } = "github-apiv4", + { fetch } = require("isomorphic-fetch"); + +const accessCode = "YOUR_GITHUB_ACCESS_CODE"; +const query = queries.Viewer(`bio ${queries.Following({ first: 10, fields: "login" })}`); + +//Make the request here using your HTTP client of choice +fetch("https://api.github.com/graphql", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `bearer ${accessCode}`, + }, + body: JSON.stringify({ query: query }), +}) + .then((res) => + res.json().then((res) => { + //Check if the response has a data + if (res.data) { + const response = res.data; + + //Use the data + console.log(response.viewer.bio); + console.log(response.viewer.following); + } else { + //Show the error + + console.log("An error occurred", res); + console.log(query); + } + }) + ) + .catch((err) => console.log(err)); +``` + +**Typescript Example - query Viewer:** + Save file as **exampleTS.ts** + ```ts -import * as githubApiv4 from 'github-apiv4'; -import * as fetch from 'isomorphic-fetch'; +import * as githubApiv4 from "github-apiv4"; +import * as fetch from "isomorphic-fetch"; + +const accessCode = "YOUR_GITHUB_ACCESS_CODE"; +const query = queries.Viewer(`bio ${queries.Following({ first: 10, fields: "login" })}`); + +//Make the request here using your HTTP client of choice +fetch("https://api.github.com/graphql", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `bearer ${accessCode}`, + }, + body: JSON.stringify({ query: query }), +}) + .then((res) => + res.json().then((res) => { + //Check if the response has a data + if (res.data) { + //Use the data. The return type is a typeof Viewer + const response: types.Viewer = res.data; + + console.log(response.viewer.bio); + console.log(response.viewer.following); + } else { + //Show the error + + console.log("An error occurred", res); + console.log(query); + } + }) + ) + .catch((err) => console.log(err)); +``` -const accessCode = 'YOUR_GITHUB_ACCESS_CODE'; -const query = queries.Viewer(`bio ${queries.Following({first:10, fields:"login"})}`) +**JavaScript Example - query Repository:** -fetch('https://api.github.com/graphql', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `bearer ${accessCode}` - }, - body: JSON.stringify({ query: query}), -}).then(res => res.json().then(res => { - if(res.data){ - const response:types.Viewer = res.data - console.log(response.viewer.bio ) - console.log(response.viewer.following) - - }else{ - console.log("An error occurred",res) - console.log(query) - } - })) - .catch(err => console.log(err)); +Save file as **repositoryJS.js** + +```js +const { githubApiv4 } = require("github-apiv4"), + { fetch } = require("isomorphic-fetch"); + +const accessCode = "YOUR_GITHUB_ACCESS_CODE"; + +//Assign Quaries to variables to help you not repeat yours +const user = queries.User("bio"); +const commitCommentField = ` + body ${queries.Author("login")} + ${queries.ReactionGroups("createdAt")} + ${queries.Reactions({ first: 10, fields: `${queries.Reactable("id")}` })} + ${queries.UserContentEdits({ first: 10, fields: "createdAt" })} +`; +//This creates the fields that will be used for the request +//Create as many Queries as you want and you can chain them +const fields = ` + ${queries.AssignableUsers({ first: 10, fields: "bio name" })} + ${queries.BranchProtectionRules({ first: 10, fields: "allowsDeletions" })} + ${queries.CodeOfConduct("id url body")} + ${queries.Collaborators({ first: 10, fields: "name url login", affiliation: "OUTSIDE" })} + ${queries.CommitComments({ first: 10, fields: commitCommentField })} + contactLinks {name} + defaultBranchRef{id name} + ${queries.DeployKeys({ first: 10, fields: "id title " })} + ${queries.Deployments({ first: 10, fields: "environment description" })} + ${queries.Forks({ first: 10, fields: "description createdAt id" })} + ${queries.Issue("22", "id body createdAt")} + ${queries.Label("severe: new feature", "description")} + ${queries.Labels({ first: 10, fields: "description" })} + ${queries.Languages({ first: 10, fields: "color name" })} + ${queries.LatestRelease("description")} + ${queries.LicenseInfo("body")} + ${queries.MentionableUsers({ first: 10, fields: "login" })} + ${queries.Milestones({ first: 10, fields: "description" })} + ${queries.Milestone(3, "description")} + ${queries.Packages({ first: 10, fields: "name" })} + ${queries.PinnedIssues({ first: 10, fields: "databaseId" })} + ${queries.Projects({ first: 10, fields: "number body" })} + ${queries.Project(14, "body number")} + ${queries.Refs({ refPrefix: "refs/", first: 10, fields: "prefix name" })} + ${queries.Ref("refs/heads/dwds-integration", "name ")} + ${queries.Releases({ first: 10, fields: "name" })} + ${queries.Release("v0.4.0", "name description")} + ${queries.RepositoryTopics({ first: 10, fields: `${queries.Topic(`name ${queries.RelatedTopics(10, "name")}`)}` })} + ${queries.Stargazers({ first: 10, fields: "login" })} + ${queries.Submodules({ first: 10, fields: "name" })} + ${queries.VulnerabilityAlerts({ first: 10, fields: "createdAt" })} + ${queries.Watchers({ first: 10, fields: "login" })} +`; + +//This passes the fields to the RepositoryQuery Query +const query = queries.RepositoryQuery("fundhillapi", "Sectur1", fields); + +//Make the request here using your HTTP client of choice +fetch("https://api.github.com/graphql", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `bearer ${accessCode}`, + }, + body: JSON.stringify({ query: query }), +}) + .then((res) => + res.json().then((res) => { + //Check if the response has a data + if (res.data) { + const response = res.data; + //Use the data + + console.log(response.repository.assignableUsers.nodes[4].name); + console.log(response.repository.branchProtectionRules); + console.log(response.repository.codeOfConduct.body); + console.log(response.repository.collaborators.nodes[0].login); + console.log(response.repository.commitComments.nodes[9].author); + console.log(response.repository.commitComments.nodes[9].reactionGroups); + console.log(response.repository.commitComments.nodes[9].reactions.nodes); + console.log(response.repository.commitComments.nodes[9].userContentEdits.nodes); + console.log(response.repository.contactLinks[0]); + console.log(response.repository.defaultBranchRef); + console.log(response.repository.deployments.nodes[0].description); + console.log(response.repository.forks.nodes); + console.log(response.repository.issue.body); + console.log(response.repository.label); + console.log(response.repository.labels); + console.log(response.repository.languages.nodes[5].name); + console.log(response.repository.latestRelease); + console.log(response.repository.licenseInfo); + console.log(response.repository.mentionableUsers); + console.log(response.repository.milestones.nodes[4].description); + console.log(response.repository.milestone.description); + console.log(response.repository.packages); + console.log(response.repository.projects.nodes); + console.log(response.repository.project); + console.log(response.repository.refs.nodes[5]); + console.log(response.repository.ref); + console.log(response.repository.releases.nodes); + console.log(response.repository.release); + console.log(response.repository.repositoryTopics.nodes[2].topic.relatedTopics[5].name); + console.log(response.repository.stargazers); + console.log(response.repository.submodules.nodes); + console.log(response.repository.vulnerabilityAlerts); + } else { + //There can be more that one error in a Query so you can loop through them + for (let error of res.errors) { + console.log(error.message, error.locations); + } + + console.log("An error occurred", res); + } + }) + ) + .catch((err) => console.log(err)); ``` **Typescript Example - query Repository:** Save file as **repositoryTS.ts** + ```ts -import * as githubApiv4 from 'github-apiv4'; -import * as fetch from 'isomorphic-fetch'; +import * as githubApiv4 from "github-apiv4"; +import * as fetch from "isomorphic-fetch"; + +const accessCode = "YOUR_GITHUB_ACCESS_CODE"; -const accessCode = 'YOUR_GITHUB_ACCESS_CODE'; -const user = queries.User("bio") +//Assign Quaries to variables to help you not repeat yours +const user = queries.User("bio"); const commitCommentField = ` body ${queries.Author("login")} ${queries.ReactionGroups("createdAt")} - ${queries.Reactions({first: 10, fields: `${queries.Reactable("id")}`})} - ${queries.UserContentEdits({first:10, fields:"createdAt"})} -` + ${queries.Reactions({ first: 10, fields: `${queries.Reactable("id")}` })} + ${queries.UserContentEdits({ first: 10, fields: "createdAt" })} +`; + +//This creates the fields that will be used for the request +//Create as many Queries as you want and you can chain them const fields = ` - ${queries.AssignableUsers({first:10,fields:"bio name"})} - ${queries.BranchProtectionRules({first: 10, fields:"allowsDeletions"})} + ${queries.AssignableUsers({ first: 10, fields: "bio name" })} + ${queries.BranchProtectionRules({ first: 10, fields: "allowsDeletions" })} ${queries.CodeOfConduct("id url body")} - ${queries.Collaborators({first: 10, fields:"name url login", affiliation:"OUTSIDE"})} - ${queries.CommitComments({first: 10, fields:commitCommentField})} + ${queries.Collaborators({ first: 10, fields: "name url login", affiliation: "OUTSIDE" })} + ${queries.CommitComments({ first: 10, fields: commitCommentField })} contactLinks {name} defaultBranchRef{id name} - ${queries.DeployKeys({first: 10, fields:"id title "})} - ${queries.Deployments({first: 10, fields:"environment description"})} - ${queries.Forks({first: 10, fields: "description createdAt id", })} - ${queries.Issue("22","id body createdAt")} - ${queries.Label("severe: new feature","description")} - ${queries.Labels({first:10, fields:"description"})} - ${queries.Languages({first: 10, fields:"color name"})} + ${queries.DeployKeys({ first: 10, fields: "id title " })} + ${queries.Deployments({ first: 10, fields: "environment description" })} + ${queries.Forks({ first: 10, fields: "description createdAt id" })} + ${queries.Issue("22", "id body createdAt")} + ${queries.Label("severe: new feature", "description")} + ${queries.Labels({ first: 10, fields: "description" })} + ${queries.Languages({ first: 10, fields: "color name" })} ${queries.LatestRelease("description")} ${queries.LicenseInfo("body")} - ${queries.MentionableUsers({first:10,fields:"login"})} - ${queries.Milestones({first:10, fields:"description"})} + ${queries.MentionableUsers({ first: 10, fields: "login" })} + ${queries.Milestones({ first: 10, fields: "description" })} ${queries.Milestone(3, "description")} - ${queries.Packages({first: 10, fields:"name"})} - ${queries.PinnedIssues({first:10, fields:"databaseId"})} - ${queries.Projects({first: 10,fields:"number body"})} + ${queries.Packages({ first: 10, fields: "name" })} + ${queries.PinnedIssues({ first: 10, fields: "databaseId" })} + ${queries.Projects({ first: 10, fields: "number body" })} ${queries.Project(14, "body number")} - ${queries.Refs({refPrefix:"refs/",first:10,fields:"prefix name"})} - ${queries.Ref("refs/heads/dwds-integration","name ")} - ${queries.Releases({first:10, fields:"name" })} - ${queries.Release("v0.4.0","name description")} - ${queries.RepositoryTopics({first:10, fields: `${queries.Topic(`name ${queries.RelatedTopics(10,"name")}`)}`})} - ${queries.Stargazers({first:10, fields:"login"})} - ${queries.Submodules({first:10, fields:"name"})} - ${queries.VulnerabilityAlerts({first: 10,fields:"createdAt"})} - ${queries.Watchers({first:10,fields:"login"})} -` -const query = queries.RepositoryQuery("fundhillapi","Sectur1",fields) -// const query = queries.RepositoryQuery("react","facebook",fields) + ${queries.Refs({ refPrefix: "refs/", first: 10, fields: "prefix name" })} + ${queries.Ref("refs/heads/dwds-integration", "name ")} + ${queries.Releases({ first: 10, fields: "name" })} + ${queries.Release("v0.4.0", "name description")} + ${queries.RepositoryTopics({ first: 10, fields: `${queries.Topic(`name ${queries.RelatedTopics(10, "name")}`)}` })} + ${queries.Stargazers({ first: 10, fields: "login" })} + ${queries.Submodules({ first: 10, fields: "name" })} + ${queries.VulnerabilityAlerts({ first: 10, fields: "createdAt" })} + ${queries.Watchers({ first: 10, fields: "login" })} +`; + +//This passes the fields to the RepositoryQuery Query +const query = queries.RepositoryQuery("fundhillapi", "Sectur1", fields); + +//Make the request here using your HTTP client of choice +fetch("https://api.github.com/graphql", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `bearer ${accessCode}`, + }, + body: JSON.stringify({ query: query }), +}) + .then((res) => + res.json().then((res) => { + //Check if the response has a data + if (res.data) { + //This creates a response variable with the typeof Repository + const response: types.Repository = res.data; + + //Use the data + console.log(response.repository.assignableUsers.nodes[4].name); + console.log(response.repository.branchProtectionRules); + console.log(response.repository.codeOfConduct.body); + console.log(response.repository.collaborators.nodes[0].login); + console.log(response.repository.commitComments.nodes[9].author); + console.log(response.repository.commitComments.nodes[9].reactionGroups); + console.log(response.repository.commitComments.nodes[9].reactions.nodes); + console.log(response.repository.commitComments.nodes[9].userContentEdits.nodes); + console.log(response.repository.contactLinks[0]); + console.log(response.repository.defaultBranchRef); + console.log(response.repository.deployments.nodes[0].description); + console.log(response.repository.forks.nodes); + console.log(response.repository.issue.body); + console.log(response.repository.label); + console.log(response.repository.labels); + console.log(response.repository.languages.nodes[5].name); + console.log(response.repository.latestRelease); + console.log(response.repository.licenseInfo); + console.log(response.repository.mentionableUsers); + console.log(response.repository.milestones.nodes[4].description); + console.log(response.repository.milestone.description); + console.log(response.repository.packages); + console.log(response.repository.projects.nodes); + console.log(response.repository.project); + console.log(response.repository.refs.nodes[5]); + console.log(response.repository.ref); + console.log(response.repository.releases.nodes); + console.log(response.repository.release); + console.log(response.repository.repositoryTopics.nodes[2].topic.relatedTopics[5].name); + console.log(response.repository.stargazers); + console.log(response.repository.submodules.nodes); + console.log(response.repository.vulnerabilityAlerts); + } else { + //There can be more that one error in a Query so you can loop through them + for (let error of res.errors) { + console.log(error.message, error.locations); + } + console.log("An error occurred", res); + } + }) + ) + .catch((err) => console.log(err)); +``` + +**Typescript Example - creating a PullRequest:** + +Save file as **createPullrequest.js** +```js +const { types } = require("github-apiv4"), + fetch = require("isomorphic-fetch"), + { mutation } = require("./mutations") + +//This assigns the mutation to the mutation variable +const mutation = mutations.CreatePullRequest({baseRefName:"master", body:"Merge Mutation with Master", headRefName:"Awesome", title:"Mutations", repositoryId:"MDEwOlJlcG9zaXRvcnkzNzAzNDg4NDU=",},queries.PullRequest("title body id")), + +//Make the request here using your HTTP client of choice fetch('https://api.github.com/graphql', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `bearer ${accessCode}` - }, - body: JSON.stringify({ query: query}), + method: 'POST', "headers": { + 'Content-Type': 'application/json', + 'Authorization': `bearer ${accessCode}`, + "Accept": "application/vnd.github.update-refs-preview+json " + "application/vnd.github.hawkgirl-preview+json " + + "application/vnd.github.starfox-preview+json " + "application/vnd.github.merge-info-preview+json " + + "application/vnd.github.corsair-preview+json " + "application/vnd.github.flash-preview+json " + + "application/vnd.github.bane-preview+json " + "application/vnd.github.package-deletes-preview+json " + + "application/vnd.github.slothette-preview+json " + "application/vnd.github.stone-crop-preview+json" + }, + body: JSON.stringify({ query: mutation }), }).then(res => res.json().then(res => { - if(res.data){ - const response:types.Repository = res.data - console.log(response.repository.assignableUsers.nodes[4].name ) - console.log(response.repository.branchProtectionRules ) - console.log(response.repository.codeOfConduct.body) - console.log(response.repository.collaborators.nodes[0].login) - console.log(response.repository.commitComments.nodes[9].author) - console.log(response.repository.commitComments.nodes[9].reactionGroups) - console.log(response.repository.commitComments.nodes[9].reactions.nodes) - console.log(response.repository.commitComments.nodes[9].userContentEdits.nodes) - console.log(response.repository.contactLinks[0]) - console.log(response.repository.defaultBranchRef) - console.log(response.repository.deployments.nodes[0].description) - console.log(response.repository.forks.nodes) - console.log(response.repository.issue.body) - console.log(response.repository.label) - console.log(response.repository.labels) - console.log(response.repository.languages.nodes[5].name) - console.log(response.repository.latestRelease) - console.log(response.repository.licenseInfo) - console.log(response.repository.mentionableUsers) - console.log(response.repository.milestones.nodes[4].description) - console.log(response.repository.milestone.description) - console.log(response.repository.packages) - console.log(response.repository.projects.nodes) - console.log(response.repository.project) - console.log(response.repository.refs.nodes[5]) - console.log(response.repository.ref) - console.log(response.repository.releases.nodes) - console.log(response.repository.release) - console.log(response.repository.repositoryTopics.nodes[2].topic.relatedTopics[5].name) - console.log(response.repository.stargazers) - console.log(response.repository.submodules.nodes) - console.log(response.repository.vulnerabilityAlerts); - }else{ - console.log(query) - for(let error of res.errors){ - console.log(error.message, error.locations) - } - console.log("An error occurred",res) - - - } - })) + //Check if the response has a data + if (res.data) { + //Use the data + if(res.errors) console.log(res.errors) + + console.log(data.createPullRequest.pullRequest) + } else { + //There can be more that one error in a Query so you can loop through them + console.log("An error occurred", res) + } +})) .catch(err => console.log(err)); ``` -**Typescript Example - creating a Repository:** -Save file as **createRepo.ts** +**Typescript Example - creating a PullRequesr:** + +Save file as **createPullrequest.ts** + ```ts import { types } from "github-apiv4" import fetch from "isomorphic-fetch"; import { mutation } from "./mutations" +//This assigns the mutation const mutation = mutations.CreatePullRequest({baseRefName:"master", body:"Merge Mutation with Master", headRefName:"Awesome", title:"Mutations", repositoryId:"MDEwOlJlcG9zaXRvcnkzNzAzNDg4NDU=",},queries.PullRequest("title body id")), fetch('https://api.github.com/graphql', { @@ -207,10 +442,54 @@ fetch('https://api.github.com/graphql', { }, body: JSON.stringify({ query: mutation }), }).then(res => res.json().then(res => { + //Check if the response has a data + if (res.data) { + //There can be more that one error in a Query so you can loop through them if(res.errors)console.log(res.errors) + //This creates a response variable with the typeof CreatePullRequest const data: types.CreatePullRequest = res.data - console.log(data.createPullRequest.pullRequest) + + console.log(data.createPullRequest.pullRequest) + } else { + //There can be more that one error in a Query so you can loop through them + console.log("An error occurred", res) + } +})) + .catch(err => console.log(err)); + +``` + +Save file as **createProject.js** + +```js +const { types } = require("github-apiv4"), + { fetch } = require("isomorphic-fetch"), + { mutation } = ("./mutations") + +//This creates the mutation +const mutation = mutations.mutations.CreateProject({name: "GitHubv4", ownerId:"MDQ6VXNlcjIyODE3NDky",body:"This is the test project for Githubv4", repositoryIds:[`"MDEwOlJlcG9zaXRvcnkzNjgxMzc4NjU="`]}, queries.Project("body url")), + +//Make the request here using your HTTP client of choice +fetch('https://api.github.com/graphql', { + method: 'POST', "headers": { + 'Content-Type': 'application/json', + 'Authorization': `bearer ${accessCode}`, + "Accept": "application/vnd.github.update-refs-preview+json " + "application/vnd.github.hawkgirl-preview+json " + + "application/vnd.github.starfox-preview+json " + "application/vnd.github.merge-info-preview+json " + + "application/vnd.github.corsair-preview+json " + "application/vnd.github.flash-preview+json " + + "application/vnd.github.bane-preview+json " + "application/vnd.github.package-deletes-preview+json " + + "application/vnd.github.slothette-preview+json " + "application/vnd.github.stone-crop-preview+json" + }, + body: JSON.stringify({ query: mutation }), +}).then(res => res.json().then(res => { + //Check if the response has a data + if (res.data) { + + //There can be more that one error in a Query so you can loop through them + if(res.errors)console.log(res.errors) + + console.log(data.CreateProject.project) } else { console.log("An error occurred", res) } @@ -218,16 +497,20 @@ fetch('https://api.github.com/graphql', { .catch(err => console.log(err)); ``` + **Typescript Example - creating a Project:** -Save file as **createRepo.ts** +Save file as **createProject.ts** + ```ts import { types } from "github-apiv4" import fetch from "isomorphic-fetch"; import { mutation } from "./mutations" +//This creates the mutation const mutation = mutations.mutations.CreateProject({name: "GitHubv4", ownerId:"MDQ6VXNlcjIyODE3NDky",body:"This is the test project for Githubv4", repositoryIds:[`"MDEwOlJlcG9zaXRvcnkzNjgxMzc4NjU="`]}, queries.Project("body url")), +//Make the request here using your HTTP client of choice fetch('https://api.github.com/graphql', { method: 'POST', "headers": { 'Content-Type': 'application/json', @@ -240,10 +523,16 @@ fetch('https://api.github.com/graphql', { }, body: JSON.stringify({ query: mutation }), }).then(res => res.json().then(res => { + //Check if the response has a data if (res.data) { + + //There can be more that one error in a Query so you can loop through them if(res.errors)console.log(res.errors) + + //This creates a response variable with the typeof CreateProject const data: types.CreateProject = res.data - console.log(data.CreateProject.project) + + console.log(data.CreateProject.project) } else { console.log("An error occurred", res) } @@ -251,6 +540,7 @@ fetch('https://api.github.com/graphql', { .catch(err => console.log(err)); ``` + Github API response (res.data is) of type [githubApiv4.types.Viewer](https://github-apiv4.netlify.com/interfaces/_types_.viewer.html) Execute script on the command line @@ -263,51 +553,57 @@ node exampleTS.js **Apollo Client Example - query Repository:** Save file as **apolloClient.ts** + ```ts -import * as githubApiV4 from 'github-apiv4'; -import { HttpLink } from 'apollo-link-http'; -import ApolloClient from 'apollo-client'; -import { InMemoryCache, IntrospectionFragmentMatcher, NormalizedCacheObject } from 'apollo-cache-inmemory'; -import { fetch } from 'cross-fetch'; +import * as githubApiV4 from "github-apiv4"; +import { HttpLink } from "apollo-link-http"; +import ApolloClient from "apollo-client"; +import { InMemoryCache, IntrospectionFragmentMatcher, NormalizedCacheObject } from "apollo-cache-inmemory"; +import { fetch } from "cross-fetch"; import gql from "graphql-tag"; -const accessCode = 'YOUR_GITHUB_ACCESS_CODE'; +const accessCode = "YOUR_GITHUB_ACCESS_CODE"; const client: ApolloClient = new ApolloClient({ - link: new HttpLink({ uri: 'https://api.github.com/graphql', fetch }), - cache: new InMemoryCache({ - fragmentMatcher: new IntrospectionFragmentMatcher({ - introspectionQueryResultData: { - __schema: { - types: [], - }, - }, - }), - }), + link: new HttpLink({ uri: "https://api.github.com/graphql", fetch }), + cache: new InMemoryCache({ + fragmentMatcher: new IntrospectionFragmentMatcher({ + introspectionQueryResultData: { + __schema: { + types: [], + }, + }, + }), + }), }); // query variables used in repository query (githubApiV4.queryVariables.Repository) const queryVariables: githubApiV4.queryVariables.Repository = { - repositoryName: 'github-apiv4', - repositoryOwner: 'community' + repositoryName: "github-apiv4", + repositoryOwner: "community", }; // query with apollo-client -client.query({ - query: gql`${githubApiV4.queries.Repository}`, - variables: queryVariables, - context: { - headers: { - Authorization: `bearer ${accessCode}` - } - } -}).then(res=>{ - if(res.data as githubApiV4.types.Repository){ - console.log(res.data); - }else{ - console.log('Type Mismatch!'); - } -}); +client + .query({ + query: gql` + ${githubApiV4.queries.Repository} + `, + variables: queryVariables, + context: { + headers: { + Authorization: `bearer ${accessCode}`, + }, + }, + }) + .then((res) => { + if (res.data as githubApiV4.types.Repository) { + console.log(res.data); + } else { + console.log("Type Mismatch!"); + } + }); ``` + Github API response (res.data is) of type [githubApiv4.types.Repository](https://github-apiv4.netlify.com/interfaces/_types_.repository.html) Execute script on the command line @@ -316,17 +612,20 @@ Execute script on the command line tsc apolloClient.ts node apolloClient.js ``` + # Resources -- [API Documentation](https://github-apiv4.netlify.com/) -- [Examples](https://github.com/Sectur1/github-test) +- [API Documentation](https://github-apiv4.netlify.com/) +- [Examples](https://github.com/Sectur1/github-test) # Contribute + ### Contributing to Github-APIv4 Check out [contributing guide](https://github.com/ioedeveloper/github-apiv4/blob/master/CONTRIBUTING.md) to get an overview of github-apiv4 development. # FAQ + ### Questions? -- [bugtracker](https://github.com/ioedeveloper/github-apiv4/issues) +- [bugtracker](https://github.com/ioedeveloper/github-apiv4/issues) From f8dc6e232e68a75d5f573158dc8afcb0ff2f4209 Mon Sep 17 00:00:00 2001 From: sectur1 Date: Fri, 16 Jul 2021 17:51:21 +0100 Subject: [PATCH 5/6] Use data remove --- README.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 93f6ce1..cea5be1 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # Github-APIv4 - - -![travis build](https://api.travis-ci.org/ioedeveloper/github-apiv4.svg?branch=master) + " [![npm version](https://badge.fury.io/js/github-apiv4.svg)](https://badge.fury.io/js/github-apiv4) -[![Open Source Helpers](https://www.codetriage.com/ioedeveloper/github-apiv4/badges/users.svg)](https://www.codetriage.com/ioedeveloper/github-apiv4) + @@ -82,9 +80,9 @@ fetch("https://api.github.com/graphql", { res.json().then((res) => { //Check if the response has a data if (res.data) { + //The return type is a typeof Viewer const response = res.data; - //Use the data console.log(response.viewer.bio); console.log(response.viewer.following); } else { @@ -122,7 +120,7 @@ fetch("https://api.github.com/graphql", { res.json().then((res) => { //Check if the response has a data if (res.data) { - //Use the data. The return type is a typeof Viewer + //The return type is a typeof Viewer const response: types.Viewer = res.data; console.log(response.viewer.bio); @@ -210,8 +208,8 @@ fetch("https://api.github.com/graphql", { res.json().then((res) => { //Check if the response has a data if (res.data) { + //The return type is a typeof Repository const response = res.data; - //Use the data console.log(response.repository.assignableUsers.nodes[4].name); console.log(response.repository.branchProtectionRules); @@ -333,7 +331,6 @@ fetch("https://api.github.com/graphql", { //This creates a response variable with the typeof Repository const response: types.Repository = res.data; - //Use the data console.log(response.repository.assignableUsers.nodes[4].name); console.log(response.repository.branchProtectionRules); console.log(response.repository.codeOfConduct.body); @@ -405,7 +402,9 @@ fetch('https://api.github.com/graphql', { }).then(res => res.json().then(res => { //Check if the response has a data if (res.data) { - //Use the data + //The return type is a typeof PullRequest + const response:types.CreatePullRequest = res.data; + if(res.errors) console.log(res.errors) console.log(data.createPullRequest.pullRequest) @@ -447,7 +446,8 @@ fetch('https://api.github.com/graphql', { if (res.data) { //There can be more that one error in a Query so you can loop through them if(res.errors)console.log(res.errors) - //This creates a response variable with the typeof CreatePullRequest + + //This creates a response variable with the typeof PullRequest const data: types.CreatePullRequest = res.data console.log(data.createPullRequest.pullRequest) @@ -485,7 +485,9 @@ fetch('https://api.github.com/graphql', { }).then(res => res.json().then(res => { //Check if the response has a data if (res.data) { - + //The return type is a typeof CreateProject + const response = res.data; + //There can be more that one error in a Query so you can loop through them if(res.errors)console.log(res.errors) From 0359c8ed8062428eeb88ae7d62008420f51a3501 Mon Sep 17 00:00:00 2001 From: sectur1 Date: Fri, 16 Jul 2021 17:52:40 +0100 Subject: [PATCH 6/6] " removed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cea5be1..eb37932 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Github-APIv4 - " + [![npm version](https://badge.fury.io/js/github-apiv4.svg)](https://badge.fury.io/js/github-apiv4)