Skip to content
New issue

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

useGraphQuery/Mutation #51

Open
fouad opened this issue Oct 31, 2018 · 0 comments
Open

useGraphQuery/Mutation #51

fouad opened this issue Oct 31, 2018 · 0 comments

Comments

@fouad
Copy link
Member

fouad commented Oct 31, 2018

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.

This might turn into two-destructurable objects like [{ loading, data }, { refetch, fetchMore }]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant