Skip to content

Commit d32da5f

Browse files
committed
Setup graphql
1 parent b3663e9 commit d32da5f

File tree

5 files changed

+2040
-1
lines changed

5 files changed

+2040
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ app.*.map.json
4242

4343
# Exceptions to above rules.
4444
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
45+
46+
# Server dependencies (should be installed by running npm install within the server folder)
47+
server/node_modules/

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void main() {
1414
home: Scaffold(
1515
backgroundColor: Colors.grey.shade200,
1616
appBar: StyledAppBar(),
17-
body: StatsScreen(),
17+
body: HomeScreen(),
1818
bottomNavigationBar: StyledBottomNavigationBar(),
1919
),
2020
),

server/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { GraphQLServer } = require('graphql-yoga');
2+
const fetch = require('node-fetch');
3+
4+
const typeDefs = `
5+
type Query {
6+
hello(name: String): String!
7+
getPerson(id: Int!): Person
8+
}
9+
10+
type Film {
11+
title: String,
12+
episode_id: String,
13+
}
14+
15+
type Person {
16+
name: String
17+
height: String
18+
mass: Int
19+
hair_color: String
20+
skin_color: String,
21+
films: [Film],
22+
}
23+
`;
24+
25+
const resolvers = {
26+
Person: {
27+
films: (person) => {
28+
const filmsPromises = person.films.map(async url => {
29+
const response = await fetch(url);
30+
return response.json();
31+
});
32+
33+
return Promise.all(filmsPromises);
34+
}
35+
},
36+
Query: {
37+
hello: (_, { name }) => { return `Hello ${name || 'World'}` },
38+
getPerson: async (_, { id }) => {
39+
const response = await fetch(`https://swapi.dev/api/people/${id}/`);
40+
return response.json();
41+
}
42+
}
43+
}
44+
45+
const server = new GraphQLServer({ typeDefs, resolvers });
46+
server.start(() => console.log('Server is running on localhost:4000'));

0 commit comments

Comments
 (0)