-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.ts
102 lines (86 loc) · 1.83 KB
/
graphql.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
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
import {
buildSchema,
GraphQLError,
GraphQLSchema,
ExecutionResult
} from 'graphql'
import {
GraphQLServer,
JsonLogger
} from '@dreamit/graphql-server'
type Data = {
name: string
}
interface User {
userId: string
userName: string
}
interface LogoutResult {
result: string
}
export const userOne: User = {userId: '1', userName:'UserOne'}
export const userTwo: User = {userId: '2', userName:'UserTwo'}
const userSchema = buildSchema(`
schema {
query: Query
mutation: Mutation
}
type Query {
returnError: User
users: [User]
user(id: String!): User
}
type Mutation {
login(userName: String, password: String): LoginData
logout: LogoutResult
}
type User {
userId: String
userName: String
}
type LoginData {
jwt: String
}
type LogoutResult {
result: String
}
`)
const userSchemaResolvers= {
returnError(): User {
throw new GraphQLError('Something went wrong!', {})
},
users(): User[] {
return [userOne, userTwo]
},
user(input: { id: string }): User {
switch (input.id) {
case '1': {
return userOne
}
case '2': {
return userTwo
}
default: {
throw new GraphQLError(`User for userid=${input.id} was not found`, {})
}
}
},
logout(): LogoutResult {
return {result: 'Goodbye!'}
}
}
const graphqlServer = new GraphQLServer(
{
schema: userSchema,
rootValue: userSchemaResolvers,
logger: new JsonLogger('fastifyServer', 'user-service')
}
)
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ExecutionResult>
) {
await graphqlServer.handleRequest(req, res)
}