Skip to content

Commit d7f680f

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

File tree

13 files changed

+1349
-1352
lines changed

13 files changed

+1349
-1352
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.

src/components/LaunchList/index.tsx

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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 {
66
data: LaunchProfileQuery;

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: 5 additions & 2 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]);

0 commit comments

Comments
 (0)