Skip to content

Commit b320cf3

Browse files
committed
Example using apollo codegen to use dynamic fragments
1 parent ca5f1ba commit b320cf3

File tree

13 files changed

+1361
-1364
lines changed

13 files changed

+1361
-1364
lines changed

__generated__/globalTypes.ts

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
"typescript": "3.4.3"
1919
},
2020
"scripts": {
21-
"start": "react-scripts start",
21+
"start:react-scripts": "react-scripts start",
22+
"start:codegen-apollo-watch": "yarn codegen --watch",
23+
"start": "run-p start:*",
2224
"build": "react-scripts build",
2325
"test": "react-scripts test",
2426
"eject": "react-scripts eject",
25-
"codegen": "graphql-codegen --config codegen.yml"
27+
"codegen": "apollo client:codegen --target typescript --endpoint=https://spacexdata.herokuapp.com/graphql"
2628
},
2729
"eslintConfig": {
2830
"extends": "react-app"
@@ -34,10 +36,8 @@
3436
"not op_mini all"
3537
],
3638
"devDependencies": {
37-
"@graphql-codegen/cli": "^1.1.0",
38-
"@graphql-codegen/typescript": "1.1.0",
39-
"@graphql-codegen/typescript-operations": "1.1.0",
40-
"@graphql-codegen/typescript-react-apollo": "1.1.0",
41-
"dotenv-cli": "^2.0.0"
39+
"apollo": "^2.13.0",
40+
"dotenv-cli": "^2.0.0",
41+
"npm-run-all": "^4.1.5"
4242
}
4343
}

src/components/LaunchList/LaunchList.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import * as React from 'react';
2-
import { LaunchListQuery } from '../../generated/graphql';
32
import './styles.css';
3+
import { LaunchListQuery } from './__generated__/LaunchListQuery';
44

55
export interface OwnProps {
66
handleIdChange: (newId: number) => void;
77
}
88

99
interface Props extends OwnProps {
10-
data: LaunchListQuery;
10+
launches: LaunchListQuery['launches'];
1111
}
1212

1313
const className = 'LaunchList';
1414

15-
const LaunchList: React.FC<Props> = ({ data, handleIdChange }) => (
15+
const LaunchList: React.FC<Props> = ({ launches, handleIdChange }) => (
1616
<div className={className}>
1717
<h3>Launches</h3>
1818
<ol className={`${className}__list`}>
19-
{!!data.launches &&
20-
data.launches.map(
19+
{!!launches &&
20+
launches.map(
2121
(launch, i) =>
2222
!!launch && (
2323
<li

src/components/LaunchList/__generated__/LaunchList.ts

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/LaunchList/__generated__/LaunchListQuery.ts

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
2-
import { useLaunchListQuery } from '../../generated/graphql';
32
import LaunchList, { OwnProps } from './LaunchList';
3+
import { useQuery } from 'react-apollo-hooks';
4+
import { QUERY_LAUNCH_LIST } from './query'
5+
import { LaunchListQuery } from './__generated__/LaunchListQuery';
46

57
const LaunchListContainer = (props: OwnProps) => {
6-
const { data, error, loading } = useLaunchListQuery();
8+
const { data, error, loading } = useQuery<LaunchListQuery>(QUERY_LAUNCH_LIST);
79

810
if (loading) {
911
return <div>Loading...</div>;
@@ -13,7 +15,7 @@ const LaunchListContainer = (props: OwnProps) => {
1315
return <div>ERROR</div>;
1416
}
1517

16-
return <LaunchList data={data} {...props} />;
18+
return <LaunchList launches={data.launches} {...props} />;
1719
};
1820

1921
export default LaunchListContainer;

src/components/LaunchList/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gql from 'graphql-tag';
22

33
export const QUERY_LAUNCH_LIST = gql`
4-
query LaunchList {
4+
query LaunchListQuery {
55
launches {
66
flight_number
77
mission_name

src/components/LaunchProfile/LaunchProfile.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
import * as React from 'react';
2-
import { LaunchProfileQuery } from '../../generated/graphql';
32
import './styles.css';
3+
import { LaunchProfileQuery } from './__generated__/LaunchProfileQuery';
44

55
interface Props {
6-
data: LaunchProfileQuery;
6+
launch: LaunchProfileQuery['launch'];
77
}
88

99
const className = 'LaunchProfile';
1010

11-
const LaunchProfile: React.FC<Props> = ({ data }) => {
12-
if (!data.launch) {
11+
const LaunchProfile: React.FC<Props> = ({ launch }) => {
12+
if (!launch) {
1313
return <div>No launch available</div>;
1414
}
1515

1616
return (
1717
<div className={className}>
1818
<div className={`${className}__status`}>
19-
<span>Flight {data.launch.flight_number}: </span>
20-
{data.launch.launch_success ? (
19+
<span>Flight {launch.flight_number}: </span>
20+
{launch.launch_success ? (
2121
<span className={`${className}__success`}>Success</span>
2222
) : (
2323
<span className={`${className}__failed`}>Failed</span>
2424
)}
2525
</div>
2626
<h1 className={`${className}__title`}>
27-
{data.launch.mission_name}
28-
{data.launch.rocket &&
29-
` (${data.launch.rocket.rocket_name} | ${data.launch.rocket.rocket_type})`}
27+
{launch.mission_name}
28+
{launch.rocket &&
29+
` (${launch.rocket.rocket_name} | ${launch.rocket.rocket_type})`}
3030
</h1>
31-
<p className={`${className}__description`}>{data.launch.details}</p>
32-
{!!data.launch.links && !!data.launch.links.flickr_images && (
31+
<p className={`${className}__description`}>{launch.details}</p>
32+
{!!launch.links && !!launch.links.flickr_images && (
3333
<div className={`${className}__image-list`}>
34-
{data.launch.links.flickr_images.map(image =>
34+
{launch.links.flickr_images.map(image =>
3535
image ? <img src={image} className={`${className}__image`} key={image} /> : null,
3636
)}
3737
</div>

src/components/LaunchProfile/__generated__/LaunchProfileQuery.ts

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/LaunchProfile/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import * as React from 'react';
2-
import { useLaunchProfileQuery } from '../../generated/graphql';
32
import LaunchProfile from './LaunchProfile';
3+
import { useQuery } from 'react-apollo-hooks';
4+
import { LaunchProfileQuery, LaunchProfileQueryVariables } from './__generated__/LaunchProfileQuery';
5+
import { QUERY_LAUNCH_PROFILE } from './query';
46

57
interface OwnProps {
68
id: number;
79
}
810

911
const LaunchProfileContainer = ({ id }: OwnProps) => {
10-
const { data, error, loading, refetch } = useLaunchProfileQuery({
12+
const { data, error, loading, refetch } = useQuery<LaunchProfileQuery, LaunchProfileQueryVariables>(QUERY_LAUNCH_PROFILE, {
1113
variables: { id: String(id) },
1214
});
15+
1316
React.useEffect(() => {
1417
refetch();
1518
}, [id]);
@@ -26,7 +29,7 @@ const LaunchProfileContainer = ({ id }: OwnProps) => {
2629
return <div>Select a flight from the panel</div>;
2730
}
2831

29-
return <LaunchProfile data={data} />;
32+
return <LaunchProfile launch={data.launch} />;
3033
};
3134

3235
export default LaunchProfileContainer;

0 commit comments

Comments
 (0)