Skip to content

Commit 1734a50

Browse files
committed
upgrade to introsepctive mock
1 parent 7dc3cb4 commit 1734a50

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

src/index.js

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,62 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { ApolloProvider } from 'react-apollo';
44
import { ApolloClient } from 'apollo-client';
5-
// import { HttpLink } from 'apollo-link-http';
5+
import { HttpLink } from 'apollo-link-http';
66
import { InMemoryCache } from 'apollo-cache-inmemory';
77

88
import { SchemaLink } from 'apollo-link-schema';
9-
import { makeExecutableSchema } from 'graphql-tools';
9+
import {
10+
makeExecutableSchema,
11+
introspectSchema,
12+
} from 'graphql-tools';
13+
14+
import { printSchema } from 'graphql/utilities/schemaPrinter';
1015

1116
import App from './App';
12-
import { typeDefs, resolvers } from './schema';
17+
import {
18+
// schema,
19+
resolvers,
20+
} from './schema';
1321

1422
import registerServiceWorker from './registerServiceWorker';
1523

16-
const cache = new InMemoryCache();
17-
18-
// const GITHUB_BASE_URL = 'https://api.github.com/graphql';
19-
20-
// const httpLink = new HttpLink({
21-
// uri: GITHUB_BASE_URL,
22-
// headers: {
23-
// authorization: `Bearer ${
24-
// process.env.REACT_APP_GITHUB_PERSONAL_ACCESS_TOKEN
25-
// }`,
26-
// },
27-
// });
28-
29-
const schema = makeExecutableSchema({ typeDefs, resolvers });
30-
31-
const client = new ApolloClient({
32-
// link: httpLink,
33-
link: new SchemaLink({ schema }),
34-
cache,
35-
});
36-
37-
ReactDOM.render(
38-
<ApolloProvider client={client}>
39-
<App />
40-
</ApolloProvider>,
41-
document.getElementById('root'),
42-
);
24+
async function render() {
25+
const cache = new InMemoryCache();
26+
27+
const GITHUB_BASE_URL = 'https://api.github.com/graphql';
28+
29+
const httpLink = new HttpLink({
30+
uri: GITHUB_BASE_URL,
31+
headers: {
32+
authorization: `Bearer ${
33+
process.env.REACT_APP_GITHUB_PERSONAL_ACCESS_TOKEN
34+
}`,
35+
},
36+
});
37+
38+
const schema = await introspectSchema(httpLink);
39+
40+
const executableSchema = makeExecutableSchema({
41+
typeDefs: printSchema(schema),
42+
resolvers,
43+
resolverValidationOptions: {
44+
requireResolversForResolveType: false,
45+
},
46+
});
47+
48+
const client = new ApolloClient({
49+
link: new SchemaLink({ schema: executableSchema }),
50+
cache,
51+
});
52+
53+
ReactDOM.render(
54+
<ApolloProvider client={client}>
55+
<App />
56+
</ApolloProvider>,
57+
document.getElementById('root'),
58+
);
59+
}
60+
61+
render();
4362

4463
registerServiceWorker();

src/schema.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const typeDefs = `
1+
export const schema = `
22
type Query {
33
organization(login: String!): Organization!
44
}
@@ -17,22 +17,22 @@ export const typeDefs = `
1717
node: Repository!
1818
}
1919
20-
type Repository {
20+
type Repository implements Starrable {
2121
id: String!
2222
name: String!
2323
url: String!
2424
viewerHasStarred: Boolean!
2525
}
2626
2727
type Mutation {
28-
addStar(input: AddStarInput!): AddStarResult!
28+
addStar(input: AddStarInput!): AddStarPayload!
2929
}
3030
3131
input AddStarInput {
3232
starrableId: ID!
3333
}
3434
35-
type AddStarResult {
35+
type AddStarPayload {
3636
starrable: Starrable!
3737
}
3838
@@ -43,14 +43,6 @@ export const typeDefs = `
4343
`;
4444

4545
export const resolvers = {
46-
Mutation: {
47-
addStar: (parent, { input }) => ({
48-
starrable: {
49-
id: input.starrableId,
50-
viewerHasStarred: true,
51-
},
52-
}),
53-
},
5446
Query: {
5547
organization: (parent, { login }) => ({
5648
name: login,
@@ -77,4 +69,15 @@ export const resolvers = {
7769
},
7870
}),
7971
},
72+
Mutation: {
73+
addStar: (parent, { input }) => ({
74+
starrable: {
75+
id: input.starrableId,
76+
viewerHasStarred: true,
77+
},
78+
}),
79+
},
80+
Starrable: {
81+
__resolveType: () => 'Repository',
82+
},
8083
};

0 commit comments

Comments
 (0)