/
setupSchema.ts
66 lines (59 loc) · 1.59 KB
/
setupSchema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { gql } from 'apollo-server-express';
import {
PubSub,
addMockFunctionsToSchema,
makeExecutableSchema
} from 'apollo-server';
import { importSchema } from 'graphql-import';
import { GraphQLSchema } from 'graphql/type/schema';
import { merge } from 'lodash';
import AuthDirective from './directives/authDirective';
import FormattableDateDirective from './directives/formattableDate';
import mocks from './mocks';
import {
BlogResolver,
ExampleResolver,
MovieResolver,
StarwarsResolver,
UserResolver
} from './resolvers';
export const pubsub = new PubSub();
const typeDefs = gql(importSchema('server/graphql/schema/main.graphql'));
// Merge all the resolvers
const resolvers = merge(
ExampleResolver,
StarwarsResolver,
UserResolver,
MovieResolver,
BlogResolver
);
const schemaDirectives = {
date: FormattableDateDirective,
auth: AuthDirective
};
export { mocks, schemaDirectives, resolvers, typeDefs };
// Create GraphQL Schema with all the pieces in place
export const setupSchema = (): GraphQLSchema => {
const myTypeDefs = importSchema('server/graphql/schema/main.graphql');
const schema = makeExecutableSchema({
typeDefs: myTypeDefs,
resolvers: resolvers,
schemaDirectives: {
date: FormattableDateDirective,
auth: AuthDirective
}
});
if (
process.env.GRAPHQL_MOCK !== undefined &&
process.env.GRAPHQL_MOCK === 'true'
) {
// Add mocks, modifies schema in place
// Preserve resolvers that are implemented
addMockFunctionsToSchema({
schema,
mocks: mocks,
preserveResolvers: true
});
}
return schema;
};