-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.ts
176 lines (147 loc) · 4.05 KB
/
db.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
connectStorage,
disconnectStorage,
Schema,
Configuration,
Entity,
generateGraphQLSchema,
ProtocolGraphQLConfiguration,
Context,
ProtocolGraphQL,
} from '../src';
import { StorageTypePostgres } from '../src/storage-connector/StorageTypePostgres';
import StoragePostgresConfiguration from '../src/storage-connector/StoragePostgresConfiguration';
import { writeFileSync } from 'fs';
import { Profile } from './models/Profile';
import { Message } from './models/Message';
import { BoardMember } from './models/BoardMember';
import { Book } from './models/Book';
import { DataTypeTester } from './models/DataTypeTester';
import { BoardMemberView } from './models/BoardMemberView';
import { Connection } from 'typeorm';
import {
ExecutionResult,
graphql,
GraphQLResolveInfo,
GraphQLSchema,
printSchema,
Source,
} from 'graphql';
import { formatGraphQLError } from '../src/graphqlProtocol/util';
import { getMutationResolver } from '../src/graphqlProtocol/resolver';
import { Website } from './models/Website';
import { WebsiteTag } from './models/WebsiteTag';
const schema = new Schema({
defaultStorageType: StorageTypePostgres,
entities: [
Profile,
Message,
BoardMember,
Book,
DataTypeTester,
BoardMemberView,
Website,
WebsiteTag,
],
});
const languages = ['en', 'de'];
const configuration = new Configuration({
languages,
schema,
protocolConfiguration: new ProtocolGraphQLConfiguration(),
});
let connection: Connection;
let graphqlSchema: GraphQLSchema;
export const initDB = async (): Promise<void> => {
const storageConfiguration = new StoragePostgresConfiguration({
connectionConfig: {
host: process.env.PGHOST || 'localhost',
port: parseInt(process.env.PGPORT || '5432', 10),
username: process.env.PGUSER || 'postgres',
password: process.env.PGPASSWORD || null,
database: process.env.SHYFT_TEST_DB || 'shyft_tests',
// logging: true,
},
});
configuration.setStorageConfiguration(storageConfiguration);
ProtocolGraphQL.setProtocolConfiguration(new ProtocolGraphQLConfiguration());
connection = await connectStorage(configuration, true, true);
};
export const disconnectDB = async (): Promise<void> => {
await disconnectStorage(connection);
};
export const initGraphQLSchema = (): void => {
graphqlSchema = generateGraphQLSchema(configuration);
writeFileSync('./schema.graphql', printSchema(graphqlSchema), 'utf8');
};
export async function testGraphql(
query: Source | string,
context?: unknown,
payload?: { [key: string]: unknown },
): Promise<ExecutionResult> {
const result = await graphql(
graphqlSchema,
query,
null,
context || {},
payload,
);
if (result.errors) {
return {
...result,
errors: result.errors.map(formatGraphQLError),
};
}
return result;
}
export const mutate = async (
entity: Entity,
mutationName: string,
payload,
id,
context: Context,
) => {
const typeName = entity.name;
const entityMutation = entity.getMutationByName(mutationName);
const resolver = getMutationResolver({
entity,
entityMutation,
typeName,
nested: false,
idResolver: () => {
return id;
},
});
const result = await resolver(
{},
{ input: { [typeName]: payload } },
context,
{} as GraphQLResolveInfo,
);
if (entityMutation.isTypeDelete) {
return result;
}
return result[typeName];
};
export const findOne = async (entity, id, payload, context) => {
return await StorageTypePostgres.findOne(entity, id, payload, context);
};
export const findOneByValue = async (entity, payload, context) => {
return await StorageTypePostgres.findOneByValues(entity, payload, context);
};
export const find = async (entity, payload, context, parentConnection?) => {
return await StorageTypePostgres.find(
entity,
payload,
context,
parentConnection,
);
};
export const count = async (entity, payload, context, parentConnection?) => {
return await StorageTypePostgres.count(
entity,
payload,
context,
parentConnection,
);
};