Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Created July 19, 2017 02:50
Show Gist options
  • Save jbaxleyiii/f17d21681246a4d6586d39968f6211f1 to your computer and use it in GitHub Desktop.
Save jbaxleyiii/f17d21681246a4d6586d39968f6211f1 to your computer and use it in GitHub Desktop.
TypeScript and React Apollo
import React from "react";
import gql from "graphql-tag";
import { graphql } from "react-apollo";
export const HERO_QUERY = gql`
query GetCharacter($episode: Episode!) {
hero(episode: $episode) {
name
id
friends {
name
id
appearsIn
}
}
}
`;
+ export type Hero = {
+ name: string;
+ id: string;
+ appearsIn: string[];
+ friends: Hero[];
+ };
+ export type Response = {
+ hero: Hero;
+ };
+ export const withCharacter = graphql<Response>(HERO_QUERY, {
- export const withCharacter(({ data: { loading, hero, error } }) => {
options: () => ({
variables: { episode: "JEDI" },
}),
});
export default withCharacter(({ data: { loading, hero, error } }) => {
if (loading) return <div>Loading</div>;
if (error) return <h1>ERROR</h1>;
return (
<div>
{hero &&
<div>
<h3>{hero.name}</h3>
{hero.friends.map(friend =>
<h6 key={friend.id}>
{friend.name}:
{" "}{friend.appearsIn.map(x => x.toLowerCase()).join(", ")}
</h6>
)}
</div>}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment