Skip to content

Commit 2e4c131

Browse files
rolandszokepeteyycz
authored andcommitted
Add graphql schema definitions
1 parent 225301f commit 2e4c131

File tree

8 files changed

+161
-17
lines changed

8 files changed

+161
-17
lines changed

scripts/createTestData.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
4+
const db = require('../src/db');
5+
6+
Promise.all(db.createTables()).then(() => {
7+
Promise.all(db.createTestData()).then(() => {
8+
console.log('Test data created.');
9+
process.exit(0);
10+
});
11+
});

scripts/dropTables.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
4+
const db = require('../src/db');
5+
6+
Promise.all(db.dropTables()).then(() => {
7+
console.log('Tables dropped.');
8+
process.exit(0);
9+
});

src/db.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const createTables = () => {
4444
)`,
4545
];
4646

47-
createTableQueries.forEach((query) => pool.query(query));
47+
return createTableQueries.map((query) => pool.query(query));
4848
};
4949

5050
const dropTables = () => {
@@ -54,11 +54,44 @@ const dropTables = () => {
5454
'DROP TABLE IF EXISTS comments',
5555
];
5656

57-
dropTableQueries.forEach((query) => pool.query(query));
57+
return dropTableQueries.map((query) => pool.query(query));
58+
};
59+
60+
const createTestData = () => {
61+
// password for test user: password
62+
const testDataQueries = [
63+
`INSERT INTO users(id, name, username, email, password_digest)
64+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b58c', 'Tyler', 'tyler1337', '[email protected]', '$2b$10$yv9DxXTpvBmBYKu8rXoSIONn3BZB5/jQRDPMKt/YUAq8eTYoXGwKu')
65+
RETURNING *
66+
`,
67+
`INSERT INTO users(id, name, username, email, password_digest)
68+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b68c', 'Marla', 'marlaSinger', '[email protected]', '$2b$10$yv9DxXTpvBmBYKu8rXoSIONn3BZB5/jQRDPMKt/YUAq8eTYoXGwKu')
69+
RETURNING *
70+
`,
71+
`INSERT INTO posts(id, title, description, content, author, timestamp)
72+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b78c', 'Test Post', 'This is my test post', 'Test post Content', '[email protected]', '2019-09-15T07:30:57.392Z')
73+
RETURNING *
74+
`,
75+
`INSERT INTO posts(id, title, description, content, author, timestamp)
76+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b58a', 'Test Post 2', 'This is my test post', 'Test post Content', '[email protected]', '2019-09-18T07:30:57.392Z')
77+
RETURNING *
78+
`,
79+
`INSERT INTO comments(id, content, author, post, timestamp)
80+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b88c', 'Test Comment', '[email protected]', '8aaf37cf-94c9-4c6a-b566-0265ce34b78c', '2019-09-15T08:30:57.392Z')
81+
RETURNING *
82+
`,
83+
`INSERT INTO comments(id, content, author, post, timestamp)
84+
VALUES('8aaf37cf-94c9-4c6a-b566-0265ce34b98c', 'Test Comment 2', '[email protected]', '8aaf37cf-94c9-4c6a-b566-0265ce34b78c', '2019-09-16T07:30:57.392Z')
85+
RETURNING *
86+
`,
87+
];
88+
89+
return testDataQueries.map((query) => pool.query(query));
5890
};
5991

6092
module.exports = {
6193
query: (text, params) => pool.query(text, params),
6294
createTables,
6395
dropTables,
96+
createTestData,
6497
};

src/gqlSchema.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
const { buildSchema } = require('graphql');
3+
4+
const { getHello, resolveQuery } = require('./fetcher');
5+
6+
const schema = buildSchema(`
7+
enum OrderDirection {
8+
asc
9+
desc
10+
}
11+
type PageInfo {
12+
hasNextPage: Boolean
13+
hasPreviousPage: Boolean
14+
totalRecords: Int
15+
}
16+
17+
enum UserArgField {
18+
id
19+
name
20+
username
21+
email
22+
}
23+
input UserFieldOrder {
24+
field: UserArgField
25+
direction: OrderDirection
26+
}
27+
type User {
28+
id: ID
29+
name: String
30+
username: String
31+
email: String
32+
}
33+
type UserEdge {
34+
node: User
35+
}
36+
type UserConnection {
37+
pageInfo: PageInfo
38+
edges: [UserEdge]
39+
}
40+
41+
enum PostArgField {
42+
id
43+
title
44+
description
45+
content
46+
author
47+
timestamp
48+
}
49+
input PostFieldOrder {
50+
field: PostArgField
51+
direction: OrderDirection
52+
}
53+
type Post {
54+
id: ID
55+
title: String
56+
description: String
57+
content: String
58+
author: String
59+
timestamp: String
60+
}
61+
type PostEdge {
62+
node: Post
63+
}
64+
type PostConnection {
65+
pageInfo: PageInfo
66+
edges: [PostEdge]
67+
}
68+
69+
type Query {
70+
hello: String
71+
users(limit: Int, offset: Int, order: UserFieldOrder): UserConnection
72+
posts(limit: Int, offset: Int, order: PostFieldOrder): PostConnection
73+
}
74+
`);
75+
76+
const rootValue = {
77+
hello: () => getHello(),
78+
users: (args) => resolveQuery({ table: 'users', args }),
79+
posts: (args) => resolveQuery({ table: 'posts', args }),
80+
};
81+
82+
module.exports = {
83+
schema,
84+
rootValue,
85+
};

src/schema/comment.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const {
1414
Node, PageInfo, FilterOperation, OrderDirection, Datetime,
1515
} = require('./common');
1616

17-
const CommentField = new GraphQLEnumType({
18-
name: 'CommentField',
17+
const CommentArgField = new GraphQLEnumType({
18+
name: 'CommentArgField',
1919
values: {
2020
id: { value: 'id' },
2121
content: { value: 'content' },
@@ -29,15 +29,15 @@ const CommentFieldFilter = new GraphQLInputObjectType({
2929
name: 'CommentFieldFilter',
3030
fields: {
3131
value: { type: GraphQLString },
32-
field: { type: CommentField },
32+
field: { type: CommentArgField },
3333
operation: { type: FilterOperation },
3434
},
3535
});
3636

3737
const CommentFieldOrder = new GraphQLInputObjectType({
3838
name: 'CommentFieldOrder',
3939
fields: {
40-
field: { type: CommentField },
40+
field: { type: CommentArgField },
4141
direction: { type: OrderDirection },
4242
},
4343
});
@@ -115,7 +115,7 @@ const CommentMutations = new GraphQLObjectType({
115115
});
116116

117117
module.exports = {
118-
CommentField,
118+
CommentArgField,
119119
CommentFieldFilter,
120120
CommentFieldOrder,
121121
Comment,

src/schema/post.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const {
1616
} = require('./common');
1717
const { CommentConnection, CommentFieldFilter, CommentFieldOrder } = require('./comment');
1818

19-
const PostField = new GraphQLEnumType({
20-
name: 'PostField',
19+
const PostArgField = new GraphQLEnumType({
20+
name: 'PostArgField',
2121
values: {
2222
id: { value: 'id' },
2323
content: { value: 'content' },
@@ -32,15 +32,15 @@ const PostFieldFilter = new GraphQLInputObjectType({
3232
name: 'PostFieldFilter',
3333
fields: {
3434
value: { type: GraphQLString },
35-
field: { type: PostField },
35+
field: { type: PostArgField },
3636
operation: { type: FilterOperation },
3737
},
3838
});
3939

4040
const PostFieldOrder = new GraphQLInputObjectType({
4141
name: 'PostFieldOrder',
4242
fields: {
43-
field: { type: PostField },
43+
field: { type: PostArgField },
4444
direction: { type: OrderDirection },
4545
},
4646
});
@@ -129,7 +129,7 @@ const PostMutations = new GraphQLObjectType({
129129
});
130130

131131
module.exports = {
132-
PostField,
132+
PostArgField,
133133
PostFieldFilter,
134134
PostFieldOrder,
135135
Post,

src/schema/user.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const {
1717
const { PostConnection, PostFieldFilter, PostFieldOrder } = require('./post');
1818
const { CommentConnection, CommentFieldFilter, CommentFieldOrder } = require('./comment');
1919

20-
const UserField = new GraphQLEnumType({
21-
name: 'UserField',
20+
const UserArgField = new GraphQLEnumType({
21+
name: 'UserArgField',
2222
values: {
2323
id: { value: 'id' },
2424
name: { value: 'name' },
@@ -31,15 +31,15 @@ const UserFieldFilter = new GraphQLInputObjectType({
3131
name: 'UserFieldFilter',
3232
fields: {
3333
value: { type: GraphQLString },
34-
field: { type: UserField },
34+
field: { type: UserArgField },
3535
operation: { type: FilterOperation },
3636
},
3737
});
3838

3939
const UserFieldOrder = new GraphQLInputObjectType({
4040
name: 'UserFieldOrder',
4141
fields: {
42-
field: { type: UserField },
42+
field: { type: UserArgField },
4343
direction: { type: OrderDirection },
4444
},
4545
});
@@ -133,7 +133,7 @@ const UserMutations = new GraphQLObjectType({
133133
});
134134

135135
module.exports = {
136-
UserField,
136+
UserArgField,
137137
UserFieldFilter,
138138
UserFieldOrder,
139139
User,

src/server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ const helmet = require('helmet');
44
const cors = require('cors');
55

66
const db = require('./db');
7+
8+
// Graphql Types - cut rootValue from graphqlHTTP when used
79
const schema = require('./schema');
810
const config = require('./config');
911

12+
// Raw Graphql schema language - add rootValue from graphqlHTTP when used
13+
// const { schema, rootValue } = require('./gqlSchema');
14+
1015
const app = express();
1116

1217
db.createTables();
@@ -21,6 +26,7 @@ app.use(cors({
2126

2227
app.use('/graphql', graphqlHTTP(async () => ({
2328
schema,
29+
// rootValue,
2430
graphiql: true,
2531
customFormatErrorFn: (error) => {
2632
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)