Commit 4efc14b7 authored by Sonia Vidal Gonzalez's avatar Sonia Vidal Gonzalez

ok

parent 3b60395c
Pipeline #5461 failed with stages
in 1 second
This source diff could not be displayed because it is too large. You can view the blob instead.
const fetch = require("node-fetch"); const fetch = require("node-fetch");
const { ApolloServer, gql } = require("apollo-server"); const { ApolloServer, gql } = require("apollo-server");
// This is a (sample) collection of books we'll be able to query // Esta es una colección (de muestra) de libros que podremos consultar
// the GraphQL server for. A more complete example might fetch // el servidor GraphQL para. Un ejemplo más completo podría recuperar
// from an existing data source like a REST API or database. // de una fuente de datos existente como una API REST o una base de datos.
const books = [ const books = [
{ {
title: "Harry Potter and the Chamber of Secrets", title: "Harry Potter and the Chamber of Secrets",
...@@ -12,11 +12,15 @@ const books = [ ...@@ -12,11 +12,15 @@ const books = [
{ {
title: "Jurassic Park", title: "Jurassic Park",
author: "Michael Crichton" author: "Michael Crichton"
},
{
title:"El perfume",
author:"Patrick"
} }
]; ];
// Type definitions define the "shape" of your data and specify // Las definiciones de tipo definen la "forma" de sus datos y especifican
// which ways the data can be fetched from the GraphQL server. // de qué formas se pueden obtener los datos del servidor GraphQL.
const typeDefs = gql` const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol. # Comments in GraphQL are defined with the hash (#) symbol.
...@@ -25,34 +29,52 @@ const typeDefs = gql` ...@@ -25,34 +29,52 @@ const typeDefs = gql`
title: String title: String
author: String author: String
} }
enum CharacterStatus{
Alive
Dead
unknown
}
type Character{
name: String
id: ID
status: CharacterStatus
episodes: [String]
}
# The "Query" type is the root of all GraphQL queries. # The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.) # (A "Mutation" type will be covered later on.)
type Query { type Query {
books: [Book] books: [Book]
characters: [Character]
character(id: ID): Character
} }
`; `;
// Resolvers define the technique for fetching the types in the // Los resolutores definen la técnica para obtener los tipos en el
// schema. We'll retrieve books from the "books" array above. // esquema. Recuperamos libros de la matriz "libros" de arriba.
const resolvers = { const resolvers = {
Query: { Query: {
books: () => books books: () => books,
characters: () => fetchCharacters(),
character: (parent, args) =>{
const {id} = args
return fetchCharacter({id})
}
} }
}; };
// In the most basic sense, the ApolloServer can be started // En el sentido más básico, ApolloServer se puede iniciar
// by passing type definitions (typeDefs) and the resolvers // pasando definiciones de tipo (typeDefs) y los resolutores
// responsible for fetching the data for those types. // responsable de obtener los datos de esos tipos.
const server = new ApolloServer({ typeDefs, resolvers }); const server = new ApolloServer({ typeDefs, resolvers });
// This `listen` method launches a web-server. Existing apps // Este método `listen` lanza un servidor web. Aplicaciones existentes
// can utilize middleware options, which we'll discuss later. // puede utilizar opciones de middleware, que discutiremos más adelante.
server.listen().then(({ url }) => { server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`); console.log(`🚀 Server ready at ${url}`);
}); });
// mock data for characters // datos simulados para personajes
const characters = [ const characters = [
{ {
name: "Rick Sanchez", name: "Rick Sanchez",
...@@ -87,7 +109,7 @@ const episodes = [ ...@@ -87,7 +109,7 @@ const episodes = [
]; ];
function fetchEpisodes() { function fetchEpisodes() {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // ¿Más información sobre la función de recuperación? https://github.com/bitinn/node-fetch#json
return fetch("https://rickandmortyapi.com/api/episode/") return fetch("https://rickandmortyapi.com/api/episode/")
.then(res => res.json()) .then(res => res.json())
.then(json => json.results); .then(json => json.results);
...@@ -105,22 +127,29 @@ function fetchEpisodeByUrl(url) { ...@@ -105,22 +127,29 @@ function fetchEpisodeByUrl(url) {
.then(json => json); .then(json => json);
} }
async function fetchCharacter() {
// ¿Más información sobre la función de recuperación? https://github.com/bitinn/node-fetch#json
const res = await fetch("https://rickandmortyapi.com/api/character/" + id);
return res.json();
}
function fetchCharacters() { function fetchCharacters() {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // ¿Más información sobre la función de recuperación? https://github.com/bitinn/node-fetch#json
return fetch("https://rickandmortyapi.com/api/character/") return fetch("https://rickandmortyapi.com/api/character/")
.then(res => res.json()) .then(res => res.json())
.then(json => json.results); .then(json => json.results);
} }
function fetchCharacterById(id) { function fetchCharacterById(id) {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // ¿Más información sobre la función de recuperación? https://github.com/bitinn/node-fetch#json
return fetch("https://rickandmortyapi.com/api/character/" + id) return fetch("https://rickandmortyapi.com/api/character/" + id)
.then(res => res.json()) .then(res => res.json())
.then(json => json); .then(json => json);
} }
function fetchCharacterByUrl(url) { function fetchCharacterByUrl(url) {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // ¿Más información sobre la función de recuperación? https://github.com/bitinn/node-fetch#json
return fetch(url) return fetch(url)
.then(res => res.json()) .then(res => res.json())
.then(json => json); .then(json => json);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment