Skip to content

Commit 8418e8f

Browse files
committed
Fixed and improved types: removed relations field from args, corrected incorrect non-null check on columns, made resolver args partial; Turning Errors into GraphQLErrors in resolvers; Removed limit argument from -Single resolvers; Fixed incorrect data mapping of JSON fields in inputs; Added tests
1 parent 571e335 commit 8418e8f

File tree

17 files changed

+6819
-249
lines changed

17 files changed

+6819
-249
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ node_modules
22
package-lock.json
33
dist
44
*.sqlite
5-
**/Test
6-
**/Tests
75
vitest.config.ts
86
drizzle.config.ts
97
*.tgz
@@ -12,4 +10,6 @@ dist-dts
1210
dist.new
1311
Server
1412
pnpm-lock.yaml
15-
drizzle.config.test.ts
13+
runMysql.ts
14+
Tests/.temp
15+
Tests/Migrations

Tests/Schema/mysql.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { relations } from 'drizzle-orm'
2+
import {
3+
bigint,
4+
boolean,
5+
char,
6+
date,
7+
int,
8+
mysqlEnum,
9+
mysqlTable,
10+
text,
11+
timestamp,
12+
varchar
13+
} from 'drizzle-orm/mysql-core'
14+
15+
export const Users = mysqlTable('users', {
16+
id: int('id').autoincrement().primaryKey(),
17+
name: text('name').notNull(),
18+
email: text('email'),
19+
bigint: bigint('big_int', { mode: 'bigint', unsigned: true }),
20+
birthdayString: date('birthday_string', { mode: 'string' }),
21+
birthdayDate: date('birthday_date', { mode: 'date' }),
22+
createdAt: timestamp('created_at').notNull().defaultNow(),
23+
role: mysqlEnum('role', ['admin', 'user']),
24+
roleText: text('role1', { enum: ['admin', 'user'] }),
25+
roleText2: text('role2', { enum: ['admin', 'user'] }).default('user'),
26+
profession: varchar('profession', { length: 20 }),
27+
initials: char('initials', { length: 2 }),
28+
isConfirmed: boolean('is_confirmed')
29+
})
30+
31+
export const Customers = mysqlTable('customers', {
32+
id: int('id').autoincrement().primaryKey(),
33+
address: text('address').notNull(),
34+
isConfirmed: boolean('is_confirmed'),
35+
registrationDate: timestamp('registration_date').notNull().defaultNow(),
36+
userId: int('user_id')
37+
.references(() => Users.id)
38+
.notNull()
39+
})
40+
41+
export const usersRelations = relations(Users, ({ many }) => ({
42+
posts: many(Posts)
43+
}))
44+
45+
export const Posts = mysqlTable('posts', {
46+
id: int('id').autoincrement().primaryKey(),
47+
content: text('content'),
48+
authorId: int('author_id')
49+
})
50+
51+
export const postsRelations = relations(Posts, ({ one }) => ({
52+
author: one(Users, {
53+
fields: [Posts.authorId],
54+
references: [Users.id]
55+
})
56+
}))

Tests/Schema/pg.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { relations } from 'drizzle-orm'
2+
import { boolean, char, date, integer, pgEnum, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core'
3+
4+
export const roleEnum = pgEnum('role', ['admin', 'user'])
5+
6+
export const Users = pgTable('users', {
7+
a: integer('a').array(),
8+
id: serial('id').primaryKey(),
9+
name: text('name').notNull(),
10+
email: text('email'),
11+
birthdayString: date('birthday_string', { mode: 'string' }),
12+
birthdayDate: date('birthday_date', { mode: 'date' }),
13+
createdAt: timestamp('created_at').notNull().defaultNow(),
14+
role: roleEnum('role'),
15+
roleText: text('role1', { enum: ['admin', 'user'] }),
16+
roleText2: text('role2', { enum: ['admin', 'user'] }).default('user'),
17+
profession: varchar('profession', { length: 20 }),
18+
initials: char('initials', { length: 2 }),
19+
isConfirmed: boolean('is_confirmed')
20+
})
21+
22+
export const Customers = pgTable('customers', {
23+
id: serial('id').primaryKey(),
24+
address: text('address').notNull(),
25+
isConfirmed: boolean('is_confirmed'),
26+
registrationDate: timestamp('registration_date').notNull().defaultNow(),
27+
userId: integer('user_id')
28+
.references(() => Users.id)
29+
.notNull()
30+
})
31+
32+
export const usersRelations = relations(Users, ({ many }) => ({
33+
posts: many(Posts)
34+
}))
35+
36+
export const Posts = pgTable('posts', {
37+
id: serial('id').primaryKey(),
38+
content: text('content'),
39+
authorId: integer('author_id')
40+
})
41+
42+
export const postsRelations = relations(Posts, ({ one }) => ({
43+
author: one(Users, {
44+
fields: [Posts.authorId],
45+
references: [Users.id]
46+
})
47+
}))

Tests/Schema/sqlite.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { relations } from 'drizzle-orm'
2+
import { blob, integer, numeric, real, sqliteTable, text } from 'drizzle-orm/sqlite-core'
3+
4+
export const Users = sqliteTable('users', {
5+
id: integer('id').primaryKey().notNull(),
6+
name: text('name').notNull(),
7+
email: text('email'),
8+
textJson: text('text_json', { mode: 'json' }),
9+
blobBigInt: blob('blob_bigint', { mode: 'bigint' }),
10+
numeric: numeric('numeric'),
11+
createdAt: integer('created_at', { mode: 'timestamp' }),
12+
createdAtMs: integer('created_at_ms', { mode: 'timestamp_ms' }),
13+
real: real('real'),
14+
text: text('text', { length: 255 }),
15+
role: text('role', { enum: ['admin', 'user'] }).default('user'),
16+
isConfirmed: integer('is_confirmed', {
17+
mode: 'boolean'
18+
})
19+
})
20+
21+
export const Customers = sqliteTable('customers', {
22+
id: integer('id').primaryKey(),
23+
address: text('address').notNull(),
24+
isConfirmed: integer('is_confirmed', { mode: 'boolean' }),
25+
registrationDate: integer('registration_date', { mode: 'timestamp_ms' })
26+
.notNull()
27+
.$defaultFn(() => new Date()),
28+
userId: integer('user_id')
29+
.references(() => Users.id)
30+
.notNull()
31+
})
32+
33+
export const usersRelations = relations(Users, ({ many }) => ({
34+
posts: many(Posts)
35+
}))
36+
37+
export const Posts = sqliteTable('posts', {
38+
id: integer('id').primaryKey(),
39+
content: text('content'),
40+
authorId: integer('author_id')
41+
})
42+
43+
export const postsRelations = relations(Posts, ({ one }) => ({
44+
author: one(Users, {
45+
fields: [Posts.authorId],
46+
references: [Users.id]
47+
})
48+
}))

Tests/Util/query/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import axios, { AxiosError } from 'axios'
2+
3+
export class GraphQLClient {
4+
constructor(private url: string) {}
5+
6+
public queryGql = async (query: string) => {
7+
try {
8+
const res = await axios.post(
9+
this.url,
10+
JSON.stringify({
11+
query: query,
12+
variables: {}
13+
}),
14+
{
15+
headers: {
16+
accept: 'application/graphql-response+json, application/json',
17+
'content-type': 'application/json'
18+
}
19+
}
20+
)
21+
22+
return res.data
23+
} catch (e) {
24+
const err = e as AxiosError<any>
25+
26+
console.warn(err.status, err.response?.data.errors)
27+
return err.response?.data
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)