-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (134 loc) · 3.14 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { ApolloServer, gql } = require('apollo-server')
const perfis = [
{id: 1, nome: 'comum'},
{id: 2, nome: 'administrator'}
]
const usuarios = [{
id : 1,
nome : "First",
email : "first@gmail.com",
idade : 20,
perfil_id: 1
},
{
id : 2,
nome : "Second",
email : "second@gmail.com",
idade : 18,
perfil_id: 2
},
{
id : 3,
nome : "other",
email : "other@gmail.com",
idade : 48,
perfil_id: 1
}]
const typeDefs = gql`
scalar Date
type Usuario {
id: Int
nome: String!
email: String!
idade: Int
salario: Float
vip: Boolean
perfil: Perfil
}
type Perfil {
id: Int,
nome: String!
}
type Produto {
nome: String!
preco: Float!
desconto: Float
precoComDesconto: Float!
}
# Pontos de entrada da API!
type Query {
ola : String
horaCerta : Date
usuarioLogado : Usuario
produtoEmDestaque : Produto
numerosMegaSena : [Int!]!
usuarios : [Usuario]
usuario(id: Int): Usuario
perfis: [Perfil]
perfil(id: Int): Perfil
}
`
const resolvers = {
Usuario: {
salario(usuario) {
return usuario.salario_real
},
perfil(usuario) {
const selct = perfis
.filter(u => u.id === usuario.perfil_id)
return selct ? selct[0] : null
}
},
Produto: {
precoComDesconto(produto) {
if (produto.desconto)
return produto.preco * (1 - produto.desconto)
return produto.preco
}
},
Query: {
ola() {
return 'String de retorno'
},
horaCerta() {
return new Date
},
usuarioLogado(){
return {
id: 1,
nome: "Administrador",
email: "admin@gmail.com",
idade: 47,
salario_real: 1234.98,
vip: true
}
},
produtoEmDestaque() {
return {
nome: "Camisa Oficial do Ibis FC 2020",
preco: 8997.00,
desconto: 0.50
}
},
numerosMegaSena() {
// return [4, 6, 123, 34, 6, 523, 23, 652, 27, 7, 55, 245]
const ASC = (a , b) => a - b
return Array(6).fill(0)
.map(() => parseInt(Math.random() * 60 + 1))
.sort(ASC)
},
usuarios() {
return usuarios
},
usuario(_, { id }) {
const selct = usuarios
.filter(u => u.id === id)
return selct ? selct[0] : null
},
perfis() {
return perfis
},
perfil(_, { id }) {
const selct = perfis
.filter(p => p.id === id)
return selct ? selct[0] : null
}
}
}
const server = new ApolloServer({
typeDefs,
resolvers
})
server.listen().then(({url}) => {
console.log(`Executando em ${url}`)
})