Skip to content

Instantly share code, notes, and snippets.

@jurajkrivda
Created March 10, 2017 22:41
Show Gist options
  • Save jurajkrivda/c6cae6630cf4d63ae8c07e5799a9cbe3 to your computer and use it in GitHub Desktop.
Save jurajkrivda/c6cae6630cf4d63ae8c07e5799a9cbe3 to your computer and use it in GitHub Desktop.
Custom Apollo/Graphql network interface
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