Skip to content

Commit 258266a

Browse files
committed
simple mock
1 parent d667539 commit 258266a

File tree

5 files changed

+25
-164
lines changed

5 files changed

+25
-164
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"apollo-client": "^2.3.0",
88
"apollo-link-http": "^1.5.4",
99
"apollo-link-schema": "^1.1.0",
10-
"apollo-test-utils": "^0.3.2",
1110
"graphql": "^0.13.2",
1211
"graphql-tag": "^2.9.2",
1312
"react": "^16.3.2",

src/App.js

Lines changed: 6 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,28 @@
11
import React from 'react';
22
import gql from 'graphql-tag';
3-
import { Query, Mutation } from 'react-apollo';
3+
import { Query } from 'react-apollo';
44

55
import './App.css';
66

7-
const GET_REPOSITORIES_OF_ORGANIZATION = gql`
7+
const GET_ORGANIZATION = gql`
88
{
99
organization(login: "the-road-to-learn-react") {
10-
repositories(first: 20) {
11-
edges {
12-
node {
13-
id
14-
name
15-
url
16-
viewerHasStarred
17-
}
18-
}
19-
}
20-
}
21-
}
22-
`;
23-
24-
const STAR_REPOSITORY = gql`
25-
mutation($id: ID!) {
26-
addStar(input: { starrableId: $id }) {
27-
starrable {
28-
id
29-
viewerHasStarred
30-
}
10+
name
11+
url
3112
}
3213
}
3314
`;
3415

3516
const App = () => (
36-
<Query query={GET_REPOSITORIES_OF_ORGANIZATION}>
17+
<Query query={GET_ORGANIZATION}>
3718
{({ data: { organization }, loading }) => {
3819
if (loading || !organization) {
3920
return <div>Loading ...</div>;
4021
}
4122

42-
return (
43-
<Repositories repositories={organization.repositories} />
44-
);
23+
return <a href={organization.url}>{organization.name}</a>;
4524
}}
4625
</Query>
4726
);
4827

49-
class Repositories extends React.Component {
50-
state = {
51-
selectedRepositoryIds: [],
52-
};
53-
54-
toggleSelectRepository = (id, isSelected) => {
55-
let { selectedRepositoryIds } = this.state;
56-
57-
selectedRepositoryIds = isSelected
58-
? selectedRepositoryIds.filter(itemId => itemId !== id)
59-
: selectedRepositoryIds.concat(id);
60-
61-
this.setState({ selectedRepositoryIds });
62-
};
63-
64-
render() {
65-
return (
66-
<RepositoryList
67-
repositories={this.props.repositories}
68-
selectedRepositoryIds={this.state.selectedRepositoryIds}
69-
toggleSelectRepository={this.toggleSelectRepository}
70-
/>
71-
);
72-
}
73-
}
74-
75-
const RepositoryList = ({
76-
repositories,
77-
selectedRepositoryIds,
78-
toggleSelectRepository,
79-
}) => (
80-
<ul>
81-
{repositories.edges.map(({ node }) => {
82-
const isSelected = selectedRepositoryIds.includes(node.id);
83-
84-
const rowClassName = ['row'];
85-
86-
if (isSelected) {
87-
rowClassName.push('row_selected');
88-
}
89-
90-
return (
91-
<li className={rowClassName.join(' ')} key={node.id}>
92-
<Select
93-
id={node.id}
94-
isSelected={isSelected}
95-
toggleSelectRepository={toggleSelectRepository}
96-
/>{' '}
97-
<a href={node.url}>{node.name}</a>{' '}
98-
{!node.viewerHasStarred && <Star id={node.id} />}
99-
</li>
100-
);
101-
})}
102-
</ul>
103-
);
104-
105-
const Star = ({ id }) => (
106-
<Mutation mutation={STAR_REPOSITORY} variables={{ id }}>
107-
{starRepository => (
108-
<button type="button" onClick={starRepository}>
109-
Star
110-
</button>
111-
)}
112-
</Mutation>
113-
);
114-
115-
const Select = ({ id, isSelected, toggleSelectRepository }) => (
116-
<button
117-
type="button"
118-
onClick={() => toggleSelectRepository(id, isSelected)}
119-
>
120-
{isSelected ? 'Unselect' : 'Select'}
121-
</button>
122-
);
123-
12428
export default App;

src/index.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ import ReactDOM from 'react-dom';
33
import { ApolloProvider } from 'react-apollo';
44
import { ApolloClient } from 'apollo-client';
55
// import { HttpLink } from 'apollo-link-http';
6-
import { SchemaLink } from 'apollo-link-schema';
76
import { InMemoryCache } from 'apollo-cache-inmemory';
87

9-
import {
10-
makeExecutableSchema,
11-
addMockFunctionsToSchema,
12-
} from 'graphql-tools';
13-
import { mockNetworkInterfaceWithSchema } from 'apollo-test-utils';
8+
import { SchemaLink } from 'apollo-link-schema';
9+
import { makeExecutableSchema } from 'graphql-tools';
1410

1511
import App from './App';
16-
import { typeDefs, mocks } from './schema';
12+
import { typeDefs, resolvers } from './schema';
1713

1814
import registerServiceWorker from './registerServiceWorker';
1915

@@ -30,11 +26,7 @@ const cache = new InMemoryCache();
3026
// },
3127
// });
3228

33-
const schema = makeExecutableSchema({ typeDefs });
34-
addMockFunctionsToSchema({
35-
schema,
36-
mocks,
37-
});
29+
const schema = makeExecutableSchema({ typeDefs, resolvers });
3830

3931
const client = new ApolloClient({
4032
// link: httpLink,

src/schema.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
export const typeDefs = `
2-
Query {
3-
...
2+
type Query {
3+
organization(login: String!): Organization!
4+
}
5+
6+
type Organization {
7+
name: String!
8+
url: String!
49
}
510
`;
611

7-
export const mocks = {
8-
Query: () => ...,
9-
Mutation: () => ...
10-
};
12+
export const resolvers = {
13+
Query: {
14+
organization: (parent, { login }) => ({
15+
name: login,
16+
url: `https://github.com/${login}`,
17+
}),
18+
},
19+
};

0 commit comments

Comments
 (0)