Created
January 8, 2020 00:55
-
-
Save axelav/a5c51be275842bb75ab221a530307eff to your computer and use it in GitHub Desktop.
`useFetch` example
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
// https://github.com/zeit/next.js/blob/8e2097997e75d8867a9111c861b624e35e14f811/examples/with-graphql-faunadb/graphql/api.js | |
import useFetch from '../lib/useFetch' | |
function getData(data) { | |
if (!data || data.errors) return null | |
return data.data | |
} | |
function getErrorMessage(error, data) { | |
if (error) return error.message | |
if (data && data.errors) { | |
return data.errors[0].message | |
} | |
return null | |
} | |
/** | |
|-------------------------------------------------- | |
| This GraphQL query returns an array of Guestbook | |
| entries complete with both the provided and implicit | |
| data attributes. | |
| | |
| Learn more about GraphQL: https://graphql.org/learn/ | |
|-------------------------------------------------- | |
*/ | |
export const useGuestbookEntries = () => { | |
const query = `query Entries($size: Int) { | |
entries(_size: $size) { | |
data { | |
_id | |
_ts | |
twitter_handle | |
story | |
} | |
after | |
} | |
}` | |
const size = 100 | |
const { data, error } = useFetch(process.env.faunaDbGraphQlEndpoint, { | |
method: 'POST', | |
headers: { | |
Authorization: `Bearer ${process.env.faunaDbSecret}`, | |
'Content-type': 'application/json', | |
Accept: 'application/json', | |
}, | |
body: JSON.stringify({ | |
query, | |
variables: { size }, | |
}), | |
}) | |
return { | |
data: getData(data), | |
errorMessage: getErrorMessage(error, data), | |
error, | |
} | |
} | |
/** | |
|-------------------------------------------------- | |
| This GraphQL mutation creates a new GuestbookEntry | |
| with the requisite twitter handle and story arguments. | |
| | |
| It returns the stored data and includes the unique | |
| identifier (_id) as well as _ts (time created). | |
| | |
| The guestbook uses the _id value as the unique key | |
| and the _ts value to sort and display the date of | |
| publication. | |
| | |
| Learn more about GraphQL mutations: https://graphql.org/learn/queries/#mutations | |
|-------------------------------------------------- | |
*/ | |
export const createGuestbookEntry = async (twitterHandle, story) => { | |
const query = `mutation CreateGuestbookEntry($twitterHandle: String!, $story: String!) { | |
createGuestbookEntry(data: { | |
twitter_handle: $twitterHandle, | |
story: $story | |
}) { | |
_id | |
_ts | |
twitter_handle | |
story | |
} | |
}` | |
const res = await fetch(process.env.faunaDbGraphQlEndpoint, { | |
method: 'POST', | |
headers: { | |
Authorization: `Bearer ${process.env.faunaDbSecret}`, | |
'Content-type': 'application/json', | |
Accept: 'application/json', | |
}, | |
body: JSON.stringify({ | |
query, | |
variables: { twitterHandle, story }, | |
}), | |
}) | |
const data = await res.json() | |
return data | |
} |
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
// https://github.com/zeit/next.js/blob/bf5e295c0c090779210f80f9f6d2ed3bc96a7c05/examples/with-graphql-faunadb/lib/useFetch.js#L3 | |
import { useState, useEffect } from 'react' | |
export default function useFetch(url, options) { | |
const [data, setData] = useState(null) | |
const [error, setError] = useState(null) | |
useEffect(() => { | |
const fetchData = async () => { | |
try { | |
const res = await fetch(url, options) | |
const json = await res.json() | |
setData(json) | |
} catch (error) { | |
setError(error) | |
} | |
} | |
fetchData() | |
// eslint-disable-next-line react-hooks/exhaustive-deps | |
}, [url]) | |
return { data, error } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment