Is any library of nodejs work with GitHub API v4 exist?
Is any library of nodejs work with GitHub API v4 exist?
Thank you guys cause reading my question. The title is exactly what i wanna known.
Hope it do not cost your time much.
1 Answer
1
Some of the most common graphql client are graphql.js and apollo-client. You can also use the popular request module. The graphql API is a single POST endpoint on https://api.github.com/graphql with a JSON body consisting of the query
fields and the variables
fields (if you have variables in the query)
query
variables
const graphql = require('graphql.js');
var graph = graphql("https://api.github.com/graphql", {
headers: {
"Authorization": "Bearer <Your Token>",
'User-Agent': 'My Application'
},
asJSON: true
});
graph(`
query repo($name: String!, $owner: String!){
repository(name:$name, owner:$owner){
createdAt
}
}
`, {
name: "linux",
owner: "torvalds"
}).then(function(response) {
console.log(JSON.stringify(response, null, 2));
}).catch(function(error) {
console.log(error);
});
fetch = require('node-fetch');
const ApolloClient = require('apollo-client').ApolloClient;
const HttpLink = require('apollo-link-http').HttpLink;
const setContext = require('apollo-link-context').setContext;
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
const gql = require('graphql-tag');
const token = "<Your Token>";
const authLink = setContext((_, {
headers
}) => {
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : null,
}
}
});
const client = new ApolloClient({
link: authLink.concat(new HttpLink({
uri: 'https://api.github.com/graphql'
})),
cache: new InMemoryCache()
});
client.query({
query: gql `
query repo($name: String!, $owner: String!){
repository(name:$name, owner:$owner){
createdAt
}
}
`,
variables: {
name: "linux",
owner: "torvalds"
}
})
.then(resp => console.log(JSON.stringify(resp.data, null, 2)))
.catch(error => console.error(error));
const request = require('request');
request({
method: 'post',
body: {
query: `
query repo($name: String!, $owner: String!){
repository(name:$name, owner:$owner){
createdAt
}
} `,
variables: {
name: "linux",
owner: "torvalds"
}
},
json: true,
url: 'https://api.github.com/graphql',
headers: {
Authorization: 'Bearer <Your Token>',
'User-Agent': 'My Application'
}
}, function(error, response, body) {
if (error) {
console.error(error);
throw error;
}
console.log(JSON.stringify(body, null, 2));
});
thank you a lot.
– Trần Huy
yesterday
Sorry, i'm new member.
– Trần Huy
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
github.com/jaredhanson/passport-github have you looked at this?
– crellee
Jun 29 at 10:14