-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (89 loc) · 2.08 KB
/
app.js
File metadata and controls
107 lines (89 loc) · 2.08 KB
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
106
107
import { GraphQLServer } from 'graphql-yoga'
// ... or using `require()`
// const { GraphQLServer } = require('graphql-yoga')
// Yapıyı https://github.com/dotansimha/graphql-yoga buradan hazır olarak alıyoruz.
// User için sahte veri.
const Users = [
{
id: 1,
username: "John",
city: 'Merlbourne'
},
{
id: 2,
username: "mseven",
city: 'Istanbul'
},
{
id: 3,
username: "maria",
city: 'Zagreb'
},
]
// Post için sahte veri.
const Posts = [
{
id: 1,
title: "Lorem 1",
userId: 1
},
{
id: 2,
title: "Lorem 2",
userId: 1
},
{
id: 3,
title: "Lorem 3",
userId: 2
},
{
id: 3,
title: "New Post",
userId: 1
},
]
// Burada sorguda gelecek tanımları yazıyoruz.
const typeDefs = `
type Query {
user(id: ID!): User!
post(id: ID!): Post!
users: [User!]!
posts: [Post!]!
}
type User{
id:ID!
username: String!
city: String
posts:[Post!]!
}
type Post{
id:ID!
title: String!
userId: String
user:User!
}
`;
// Burada sorguya karşılık gelecek işlemleri yapıyoruz.
const resolvers = {
Query: {
// Tek bir user nesnesi için.
user: (parent, args) => Users.find(user => String(user.id) === args.id),
// Tek bir post nesnesi için.
post: (parent, args) => Posts.find(post => String(post.id) === args.id),
// Userların tümü için.
users: (parent) => Users,
// Postların tümü için.
posts: (parent) => Posts,
},
// Post sorgusu içerisinde user döndürmek için.
Post: {
user: (parent) => Users.find(user => user.id == parent.userId)
},
// User sorgusu içerisinde postları döndürmek için.
User: {
posts: (parent) => Posts.filter(post => post.userId == parent.id)
}
}
const server = new GraphQLServer({ typeDefs, resolvers })
server.start(() => console.log('Server is running on localhost:4000'))