Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Last active May 13, 2020 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oluwasetemi/2602cf6284af7b166439801b0472baf2 to your computer and use it in GitHub Desktop.
Save Oluwasetemi/2602cf6284af7b166439801b0472baf2 to your computer and use it in GitHub Desktop.
const { GraphQLClient } = require('graphql-request');
const client = new GraphQLClient('https://api.github.com/graphql', {
headers: { Authorization: 'token XXXX' }
});
(async () => {
try {
const query = `
{ viewer { name repositories(isFork: true, first: 10) { edges { node { createdAt name databaseId } cursor } totalCount pageInfo { endCursor startCursor hasNextPage hasPreviousPage } } } }
`;
const data = await client.request(query);
console.log(data);
} catch (error) {
console.log(error.message);
}
})();
const axios = require('axios');
(async () => {
try {
const query = `
{ viewer { name repositories(isFork: true, first: 10) { edges { node { createdAt name databaseId } cursor } totalCount pageInfo { endCursor startCursor hasNextPage hasPreviousPage } } } }
`;
const { data, error } = await axios.post(
'https://api.github.com/graphql',
{ query },
{
headers: {
Authorization: 'token XXXXXX',
'Content-Type': 'application/json'
}
}
);
console.log(data);
} catch (error) {
console.log(error.message);
}
})();
const gql = require('graphql-tag');
const { ApolloClient } = require('apollo-client');
const fetch = require('node-fetch');
const { createHttpLink } = require('apollo-link-http');
const { setContext } = require('apollo-link-context');
const { InMemoryCache } = require('apollo-cache-inmemory');
const httpLink = createHttpLink({
uri: 'https://api.github.com/graphql',
fetch
});
const authLink = setContext((_, { headers }) =>
// return the headers to the context so httpLink can read them
({
headers: { Authorization: 'token XXX' }
})
);
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
(async () => {
try {
const query = gql`
query {
viewer {
name
repositories(isFork: true, first: 10) {
edges {
node {
createdAt
name
databaseId
}
cursor
}
totalCount
pageInfo {
endCursor
startCursor
hasNextPage
hasPreviousPage
}
}
}
}
`;
const data = await client.query({ query });
console.log(data);
} catch (error) {
console.log(error.message);
}
})();
@Oluwasetemi
Copy link
Author

I hope 🤞 this helps any one trying to setup graphql fetching of a resource in a node app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment