Skip to content

Instantly share code, notes, and snippets.

@ryanditjia
Created May 29, 2020 15:03
Show Gist options
  • Save ryanditjia/9c77389d943dd6322f8dca606e7a0c66 to your computer and use it in GitHub Desktop.
Save ryanditjia/9c77389d943dd6322f8dca606e7a0c66 to your computer and use it in GitHub Desktop.
GraphQL in Sapper
import { GraphQLClient } from 'graphql-request'
export const graphqlClient = new GraphQLClient('YOUR_GRAPHQL_ENDPOINT', {
headers: {
authorization: `Bearer SOMETHING_SOMETHING`, // if auth is needed
},
})
import { graphqlClient } from '../graphql_client'
const query = /* GraphQL */ `
`
export function get(req, res) {
graphqlClient
.request(query)
.then(r => {
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(JSON.stringify(r))
})
.catch(error => {
console.error(JSON.stringify(error, undefined, 2))
res.writeHead(error.response.status, {
'Content-Type': 'application/json',
})
res.end(JSON.stringify({ message: 'Something went wrong while fetching data.' }))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment