@@ -7,180 +7,180 @@ import _ from 'underscore.ts';
77const app = new Application ( ) ;
88
99app . use ( async ( ctx , next ) => {
10- await next ( ) ;
11- const rt = ctx . response . headers . get ( 'X-Response-Time' ) ;
12- console . log ( `${ ctx . request . method } ${ ctx . request . url } - ${ rt } ` ) ;
10+ await next ( ) ;
11+ const rt = ctx . response . headers . get ( 'X-Response-Time' ) ;
12+ console . log ( `${ ctx . request . method } ${ ctx . request . url } - ${ rt } ` ) ;
1313} ) ;
1414
1515app . use ( async ( ctx , next ) => {
16- const start = Date . now ( ) ;
17- await next ( ) ;
18- const ms = Date . now ( ) - start ;
19- ctx . response . headers . set ( 'X-Response-Time' , `${ ms } ms` ) ;
16+ const start = Date . now ( ) ;
17+ await next ( ) ;
18+ const ms = Date . now ( ) - start ;
19+ ctx . response . headers . set ( 'X-Response-Time' , `${ ms } ms` ) ;
2020} ) ;
2121
2222const typeDefs = gql `
23- input CreateUserInput {
24- name: String!
25- email: String!
26- age: Int
27- }
28-
29- type User {
30- id: ID!
31- name: String!
32- email: String!
33- age: Int
34- }
35-
36- type Post {
37- id: ID!
38- title: String!
39- body: String!
40- published: Boolean!
41- author: User
42- }
43-
44- type SuccessOrError {
45- success: Boolean!
46- message: String
47- }
48-
49- type Query {
50- testInput(input: CreateUserInput!): User
51- me: User!
52- user(id: ID!): User
53- users: [User]!
54- post(id: ID!): Post
55- posts(query: String): [Post!]!
56- grades: [Int!]!
57- add(nums: [Int!]!): Int!
58- }
59-
60- type Mutation {
61- createUser(name: String!, email: String!, age: Int): User!
62- deleteUser(id: ID!): SuccessOrError!
63- createPost(title: String!, body: String!, published: Boolean!): Post!
64- }
23+ input CreateUserInput {
24+ name: String!
25+ email: String!
26+ age: Int
27+ }
28+
29+ type User {
30+ id: ID!
31+ name: String!
32+ email: String!
33+ age: Int
34+ }
35+
36+ type Post {
37+ id: ID!
38+ title: String!
39+ body: String!
40+ published: Boolean!
41+ author: User
42+ }
43+
44+ type SuccessOrError {
45+ success: Boolean!
46+ message: String
47+ }
48+
49+ type Query {
50+ testInput(input: CreateUserInput!): User
51+ me: User!
52+ user(id: ID!): User
53+ users: [User]!
54+ post(id: ID!): Post
55+ posts(query: String): [Post!]!
56+ grades: [Int!]!
57+ add(nums: [Int!]!): Int!
58+ }
59+
60+ type Mutation {
61+ createUser(name: String!, email: String!, age: Int): User!
62+ deleteUser(id: ID!): SuccessOrError!
63+ createPost(title: String!, body: String!, published: Boolean!): Post!
64+ }
6565` ;
6666
6767// console.log(JSON.stringify(typeDefs, null, 2));
6868
6969const resolvers = {
70- Query : {
71- testInput : async (
72- parent : any ,
73- { input } : { input : { name : string ; email : string ; age ?: number } } ,
74- context : any ,
75- info : any
76- ) => {
77- return { id : 'wtf' , ...input } ;
70+ Query : {
71+ testInput : async (
72+ parent : any ,
73+ { input } : { input : { name : string ; email : string ; age ?: number } } ,
74+ context : any ,
75+ info : any ,
76+ ) => {
77+ return { id : 'wtf' , ...input } ;
78+ } ,
79+ grades : async ( parent : any , args : any , context : any , info : any ) => {
80+ return [ 1 , 2 , 3 , 4 , 5 ] ;
81+ } ,
82+ add : async (
83+ parent : any ,
84+ { nums } : { nums : number [ ] } ,
85+ context : any ,
86+ info : any ,
87+ ) => {
88+ return nums . reduce ( ( a , b ) => a + b , 0 ) ;
89+ } ,
90+ me : async ( parent : any , args : any , context : any , info : any ) => {
91+ return {
92+ id : 'jdillick' ,
93+ name : 'John' ,
94+ email : 'john@dillick.us' ,
95+ } ;
96+ } ,
97+
98+ user : async ( parent : any , { id } : any , context : any , info : any ) => {
99+ const user = await User . load ( id ) ;
100+ return user ;
101+ } ,
102+
103+ users : async ( parent : any , args : any , context : any , info : any ) => {
104+ // await new Promise((resolve) => setTimeout(resolve, 5000));
105+ const users = User . find ( ) ;
106+ return users ;
107+ } ,
108+
109+ post : async (
110+ parent : any ,
111+ { id } : { id : number } ,
112+ context : any ,
113+ info : any ,
114+ ) => {
115+ console . log ( { id } ) ;
116+ return {
117+ id : 123 ,
118+ title : 'Hello World' ,
119+ body : 'This is a post.' ,
120+ published : true ,
121+ } ;
122+ } ,
123+
124+ posts : async ( parent : any , { query } : any , context : any , info : any ) => {
125+ if ( query ) {
126+ return Post . find ( { title : { $regex : query , $options : 'i' } } ) ;
127+ }
128+ return Post . find ( ) ;
129+ } ,
78130 } ,
79- grades : async ( parent : any , args : any , context : any , info : any ) => {
80- return [ 1 , 2 , 3 , 4 , 5 ] ;
131+ Mutation : {
132+ createUser : async (
133+ parent : any ,
134+ { name, email, age } : { name : string ; email : string ; age ?: number } ,
135+ context : any ,
136+ info : any ,
137+ ) => {
138+ const user = new User ( { name, email, age } ) ;
139+ await user . save ( ) ;
140+ return user ;
141+ } ,
142+ deleteUser : async (
143+ parent : any ,
144+ { id } : { id : string } ,
145+ context : any ,
146+ info : any ,
147+ ) => {
148+ try {
149+ const user = await User . delete ( id ) ;
150+ return { success : true } ;
151+ } catch ( e ) {
152+ return { success : false , message : e . message } ;
153+ }
154+ } ,
155+ createPost : async (
156+ parent : any ,
157+ {
158+ title,
159+ body,
160+ published,
161+ } : {
162+ title : string ;
163+ body : string ;
164+ published : boolean ;
165+ } ,
166+ context : any ,
167+ info : any ,
168+ ) => {
169+ const post = new Post ( { title, body, published } ) ;
170+ await post . save ( ) ;
171+ return post ;
172+ } ,
81173 } ,
82- add : async (
83- parent : any ,
84- { nums } : { nums : number [ ] } ,
85- context : any ,
86- info : any
87- ) => {
88- return nums . reduce ( ( a , b ) => a + b , 0 ) ;
89- } ,
90- me : async ( parent : any , args : any , context : any , info : any ) => {
91- return {
92- id : 'jdillick' ,
93- name : 'John' ,
94- email : 'john@dillick.us' ,
95- } ;
96- } ,
97-
98- user : async ( parent : any , { id } : any , context : any , info : any ) => {
99- const user = await User . load ( id ) ;
100- return user ;
101- } ,
102-
103- users : async ( parent : any , args : any , context : any , info : any ) => {
104- // await new Promise((resolve) => setTimeout(resolve, 5000));
105- const users = User . find ( ) ;
106- return users ;
107- } ,
108-
109- post : async (
110- parent : any ,
111- { id } : { id : number } ,
112- context : any ,
113- info : any
114- ) => {
115- console . log ( { id } ) ;
116- return {
117- id : 123 ,
118- title : 'Hello World' ,
119- body : 'This is a post.' ,
120- published : true ,
121- } ;
122- } ,
123-
124- posts : async ( parent : any , { query } : any , context : any , info : any ) => {
125- if ( query ) {
126- return Post . find ( { title : { $regex : query , $options : 'i' } } ) ;
127- }
128- return Post . find ( ) ;
129- } ,
130- } ,
131- Mutation : {
132- createUser : async (
133- parent : any ,
134- { name, email, age } : { name : string ; email : string ; age ?: number } ,
135- context : any ,
136- info : any
137- ) => {
138- const user = new User ( { name, email, age } ) ;
139- await user . save ( ) ;
140- return user ;
141- } ,
142- deleteUser : async (
143- parent : any ,
144- { id } : { id : string } ,
145- context : any ,
146- info : any
147- ) => {
148- try {
149- const user = await User . delete ( id ) ;
150- return { success : true } ;
151- } catch ( e ) {
152- return { success : false , message : e . message } ;
153- }
154- } ,
155- createPost : async (
156- parent : any ,
157- {
158- title,
159- body,
160- published,
161- } : {
162- title : string ;
163- body : string ;
164- published : boolean ;
165- } ,
166- context : any ,
167- info : any
168- ) => {
169- const post = new Post ( { title, body, published } ) ;
170- await post . save ( ) ;
171- return post ;
172- } ,
173- } ,
174174} ;
175175
176176const GraphQLService = await applyGraphQL < Router > ( {
177- Router,
178- typeDefs,
179- resolvers,
180- context : ( ctx ) => {
181- // this line is for passing a user context for the auth
182- return { user : 'Boo' } ;
183- } ,
177+ Router,
178+ typeDefs,
179+ resolvers,
180+ context : ( ctx ) => {
181+ // this line is for passing a user context for the auth
182+ return { user : 'Boo' } ;
183+ } ,
184184} ) ;
185185
186186app . use ( GraphQLService . routes ( ) , GraphQLService . allowedMethods ( ) ) ;
0 commit comments