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 { ApolloServer, gql } = require("apollo-server");
// This is a (sample) collection of books we'll be able to query
// the GraphQL server for. A more complete example might fetch
// from an existing data source like a REST API or database.
// Esta es una colección (de muestra) de libros que podremos consultar
// el servidor GraphQL para. Un ejemplo más completo podría recuperar
// de una fuente de datos existente como una API REST o una base de datos.
const books = [
{
title: "Harry Potter and the Chamber of Secrets",
......@@ -12,11 +12,15 @@ const books = [
{
title: "Jurassic Park",
author: "Michael Crichton"
},
{
title:"El perfume",
author:"Patrick"
}
];
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
// Las definiciones de tipo definen la "forma" de sus datos y especifican
// de qué formas se pueden obtener los datos del servidor GraphQL.
const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
......@@ -25,34 +29,52 @@ const typeDefs = gql`
title: 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.
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
characters: [Character]
character(id: ID): Character
}
`;
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
// Los resolutores definen la técnica para obtener los tipos en el
// esquema. Recuperamos libros de la matriz "libros" de arriba.
const resolvers = {
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
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
// En el sentido más básico, ApolloServer se puede iniciar
// pasando definiciones de tipo (typeDefs) y los resolutores
// responsable de obtener los datos de esos tipos.
const server = new ApolloServer({ typeDefs, resolvers });
// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
// Este método `listen` lanza un servidor web. Aplicaciones existentes
// puede utilizar opciones de middleware, que discutiremos más adelante.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
// mock data for characters
// datos simulados para personajes
const characters = [
{
name: "Rick Sanchez",
......@@ -87,7 +109,7 @@ const episodes = [
];
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/")
.then(res => res.json())
.then(json => json.results);
......@@ -105,22 +127,29 @@ function fetchEpisodeByUrl(url) {
.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() {
// 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/")
.then(res => res.json())
.then(json => json.results);
}
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)
.then(res => res.json())
.then(json => json);
}
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)
.then(res => res.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