Skip to content

Instantly share code, notes, and snippets.

@jordanmkoncz
Created September 4, 2019 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanmkoncz/caadf35214cdb04ef1eabba6238f9f3d to your computer and use it in GitHub Desktop.
Save jordanmkoncz/caadf35214cdb04ef1eabba6238f9f3d to your computer and use it in GitHub Desktop.
Apollo Client Hooks + Hasura
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