Created
March 10, 2017 22:41
-
-
Save jurajkrivda/c6cae6630cf4d63ae8c07e5799a9cbe3 to your computer and use it in GitHub Desktop.
Custom Apollo/Graphql network interface
This file contains 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 { printAST } from 'apollo-client'; | |
import Config from 'react-native-config'; | |
const isConnected = true; | |
export default () => { | |
return { | |
async query(request) { | |
if (!isConnected) { | |
return new Promise.reject(new Error('no-connection')) | |
} else { | |
try { | |
const response = await fetch(Config.API_URL, { | |
headers: { Accept: 'application/json' }, | |
body: JSON.stringify({ | |
query: printAST(request.query), | |
variables: JSON.stringify(request.variables || {}), | |
debugName: JSON.stringify(request.debugName || ''), | |
operationName: JSON.stringify(request.operationName || '') | |
}), | |
method: 'POST', | |
}); | |
return await response.json(); | |
} catch (error) { | |
return new Promise.reject(error); | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment