Last active
May 13, 2020 08:45
-
-
Save Oluwasetemi/2602cf6284af7b166439801b0472baf2 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 { 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); | |
} | |
})(); |
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 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); | |
} | |
})(); |
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 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); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hope 🤞 this helps any one trying to setup graphql fetching of a resource in a node app.