Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Proyect-GraphQL-React
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sonia Vidal Gonzalez
Proyect-GraphQL-React
Commits
4efc14b7
Commit
4efc14b7
authored
Sep 20, 2021
by
Sonia Vidal Gonzalez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ok
parent
3b60395c
Pipeline
#5461
failed with stages
in 1 second
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
4164 additions
and
18 deletions
+4164
-18
package-lock.json
package-lock.json
+4117
-0
index.js
src/index.js
+47
-18
No files found.
package-lock.json
0 → 100644
View file @
4efc14b7
This source diff could not be displayed because it is too large. You can
view the blob
instead.
src/index.js
View file @
4efc14b7
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 resolver
s
//
pasando definiciones de tipo (typeDefs) y los resolutore
s
// respons
ible for fetching the data for those type
s.
// respons
able de obtener los datos de esos tipo
s.
const
server
=
new
ApolloServer
({
typeDefs
,
resolvers
});
const
server
=
new
ApolloServer
({
typeDefs
,
resolvers
});
//
This `listen` method launches a web-server. Existing app
s
//
Este método `listen` lanza un servidor web. Aplicaciones existente
s
//
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 character
s
//
datos simulados para personaje
s
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 functio
n? 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 functio
n? 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 functio
n? 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 functio
n? 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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment