Skip to content

Commit 91746db

Browse files
committed
initial
0 parents  commit 91746db

File tree

4 files changed

+4392
-0
lines changed

4 files changed

+4392
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { GraphQLServer } from "graphql-yoga";
2+
3+
const Users = [
4+
{
5+
id: 1,
6+
username: "john",
7+
city: "Melbourne",
8+
},
9+
{
10+
id: 2,
11+
username: "mseven",
12+
city: "Istanbul",
13+
},
14+
{
15+
id: 3,
16+
username: "maria",
17+
city: "Zagreb",
18+
},
19+
];
20+
const Posts = [
21+
{
22+
id: 1,
23+
title:
24+
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
25+
userId: 1,
26+
},
27+
{
28+
id: 2,
29+
title:
30+
"Lorem Ipsum je jednostavno probni tekst koji se koristi u tiskarskoj i slovoslagarskoj industriji.",
31+
userId: 3,
32+
},
33+
{
34+
id: 3,
35+
title:
36+
"Lorem Ipsum, dizgi ve baskı endüstrisinde kullanılan mıgır metinlerdir.",
37+
userId: 2,
38+
},
39+
{
40+
id: 4,
41+
title:
42+
"Lorem Ipsum, dizgi ve baskı endüstrisinde kullanılan mıgır metinlerdir22222.",
43+
userId: 1,
44+
},
45+
];
46+
47+
const typeDefs = `
48+
type Query {
49+
hello: String
50+
users: [User!]!
51+
posts: [Post!]!
52+
user(id: ID!): User!
53+
post(id:ID!): Post!
54+
}
55+
56+
type Mutation{
57+
addUser(id:ID!, username:String!, city:String! ): User
58+
}
59+
60+
type User{
61+
id: ID!
62+
username: String!
63+
city: String
64+
posts: [Post!]
65+
}
66+
67+
type Post{
68+
id: ID!
69+
title: String!
70+
userId: ID!
71+
user: User!
72+
}
73+
`;
74+
const resolvers = {
75+
Query: {
76+
user: (parent, args) => Users.find((user) => String(user.id) === args.id),
77+
users: (parent, args) => Users,
78+
79+
post: (parent, args) => Posts.find((post) => String(post.id) === args.id),
80+
posts: (parent, args) => Posts,
81+
},
82+
Post: {
83+
user: (parent, args) => Users.find((user) => user.id === parent.userId),
84+
},
85+
User: {
86+
posts: (parent, args) => Posts.filter((post) => parent.id === post.userId),
87+
},
88+
Mutation: {
89+
addUser: (_, { id, username, city }) => {
90+
const newUser = {
91+
id: id,
92+
username: username,
93+
city: city,
94+
};
95+
Users.push(newUser);
96+
return newUser;
97+
},
98+
},
99+
};
100+
101+
const server = new GraphQLServer({ typeDefs, resolvers });
102+
103+
server.start({ port: 4001 }, () =>
104+
console.log("Server is running on localhost:4001")
105+
);

0 commit comments

Comments
 (0)