-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
65 lines (59 loc) · 1.54 KB
/
app.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
const { GraphQLServer } = require('graphql-yoga')
const fetch = require('node-fetch')
const typeDefs = `
type Query {
hello(name: String): String!
getPosts(id: Int): Post
getUser(userId: Int): User
}
type User {
id: Int,
name: String
username: String
email: String
phone: String
website: String
posts: [Post]
}
type Post {
userId: User
id: Int
title: String
body: String
}
`
const resolvers = {
User: {
posts: async parent => {
const response = await fetch(
`http://jsonplaceholder.typicode.com/posts?userId=${parent.id}`
)
return response.json()
}
},
Post: {
userId: async parent => {
const reponse = await fetch(
`http://jsonplaceholder.typicode.com/users/${parent.userId}`
)
return reponse.json()
}
},
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}`,
getPosts: async (_, { id }) => {
const response = await fetch(
`http://jsonplaceholder.typicode.com/posts/${id}`
)
return response.json()
},
getUser: async (_, { userId }) => {
const response = await fetch(
`http://jsonplaceholder.typicode.com/users/${userId}`
)
return response.json()
}
}
}
const server = new GraphQLServer({ typeDefs, resolvers })
server.start(() => console.log('Server is running on localhost:4000'))