Created
September 4, 2019 04:50
-
-
Save jordanmkoncz/caadf35214cdb04ef1eabba6238f9f3d to your computer and use it in GitHub Desktop.
Apollo Client Hooks + Hasura
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
import React from "react"; | |
import { useSubscription, useQuery } from "@apollo/react-hooks"; | |
import gql from "graphql-tag"; | |
const PROFILE_QUERY = gql` | |
query Profile($id: String!) { | |
Person(where: { id: { _eq: $id } }) { | |
id | |
firstName | |
lastName | |
} | |
} | |
`; | |
const PROFILE_SUBSCRIPTION = gql` | |
subscription Profile($id: String!) { | |
Person(where: { id: { _eq: $id } }) { | |
id | |
firstName | |
lastName | |
} | |
} | |
`; | |
function Profile({ id }) { | |
const { loading, error, data } = useQuery(PROFILE_QUERY, { | |
variables: { id }, | |
}); | |
useSubscription(PROFILE_SUBSCRIPTION, { | |
variables: { id }, | |
}); | |
if (loading) { | |
return <p>Loading...</p>; | |
} | |
if (error) { | |
return <p>Error...</p>; | |
} | |
return ( | |
<p> | |
{data.Person.firstName} {data.Person.lastName} | |
</p> | |
); | |
} | |
export default Profile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment