Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 00cb43f

Browse files
author
Sashko Stubailo
committed
Refactor schema composition
1 parent c32c908 commit 00cb43f

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

api/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path';
22
import express from 'express';
33
import { apolloExpress, graphiqlExpress } from 'apollo-server';
4-
import { makeExecutableSchema } from 'graphql-tools';
54
import bodyParser from 'body-parser';
65

76
import {
@@ -10,11 +9,12 @@ import {
109
} from './githubKeys';
1110

1211
import { setUpGitHubLogin } from './githubLogin';
13-
import { schema, resolvers } from './schema';
1412
import { GitHubConnector } from './github/connector';
1513
import { Repositories, Users } from './github/models';
1614
import { Entries, Comments } from './sql/models';
1715

16+
import executableSchema from './schema';
17+
1818
let PORT = 3010;
1919
if (process.env.PORT) {
2020
PORT = parseInt(process.env.PORT, 10) + 100;
@@ -27,11 +27,6 @@ app.use(bodyParser.json());
2727

2828
setUpGitHubLogin(app);
2929

30-
const executableSchema = makeExecutableSchema({
31-
typeDefs: schema,
32-
resolvers,
33-
});
34-
3530
app.use('/graphql', apolloExpress((req) => {
3631
// Get the query, the same way express-graphql does it
3732
// https://github.com/graphql/express-graphql/blob/3fa6e68582d6d933d37fa9e841da5d2aa39261cd/src/index.js#L257
@@ -73,6 +68,17 @@ app.use('/graphql', apolloExpress((req) => {
7368

7469
app.use('/graphiql', graphiqlExpress({
7570
endpointURL: '/graphql',
71+
query: `{
72+
feed (type: NEW, limit: 5) {
73+
repository {
74+
owner { login }
75+
name
76+
}
77+
78+
postedBy { login }
79+
}
80+
}
81+
`,
7682
}));
7783

7884
// Serve our helpful static landing page. Not used in production.

api/schema.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { merge } from 'lodash';
22
import { schema as gitHubSchema, resolvers as gitHubResolvers } from './github/schema';
33
import { schema as sqlSchema, resolvers as sqlResolvers } from './sql/schema';
4+
import { makeExecutableSchema } from 'graphql-tools';
45

56
const rootSchema = [`
7+
68
# To select the sort order of the feed
79
enum FeedType {
810
HOT
@@ -43,6 +45,7 @@ schema {
4345
query: Query
4446
mutation: Mutation
4547
}
48+
4649
`];
4750

4851
const rootResolvers = {
@@ -116,5 +119,12 @@ const rootResolvers = {
116119
},
117120
};
118121

119-
export const schema = [...rootSchema, ...gitHubSchema, ...sqlSchema];
120-
export const resolvers = merge(rootResolvers, gitHubResolvers, sqlResolvers);
122+
const schema = [...rootSchema, ...gitHubSchema, ...sqlSchema];
123+
const resolvers = merge(rootResolvers, gitHubResolvers, sqlResolvers);
124+
125+
const executableSchema = makeExecutableSchema({
126+
typeDefs: schema,
127+
resolvers,
128+
});
129+
130+
export default executableSchema;

0 commit comments

Comments
 (0)