Skip to content

Instantly share code, notes, and snippets.

@inodaf
Created August 20, 2018 20:35
Show Gist options
  • Save inodaf/9e9f6aa68522c429dbc0f3b0d2b8721e to your computer and use it in GitHub Desktop.
Save inodaf/9e9f6aa68522c429dbc0f3b0d2b8721e to your computer and use it in GitHub Desktop.
Function Composition
const Fetch = (fun, fetcher) => fun({
get(endpoint, headers) {
return fetch(endpoint, headers)
},
post(endpoint, body, headers) {
return fetch(endpoint, { body, headers, method: 'POST' })
}
})
const GraphQLConnection = ({ name, args, query }, cb, fetcher) =>
Fetch(http => cb({
fields() {
return { name, args, query }
},
connect(query) {
return http.post('http://localhost:5000', { query })
},
send() {
throw new Error('Should be implemented')
}
}), fetcher)
const MutationBuilder = (GraphQLSchema, fun) => {
return fun(GraphQLSchema.fields())
}
const GraphQLMutation = props =>
GraphQLConnection(props, graphql =>
MutationBuilder(graphql, mutation => ({
send: () => graphql.connect(mutation)
}))
);
const mutation = GraphQLMutation({
name: 'postAccountDocument',
args: { document: '' },
query: 'query {}'
})
mutation.send().then(err => {
return err.json()
}).then(body => {
console.log(body)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment