Skip to content

Commit ff4eb3e

Browse files
feat: add comments and posts functionality with GraphQL resolvers and schema definitions
1 parent 095f7e6 commit ff4eb3e

File tree

16 files changed

+362
-287
lines changed

16 files changed

+362
-287
lines changed

README.md

Lines changed: 254 additions & 147 deletions
Large diffs are not rendered by default.

diff.txt

Lines changed: 0 additions & 51 deletions
This file was deleted.

playground/server/graphql/comments/comment-resolver.ts renamed to playground/server/graphql/comments/add-comment.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
export default createResolver({
2-
Query: {
3-
comments: async (parent, { postId }, context) => {
4-
return [
5-
{ id: '1', content: 'Great post!', postId, authorId: '1' },
6-
{ id: '2', content: 'Nice work!', postId, authorId: '2' },
7-
]
8-
},
9-
},
1+
import { createResolver } from 'nitro-graphql-yoga'
102

3+
export default createResolver({
114
Mutation: {
125
addComment: async (parent, { input }, context) => {
136
return {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createResolver } from 'nitro-graphql-yoga'
2+
3+
export default createResolver({
4+
Query: {
5+
comments: async (parent, { postId }, context) => {
6+
return [
7+
{ id: '1', content: 'Great post!', postId, authorId: '1' },
8+
{ id: '2', content: 'Nice work!', postId, authorId: '2' },
9+
]
10+
},
11+
},
12+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Comment {
2+
id: ID!
3+
content: String!
4+
postId: ID!
5+
authorId: ID!
6+
}
7+
8+
input AddCommentInput {
9+
content: String!
10+
postId: ID!
11+
authorId: ID!
12+
}
13+
14+
extend type Query {
15+
comments(postId: ID!): [Comment!]!
16+
}
17+
18+
extend type Mutation {
19+
addComment(input: AddCommentInput!): Comment!
20+
}

playground/server/graphql/resolvers/queries/hello.ts renamed to playground/server/graphql/hello.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createResolver } from 'nitro-graphql-yoga'
2+
13
export default createResolver({
24
Query: {
35
hello: () => 'Hello from auto-discovered resolver!',
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createResolver } from 'nitro-graphql-yoga'
2+
3+
export default createResolver({
4+
Mutation: {
5+
createPost: async (parent, { input }, context) => {
6+
return {
7+
id: Date.now().toString(),
8+
title: input.title,
9+
content: input.content,
10+
authorId: input.authorId,
11+
}
12+
},
13+
},
14+
})
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createResolver } from 'nitro-graphql-yoga'
2+
13
export default createResolver({
24
Query: {
35
post: async (parent, { id }, context) => {
@@ -16,15 +18,4 @@ export default createResolver({
1618
]
1719
},
1820
},
19-
20-
Mutation: {
21-
createPost: async (parent, { input }, context) => {
22-
return {
23-
id: Date.now().toString(),
24-
title: input.title,
25-
content: input.content,
26-
authorId: input.authorId,
27-
}
28-
},
29-
},
3021
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
type Post {
2+
id: ID!
3+
title: String!
4+
content: String!
5+
authorId: ID!
6+
}
7+
8+
input CreatePostInput {
9+
title: String!
10+
content: String!
11+
authorId: ID!
12+
}
13+
14+
extend type Query {
15+
post(id: ID!): Post
16+
posts: [Post!]!
17+
}
18+
19+
extend type Mutation {
20+
createPost(input: CreatePostInput!): Post!
21+
}

playground/server/graphql/schema.graphql

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,6 @@ scalar JSON
44
type Query {
55
hello: String!
66
greeting(name: String!): String!
7-
users: [User!]!
8-
user(id: ID!): User
9-
todos: [Todo!]!
10-
todo(id: ID!): Todo
11-
post(id: ID!): Post
12-
posts: [Post!]!
13-
comments(postId: ID!): [Comment!]!
147
}
158

16-
type Todo {
17-
id: ID!
18-
title: String!
19-
completed: Boolean!
20-
createdAt: DateTime!
21-
}
22-
23-
type User {
24-
id: ID!
25-
name: String!
26-
email: String!
27-
createdAt: DateTime!
28-
}
29-
30-
input CreateUserInput {
31-
name: String!
32-
email: String!
33-
}
34-
35-
type Post {
36-
id: ID!
37-
title: String!
38-
content: String!
39-
authorId: ID!
40-
}
41-
42-
type Comment {
43-
id: ID!
44-
content: String!
45-
postId: ID!
46-
authorId: ID!
47-
}
48-
49-
input CreatePostInput {
50-
title: String!
51-
content: String!
52-
authorId: ID!
53-
}
54-
55-
input AddCommentInput {
56-
content: String!
57-
postId: ID!
58-
authorId: ID!
59-
}
60-
61-
type Mutation {
62-
addTodo(title: String!): Todo!
63-
toggleTodo(id: ID!): Todo!
64-
deleteTodo(id: ID!): Boolean!
65-
createUser(input: CreateUserInput!): User!
66-
createPost(input: CreatePostInput!): Post!
67-
addComment(input: AddCommentInput!): Comment!
68-
}
9+
type Mutation

0 commit comments

Comments
 (0)