Last active
August 16, 2020 09:26
-
-
Save nicostombros/774261cb69fd763462ee2e5e69d41e04 to your computer and use it in GitHub Desktop.
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 { 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; |
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 { 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