Skip to content

Commit a4f614b

Browse files
authored
Merge pull request #29 from OrcaPracticas/add/Mutation
Busqueda y manejo de errores
2 parents 50929e7 + 0e0653b commit a4f614b

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/schemas/resolvers/BuildMutation.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class BuildMutation {
2626
.then(data => this.modelo
2727
.query()
2828
.deleteById(...this.params)
29-
.then(() => data))
29+
.then((filas) => {
30+
if (filas > 0) return data;
31+
throw new Error(`El registro con el id ${this.params[0]} no se puede elmiar`);
32+
}))
3033
);
3134
}
3235

src/schemas/resolvers/resolver.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ const RESOLVERS = {
1414
comentarios: () => ModeloComentario.query().eager(),
1515
curso: (rootValue, args) => ModeloCurso.query().findById(args.id),
1616
profesor: (rootValue, args) => ModeloProfesor.query().findById(args.id),
17+
buscar: (_, args) => {
18+
return [
19+
ModeloProfesor.query().findById(3),
20+
ModeloCurso.query().findById(2),
21+
];
22+
},
23+
},
24+
ResultadoBusqueda: {
25+
__resolveType: object => ((object.nombre) ? "Profesor" : "Curso"),
1726
},
1827
Mutation: {
1928
profesorAdd: (_, args) => BuildMutation.query(ModeloProfesor, args).save,

src/schemas/schema.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import { Curso, Profesor } from "./schemas";
88
// Creacion del schema, es importante declarar
99
// el Query root ya que este indica el endpoint
1010
const ROOT_QUERY = `
11+
# **Busqueda
12+
union ResultadoBusqueda = Profesor | Curso
13+
1114
# **Root type Query** _endpoint_ principal
1215
type Query {
1316
cursos: [Curso]
1417
profesores: [Profesor]
1518
comentarios: [Comentario]
1619
curso(id: Int): Curso
1720
profesor(id: Int): Profesor
21+
buscar(query: String!): [ResultadoBusqueda]
1822
}
1923
2024
type Mutation {
@@ -27,7 +31,6 @@ const ROOT_QUERY = `
2731
comentarioAdd(comentario: newComentario): Comentario
2832
comentarioEdit(id: Int!, comentario: editComentario): Comentario
2933
comentarioDelete(id: Int!): Comentario
30-
3134
}
3235
`;
3336

src/server.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const PORT = 8080;
1414
SERVER.use(
1515
"/gql",
1616
BodyParser.json(),
17-
graphqlExpress({ schema: Schema }),
17+
graphqlExpress({
18+
schema: Schema,
19+
formatError: error => ({
20+
errorCode: "A55",
21+
name: error.name,
22+
mensaje: error.message,
23+
}),
24+
}),
1825
);
1926

2027
/**

0 commit comments

Comments
 (0)