-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (71 loc) · 2.45 KB
/
index.js
File metadata and controls
85 lines (71 loc) · 2.45 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
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import gql from 'graphql-tag';
import fs from 'fs';
// Datos de ejemplo para las personas en la red de distribución
const network = [
{ id: '01', name: 'Diamante' , sponsorId: null },
{ id: '02', name: 'Esperalda 1' , sponsorId: '01' },
{ id: '03', name: 'Esmeralda 2' , sponsorId: '01' },
{ id: '04', name: 'Esmeralda 3' , sponsorId: '01' },
{ id: '05', name: 'Plata 1' , sponsorId: '02' },
{ id: '06', name: 'Plata 2' , sponsorId: '02' },
{ id: '07', name: 'Plata 3' , sponsorId: '03' },
{ id: '08', name: 'Plata 4' , sponsorId: '03' },
{ id: '09', name: 'Plata 5' , sponsorId: '04' },
{ id: '10', name: 'Plata 6' , sponsorId: '04' },
{ id: '11', name: 'Nuevo 1' , sponsorId: '05' },
{ id: '12', name: 'Nuevo 2' , sponsorId: '05' },
{ id: '13', name: 'Nuevo 3' , sponsorId: '05' },
{ id: '14', name: 'Nuevo 4' , sponsorId: '05' },
{ id: '15', name: 'Nuevo 5' , sponsorId: '05' },
{ id: '16', name: 'Nuevo 6' , sponsorId: '06' },
{ id: '17', name: 'Nuevo 7' , sponsorId: '07' },
{ id: '18', name: 'Nuevo 8' , sponsorId: '08' },
{ id: '19', name: 'Nuevo 9' , sponsorId: '09' },
{ id: '20', name: 'Nuevo 10' , sponsorId: '10' },
];
// Definición de los tipos y consultas GraphQL utilizando el lenguaje de esquema GraphQL (SDL)
const typeDefs = gql`
type Person {
id: ID!
name: String!
sponsor: Person
recruits: [Person]
}
type Query {
person(id: ID!): Person
network: [Person]
}
type Mutation {
addPerson(name: String!, sponsorId: ID): Person
}
`;
// Resolvers para las consultas y mutaciones de GraphQL
const resolvers = {
Query: {
person: (_, { id }) => network.find(person => person.id === id),
network: () => network,
},
Mutation: {
addPerson: (_, { name, sponsorId }) => {
const newPerson = {
id: String(network.length + 1),
name,
sponsorId,
};
network.push(newPerson);
return newPerson;
},
},
Person: {
sponsor: person => network.find(p => p.id === person.sponsorId),
recruits: person => network.filter(p => p.sponsorId === person.id),
},
};
async function startServer() {
const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server);
console.log(`🚀🚀🚀 Servidor en Línea en el Puerto ==> ${url}`);
}
startServer();