Created
October 22, 2018 04:29
-
-
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!
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
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); | |
} |
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
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