Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Typescript example update (#1471)
Browse files Browse the repository at this point in the history
* initial typescript examples update to emulate real to life project

* convert to use render callback styled Query

* fix parameterized type

* use named import not relative

* trying to get build running with package resolution from live repo

* wip through examples build

* Fix missing type annotations

* More type fixes

* comment out test and update App setup for 2.0

* runtime test cannot be compiled because we now have types guarding input.

* changelog
  • Loading branch information
rosskevin authored Dec 27, 2017
1 parent 08cb715 commit 8f339f1
Show file tree
Hide file tree
Showing 22 changed files with 464 additions and 526 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ first three params (`TChildProps` can be derived). [#1402](https://github.com/ap
- Removed unused gzip script [#1468](https://github.com/apollographql/react-apollo/pull/1468)
- Minify umd and ensure umd name consistency [#1469](https://github.com/apollographql/react-apollo/pull/1469)
- Converted `test/test-utils/test-utils.test.js` to `test/test-utils.test.tsx` [#1475](https://github.com/apollographql/react-apollo/pull/1475)
- Updates to `examples/typescript` [#1471](https://github.com/apollographql/react-apollo/pull/1471)

### 2.0.4
- rolled back on the lodash-es changes from
Expand Down
22 changes: 13 additions & 9 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"apollo-client": "^1.9.0-1",
"graphql-tag": "^2.4.2",
"react": "^15.6.1",
"react-apollo": "file:../..",
"react-dom": "^15.6.1"
"apollo-cache-inmemory": "^1.1.4",
"apollo-client": "^2.0.4",
"apollo-link-http": "^1.3.2",
"graphql-tag": "^2.6.1",
"react": "^16.2.0",
"react-apollo": "rosskevin/react-apollo#examples-update",
"react-dom": "^16.2.0"
},
"devDependencies": {
"@types/jest": "21.1.9",
"@types/node": "8.5.2",
"@types/prop-types": "^15.5.2",
"@types/react": "16.0.31",
"@types/react-dom": "15.5.6",
"@types/react-dom": "16.0.3",
"@types/react-test-renderer": "^16.0.0",
"apollo-codegen": "0.18.3",
"react-scripts-ts": "2.4.0",
"react-test-renderer": "15.6.1"
"react-scripts-ts": "2.8.0",
"react-test-renderer": "16.2.0"
},
"scripts": {
"start": "react-scripts-ts start",
Expand All @@ -26,6 +30,6 @@
"schema":
"apollo-codegen introspect-schema https://mpjk0plp9.lp.gql.zone/graphql --output ./src/schema.json",
"types":
"apollo-codegen generate ./src/**/*.tsx --schema ./src/schema.json --target typescript --output ./src/schema.ts --add-typename"
"apollo-codegen generate --schema schema.json --target typescript-modern `find ./src -name *.graphql`"
}
}

Large diffs are not rendered by default.

62 changes: 2 additions & 60 deletions examples/typescript/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,4 @@
// @flow
import * as React from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import Character from './Character';

// import types generated by codegen
import { GetCharacterQuery, GetCharacterQueryVariables } from './schema';

export const HERO_QUERY = gql`
query GetCharacter($episode: Episode!) {
hero(episode: $episode) {
name
id
friends {
name
id
appearsIn
}
}
}
`;

export const CharacterWithoutData = ({ 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 &&
hero.friends.map(
friend =>
friend && (
<h6 key={friend.id}>
{friend.name}:{' '}
{friend.appearsIn.map(x => x && x.toLowerCase()).join(', ')}
</h6>
),
)}
</div>
)}
</div>
);
};

export const Character = graphql<
{},
GetCharacterQuery,
GetCharacterQueryVariables
>(HERO_QUERY, {
options: ({ episode }) => ({
variables: { episode },
}),
props: ({ data }) => ({ ...data }),
})(CharacterWithoutData);

export const App = () => (
<div>
<Character episode="NEWHOPE" />
</div>
);
export const App = () => <Character episode="NEWHOPE" />;
11 changes: 11 additions & 0 deletions examples/typescript/src/Character.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query GetCharacter($episode: Episode!) {
hero(episode: $episode) {
name
id
friends {
name
id
appearsIn
}
}
}
51 changes: 51 additions & 0 deletions examples/typescript/src/Character.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as React from 'react';
import {
GetCharacter,
GetCharacter_hero_friends,
} from './__generated__/GetCharacter';
import { Query, QueryResult } from 'react-apollo';
// import Query, { QueryResult } from '../../../src/Query';

const QUERY = require('./Character.graphql');
// https://github.com/Microsoft/TypeScript/issues/6395#issuecomment-282133254
class CharacterQuery extends Query<GetCharacter> {}

export interface CharacterProps {
episode: string;
}

export const Character = (props: CharacterProps) => {
const { episode } = props;
return (
<CharacterQuery query={QUERY} variables={{ episode }}>
{({ loading, data, error }: QueryResult<GetCharacter>) => {
if (loading) return <div>Loading</div>;
if (error) return <h1>ERROR</h1>;
const { hero } = data;
return (
<div>
{hero && (
<div>
<h3>{hero.name}</h3>
{hero.friends &&
hero.friends.map(
(friend: GetCharacter_hero_friends) =>
friend && (
<h6 key={friend.id}>
{friend.name}:{' '}
{friend.appearsIn
.map(x => x && x.toLowerCase())
.join(', ')}
</h6>
),
)}
</div>
)}
</div>
);
}}
</CharacterQuery>
);
};

export default Character;
40 changes: 40 additions & 0 deletions examples/typescript/src/__generated__/GetCharacter.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8f339f1

Please sign in to comment.