Created
May 29, 2020 15:03
-
-
Save ryanditjia/9c77389d943dd6322f8dca606e7a0c66 to your computer and use it in GitHub Desktop.
GraphQL in Sapper
This file contains hidden or 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
import { GraphQLClient } from 'graphql-request' | |
export const graphqlClient = new GraphQLClient('YOUR_GRAPHQL_ENDPOINT', { | |
headers: { | |
authorization: `Bearer SOMETHING_SOMETHING`, // if auth is needed | |
}, | |
}) |
This file contains hidden or 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
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