Skip to content

Instantly share code, notes, and snippets.

@ianfabs
Created October 22, 2018 04:29
Show Gist options
  • Save ianfabs/b68523d20eef42ed2ea94415de0489dd to your computer and use it in GitHub Desktop.
Save ianfabs/b68523d20eef42ed2ea94415de0489dd to your computer and use it in GitHub Desktop.
jraph - a simple fetch wrapper for GraphQL, just to tidy up the look of things!
async function jraph(argv: JraphOptions){
let url = argv.url;
let headers = { 'Content-Type': 'application/json' }
let body = JSON.stringify({query: argv.query});
let fetch_options = {
method: argv.method,
headers,
body
};
//returns request as JSON
// return (await fetch(url, fetch_options).then(res=>res.json())).data;
return fetch(url, fetch_options).then(res=>res.json());
}
//Example Usage
try {
const result = await jraph({
url: 'http://localhost:4000/graphql',
method: 'POST',
query: '{language}'
});
console.dir(result);
//Output should be: `{ data: { langauge: "GraphQL" }}`
} catch (e) {
console.error('err', e);
}
const express = require('express');
const { graphql, buildSchema } = require('graphql');
const graphqlHTTP = require('express-graphql');
const cors = require('cors');
const schema = buildSchema(`
type Query {
language: String
}
`)
const rootValue = {
language: () => 'GraphQL'
}
const app = express()
app.use(cors())
app.use('/graphql', graphqlHTTP({
rootValue, schema, graphiql: true
})
)
app.listen(4000, () => console.log('Listening on 4000'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment