Created
August 20, 2018 20:35
-
-
Save inodaf/9e9f6aa68522c429dbc0f3b0d2b8721e to your computer and use it in GitHub Desktop.
Function Composition
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 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