Skip to content

Commit c08126a

Browse files
committed
02-React GraphQL Query
1 parent ca7b278 commit c08126a

File tree

1 file changed

+90
-4
lines changed

1 file changed

+90
-4
lines changed

src/App.js

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,71 @@ const axiosGitHubGraphQL = axios.create({
1212
},
1313
});
1414

15+
const GET_ISSUES_OF_REPOSITORY = `
16+
query ($organization: String!, $repository: String!) {
17+
organization(login: $organization) {
18+
name
19+
url
20+
repository(name: $repository) {
21+
name
22+
url
23+
issues(last: 5) {
24+
edges {
25+
node {
26+
id
27+
title
28+
url
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
`;
36+
37+
const getIssuesOfRepository = path => {
38+
const [organization, repository] = path.split('/');
39+
40+
return axiosGitHubGraphQL.post('', {
41+
query: GET_ISSUES_OF_REPOSITORY,
42+
variables: { organization, repository },
43+
});
44+
};
45+
46+
const resolveIssuesQuery = queryResult => () => ({
47+
organization: queryResult.data.data.organization,
48+
errors: queryResult.data.errors,
49+
});
50+
1551
class App extends Component {
1652
state = {
1753
path: 'the-road-to-learn-react/the-road-to-learn-react',
54+
organization: null,
55+
errors: null,
1856
};
1957

2058
componentDidMount() {
21-
// fetch data
59+
this.onFetchFromGitHub(this.state.path);
2260
}
2361

2462
onChange = event => {
2563
this.setState({ path: event.target.value });
2664
};
2765

2866
onSubmit = event => {
29-
// fetch data
67+
this.onFetchFromGitHub(this.state.path);
3068

3169
event.preventDefault();
3270
};
3371

72+
onFetchFromGitHub = path => {
73+
getIssuesOfRepository(path).then(queryResult =>
74+
this.setState(resolveIssuesQuery(queryResult)),
75+
);
76+
};
77+
3478
render() {
35-
const { path } = this.state;
79+
const { path, organization, errors } = this.state;
3680

3781
return (
3882
<div>
@@ -54,10 +98,52 @@ class App extends Component {
5498

5599
<hr />
56100

57-
{/* Here comes the result! */}
101+
{organization ? (
102+
<Organization organization={organization} errors={errors} />
103+
) : (
104+
<p>No information yet ...</p>
105+
)}
58106
</div>
59107
);
60108
}
61109
}
62110

111+
const Organization = ({ organization, errors }) => {
112+
if (errors) {
113+
return (
114+
<p>
115+
<strong>Something went wrong:</strong>
116+
{errors.map(error => error.message).join(' ')}
117+
</p>
118+
);
119+
}
120+
121+
return (
122+
<div>
123+
<p>
124+
<strong>Issues from Organization:</strong>
125+
<a href={organization.url}>{organization.name}</a>
126+
</p>
127+
<Repository repository={organization.repository} />
128+
</div>
129+
);
130+
};
131+
132+
const Repository = ({ repository }) => (
133+
<div>
134+
<p>
135+
<strong>In Repository:</strong>
136+
<a href={repository.url}>{repository.name}</a>
137+
</p>
138+
139+
<ul>
140+
{repository.issues.edges.map(issue => (
141+
<li key={issue.node.id}>
142+
<a href={issue.node.url}>{issue.node.title}</a>
143+
</li>
144+
))}
145+
</ul>
146+
</div>
147+
);
148+
63149
export default App;

0 commit comments

Comments
 (0)