We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Note: there's an ongoing conversation in #24 about promises/async functions in Hooks + Suspense
Here's a rough idea for exposing lightweight Hooks for GraphQL:
import useGraphQuery from '@rehooks/use-graph-query' import ProfileSkeleton from './profile-skeleton' import gql from 'graphql-tag' export default function ProfileDetail(props) { const [{ data, loading }] = useGraphQuery(profileQuery(props.id)) if (loading) return <ProfileSkeleton /> return <div>Name: {data.firstName} {data.lastName}</div> } export function profileQuery(id) { return gql` query { user(id: ${id}) { firstName lastName } } ` }
And then for mutations:
import useInputValue from '@rehooks/input-value' import useGraphQuery from '@rehooks/use-graph-query' import useGraphMutation from '@rehooks/use-graph-mutation' import FormError from './form-error' import FormSkeleton from './form-skeleton' import { profileQuery } from './profile-detail' export default function ProfileForm({ id } { const [{ data, loading, error }] = useGraphQuery(profileQuery(props.id)) const [saveResult, onSave] = useGraphMutation(saveProfileMutation()) if (loading) return <FormSkeleton /> const firstNameInput = useInputValue(data.firstName) const lastNameInput = useInputValue(data.lastName) const onSubmit = evt => { evt.preventDefault() const firstName = firstNameInput[0] const lastName = lastNameInput[0] onSave({ variables: { id, firstName, lastName } }) } return ( <form onSubmit={onSubmit}> <FormError error={error || saveResult.error} /> <input type="text" {...firstNameInput} /> <input type="text" {...lastNameInput} /> <input type="submit" value="Submit" /> </form> ) } export function saveProfileMutation() { return gql` mutation SaveProfile($id: String!, $firstName: String, $lastName: String) { updateProfile(profileId: $id, firstName: $firstName, lastName: $lastName) { id firstName lastName } } ` }
Author's note: I went with [value, fn] for useGraphQuery to account for fetchMore, refetch and other pagination handling patterns.
[value, fn]
useGraphQuery
fetchMore
refetch
This might turn into two-destructurable objects like [{ loading, data }, { refetch, fetchMore }]
[{ loading, data }, { refetch, fetchMore }]
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Here's a rough idea for exposing lightweight Hooks for GraphQL:
And then for mutations:
Author's note: I went with
[value, fn]
foruseGraphQuery
to account forfetchMore
,refetch
and other pagination handling patterns.This might turn into two-destructurable objects like
[{ loading, data }, { refetch, fetchMore }]
The text was updated successfully, but these errors were encountered: