Skip to content

Instantly share code, notes, and snippets.

@nicostombros
Last active August 16, 2020 09:26
Show Gist options
  • Save nicostombros/774261cb69fd763462ee2e5e69d41e04 to your computer and use it in GitHub Desktop.
Save nicostombros/774261cb69fd763462ee2e5e69d41e04 to your computer and use it in GitHub Desktop.
import { Environment, Network, RecordSource, Store } from "relay-runtime";
const store = new Store(new RecordSource());
const network = Network.create((operation, variables) => {
return fetch("http://127.0.0.1:8000/graphqlEndpoint/", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
query: operation.text,
variables
})
}).then(response => {
return response.json();
});
});
const environment = new Environment({
network,
store
});
export default environment;
import { useState } from 'react';
import { commitMutation } from 'react-relay';
import environment from './environment';
function RootMutation(
mutation,
variables,
) {
// Defining this state, I get https://reactjs.org/warnings/invalid-hook-call-warning.html
// Without this line, my mutation works perfectly fine.
const [data, setData] = useState(null);
commitMutation(
environment,
{
mutation,
variables,
onCompleted: (response, errors) => {
console.log('Response received from server.');
console.log(response)
console.log(errors)
},
onError: err => console.error(err),
},
);
}
export default RootMutation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment