-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (97 loc) · 2.01 KB
/
index.js
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
103
104
105
import { GraphQLServer } from "graphql-yoga";
const Users = [
{
id: 1,
username: "john",
city: "Melbourne",
},
{
id: 2,
username: "mseven",
city: "Istanbul",
},
{
id: 3,
username: "maria",
city: "Zagreb",
},
];
const Posts = [
{
id: 1,
title:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
userId: 1,
},
{
id: 2,
title:
"Lorem Ipsum je jednostavno probni tekst koji se koristi u tiskarskoj i slovoslagarskoj industriji.",
userId: 3,
},
{
id: 3,
title:
"Lorem Ipsum, dizgi ve baskı endüstrisinde kullanılan mıgır metinlerdir.",
userId: 2,
},
{
id: 4,
title:
"Lorem Ipsum, dizgi ve baskı endüstrisinde kullanılan mıgır metinlerdir22222.",
userId: 1,
},
];
const typeDefs = `
type Query {
hello: String
users: [User!]!
posts: [Post!]!
user(id: ID!): User!
post(id:ID!): Post!
}
type Mutation{
addUser(id:ID!, username:String!, city:String! ): User
}
type User{
id: ID!
username: String!
city: String
posts: [Post!]
}
type Post{
id: ID!
title: String!
userId: ID!
user: User!
}
`;
const resolvers = {
Query: {
user: (parent, args) => Users.find((user) => String(user.id) === args.id),
users: (parent, args) => Users,
post: (parent, args) => Posts.find((post) => String(post.id) === args.id),
posts: (parent, args) => Posts,
},
Post: {
user: (parent, args) => Users.find((user) => user.id === parent.userId),
},
User: {
posts: (parent, args) => Posts.filter((post) => parent.id === post.userId),
},
Mutation: {
addUser: (_, { id, username, city }) => {
const newUser = {
id: id,
username: username,
city: city,
};
Users.push(newUser);
return newUser;
},
},
};
const server = new GraphQLServer({ typeDefs, resolvers });
server.start({ port: 4000 }, () =>
console.log("Server is running on http://localhost:4000")
);