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
15fa642a
Commit
15fa642a
authored
May 29, 2019
by
alexlbr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removes src/index_base.js
parent
172ed06b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
136 deletions
+10
-136
index.js
src/index.js
+10
-34
index_base.js
src/index_base.js
+0
-102
No files found.
src/index.js
View file @
15fa642a
...
...
@@ -6,47 +6,24 @@ const { ApolloServer, gql } = require("apollo-server");
const
typeDefs
=
gql
`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Character" type can be used in other type declarations.
type Character {
id: Int
name: String
status: String
episodes: [Episode]
}
type Episode {
id: Int
name: String
# This "Book" type can be used in other type declarations.
type Book {
title: String
author: String
}
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
characters: [Character]
character(id: Int): Character
episodes: [Episode]
books: [Book]
}
`
;
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const
resolvers
=
{
Query
:
{
characters
:
()
=>
fetchCharacters
(),
character
:
(
_
,
args
)
=>
fetchCharacter
(
args
.
id
),
episodes
:
()
=>
fetchEpisodes
()
},
Character
:
{
episodes
:
async
obj
=>
{
const
characterEpisodes
=
obj
.
episode
||
[];
const
episodes
=
await
fetchEpisodes
();
if
(
characterEpisodes
.
length
===
0
)
{
return
[];
}
return
characterEpisodes
.
map
(
episodeUrl
=>
{
return
mapEpisodeUrlToEpisode
(
episodes
,
episodeUrl
);
});
}
books
:
()
=>
books
}
};
...
...
@@ -61,9 +38,7 @@ server.listen().then(({ url }) => {
console
.
log
(
`🚀 Server ready at
${
url
}
`
);
});
// 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.
// mock data for characters
const
characters
=
[
{
name
:
"Rick Sanchez"
,
...
...
@@ -85,6 +60,7 @@ const characters = [
}
];
// mock data for episodes
const
episodes
=
[
{
name
:
"Pilot"
,
...
...
src/index_base.js
deleted
100644 → 0
View file @
172ed06b
const
fetch
=
require
(
"node-fetch"
);
const
{
ApolloServer
,
gql
}
=
require
(
"apollo-server"
);
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const
typeDefs
=
gql
`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Book" type can be used in other type declarations.
type Book {
title: String
author: String
}
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
}
`
;
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const
resolvers
=
{
Query
:
{
books
:
()
=>
books
}
};
// 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.
const
server
=
new
ApolloServer
({
typeDefs
,
resolvers
});
// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
server
.
listen
().
then
(({
url
})
=>
{
console
.
log
(
`🚀 Server ready at
${
url
}
`
);
});
// mock data for characters
const
characters
=
[
{
name
:
"Rick Sanchez"
,
id
:
1
,
status
:
"Alive"
,
episodes
:
[
"https://rickandmortyapi.com/api/episode/1"
,
"https://rickandmortyapi.com/api/episode/2"
]
},
{
name
:
"Morty Smith"
,
id
:
2
,
status
:
"Alive"
,
episodes
:
[
"https://rickandmortyapi.com/api/episode/1"
,
"https://rickandmortyapi.com/api/episode/3"
]
}
];
// mock data for episodes
const
episodes
=
[
{
name
:
"Pilot"
,
id
:
1
},
{
name
:
"Lawnmower Dog"
,
id
:
2
}
];
function
getEpisodeIdFromUrl
(
url
)
{
return
url
&&
url
.
split
(
"/"
).
pop
();
}
function
mapEpisodeUrlToEpisode
(
episodes
,
episodeUrl
)
{
return
episodes
.
find
(
e
=>
e
.
id
==
getEpisodeIdFromUrl
(
episodeUrl
));
}
function
fetchEpisodes
()
{
// More info about the fetch function? https://github.com/bitinn/node-fetch#json
return
fetch
(
"https://rickandmortyapi.com/api/character/"
)
.
then
(
res
=>
res
.
json
())
.
then
(
json
=>
json
.
results
);
}
function
fetchCharacters
()
{
// More info about the fetch function? https://github.com/bitinn/node-fetch#json
return
fetch
(
"https://rickandmortyapi.com/api/character/"
)
.
then
(
res
=>
res
.
json
())
.
then
(
json
=>
json
.
results
);
}
function
fetchCharacter
(
id
)
{
// More info about the fetch function? https://github.com/bitinn/node-fetch#json
return
fetch
(
"https://rickandmortyapi.com/api/character/"
+
id
)
.
then
(
res
=>
res
.
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