Skip to content

Commit 28eadab

Browse files
lyskos97nodkz
authored andcommitted
feat: create demo app with SWAPI
1 parent ed74191 commit 28eadab

File tree

11 files changed

+584
-22
lines changed

11 files changed

+584
-22
lines changed

demo/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* @flow */
2+
3+
import express from 'express';
4+
import expressGraphQL from 'express-graphql';
5+
import schema from './schema/Schema';
6+
7+
const PORT = 3000;
8+
const app = express();
9+
10+
app.use(
11+
'/graphql',
12+
expressGraphQL(req => ({
13+
schema,
14+
graphiql: true,
15+
context: req,
16+
}))
17+
);
18+
19+
app.listen(PORT, () => {
20+
console.log(`App running on port ${PORT}`);
21+
console.log(`Visit http://localhost:${PORT}/graphql`);
22+
});

demo/schema/Film.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* @flow */
2+
3+
import {
4+
createFindByIdResolver,
5+
createFindListByPageNumberResolver,
6+
createFindByUrlListResolver,
7+
} from './../utils';
8+
import composeWithRest from '../../src/index';
9+
import { PersonTC } from './Person';
10+
import { PlanetTC } from './Planet';
11+
import { VehicleTC } from './Vehicle';
12+
import { SpeciesTC } from './Species';
13+
import { StarshipTC } from './Starship';
14+
15+
const responseFromRestApi = {
16+
title: 'A New Hope',
17+
episode_id: 4,
18+
opening_crawl:
19+
"It is a period of civil war.\r\nRebel spaceships, striking\r\nfrom a hidden base, have won\r\ntheir first victory against\r\nthe evil Galactic Empire.\r\n\r\nDuring the battle, Rebel\r\nspies managed to steal secret\r\nplans to the Empire's\r\nultimate weapon, the DEATH\r\nSTAR, an armored space\r\nstation with enough power\r\nto destroy an entire planet.\r\n\r\nPursued by the Empire's\r\nsinister agents, Princess\r\nLeia races home aboard her\r\nstarship, custodian of the\r\nstolen plans that can save her\r\npeople and restore\r\nfreedom to the galaxy....",
20+
director: 'George Lucas',
21+
producer: 'Gary Kurtz, Rick McCallum',
22+
release_date: '1977-05-25',
23+
characters: [
24+
'https://swapi.co/api/people/1/',
25+
'https://swapi.co/api/people/2/',
26+
'https://swapi.co/api/people/3/',
27+
'https://swapi.co/api/people/4/',
28+
'https://swapi.co/api/people/5/',
29+
'https://swapi.co/api/people/6/',
30+
'https://swapi.co/api/people/7/',
31+
'https://swapi.co/api/people/8/',
32+
'https://swapi.co/api/people/9/',
33+
'https://swapi.co/api/people/10/',
34+
'https://swapi.co/api/people/12/',
35+
'https://swapi.co/api/people/13/',
36+
'https://swapi.co/api/people/14/',
37+
'https://swapi.co/api/people/15/',
38+
'https://swapi.co/api/people/16/',
39+
'https://swapi.co/api/people/18/',
40+
'https://swapi.co/api/people/19/',
41+
'https://swapi.co/api/people/81/',
42+
],
43+
planets: [
44+
'https://swapi.co/api/planets/2/',
45+
'https://swapi.co/api/planets/3/',
46+
'https://swapi.co/api/planets/1/',
47+
],
48+
starships: [
49+
'https://swapi.co/api/starships/2/',
50+
'https://swapi.co/api/starships/3/',
51+
'https://swapi.co/api/starships/5/',
52+
'https://swapi.co/api/starships/9/',
53+
'https://swapi.co/api/starships/10/',
54+
'https://swapi.co/api/starships/11/',
55+
'https://swapi.co/api/starships/12/',
56+
'https://swapi.co/api/starships/13/',
57+
],
58+
vehicles: [
59+
'https://swapi.co/api/vehicles/4/',
60+
'https://swapi.co/api/vehicles/6/',
61+
'https://swapi.co/api/vehicles/7/',
62+
'https://swapi.co/api/vehicles/8/',
63+
],
64+
species: [
65+
'https://swapi.co/api/species/5/',
66+
'https://swapi.co/api/species/3/',
67+
'https://swapi.co/api/species/2/',
68+
'https://swapi.co/api/species/1/',
69+
'https://swapi.co/api/species/4/',
70+
],
71+
created: '2014-12-10T14:23:31.880000Z',
72+
edited: '2015-04-11T09:46:52.774897Z',
73+
url: 'https://swapi.co/api/films/1/',
74+
};
75+
76+
export const FilmTC = composeWithRest('Film', responseFromRestApi);
77+
78+
// //////////////
79+
// RESOLVERS aka FieldConfig in GraphQL
80+
// //////////////
81+
82+
createFindByIdResolver(FilmTC, 'films');
83+
84+
createFindListByPageNumberResolver(FilmTC, 'films');
85+
86+
createFindByUrlListResolver(FilmTC);
87+
88+
// //////////////
89+
// RELATIONS
90+
// //////////////
91+
92+
FilmTC.addRelation('characterObjs', {
93+
resolver: () => PersonTC.getResolver('findByUrlList'),
94+
prepareArgs: {
95+
urls: source => source.characters,
96+
},
97+
});
98+
99+
FilmTC.addRelation('speciesObjs', {
100+
resolver: () => SpeciesTC.getResolver('findByUrlList'),
101+
prepareArgs: {
102+
urls: source => source.species,
103+
},
104+
});
105+
106+
FilmTC.addRelation('vehicleObjs', {
107+
resolver: () => VehicleTC.getResolver('findByUrlList'),
108+
prepareArgs: {
109+
urls: source => source.vehicles,
110+
},
111+
});
112+
113+
FilmTC.addRelation('starshipObjs', {
114+
resolver: () => StarshipTC.getResolver('findByUrlList'),
115+
prepareArgs: {
116+
urls: source => source.starships,
117+
},
118+
});
119+
120+
FilmTC.addRelation('planetObjs', {
121+
resolver: () => PlanetTC.getResolver('findByUrlList'),
122+
prepareArgs: {
123+
urls: source => source.planets,
124+
},
125+
});

demo/schema/Person.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* @flow */
2+
3+
// import fetch from 'node-fetch';
4+
import composeWithRest from '../../src/index';
5+
import {
6+
createFindByIdResolver,
7+
createFindListByPageNumberResolver,
8+
createFindByUrlListResolver,
9+
} from './../utils';
10+
import { FilmTC } from './Film';
11+
import { PlanetTC } from './Planet';
12+
import { VehicleTC } from './Vehicle';
13+
import { SpeciesTC } from './Species';
14+
import { StarshipTC } from './Starship';
15+
16+
const responseFromRestApi = {
17+
name: 'Anakin Skywalker',
18+
height: '188',
19+
mass: '84',
20+
hair_color: 'blond',
21+
skin_color: 'fair',
22+
eye_color: 'blue',
23+
birth_year: '41.9BBY',
24+
gender: 'male',
25+
homeworld: 'https://swapi.co/api/planets/1/',
26+
films: [
27+
'https://swapi.co/api/films/5/',
28+
'https://swapi.co/api/films/4/',
29+
'https://swapi.co/api/films/6/',
30+
],
31+
species: ['https://swapi.co/api/species/1/'],
32+
vehicles: ['https://swapi.co/api/vehicles/44/', 'https://swapi.co/api/vehicles/46/'],
33+
starships: [
34+
'https://swapi.co/api/starships/59/',
35+
'https://swapi.co/api/starships/65/',
36+
'https://swapi.co/api/starships/39/',
37+
],
38+
created: '2014-12-10T16:20:44.310000Z',
39+
edited: '2014-12-20T21:17:50.327000Z',
40+
url: 'https://swapi.co/api/people/11/',
41+
};
42+
43+
export const PersonTC = composeWithRest('Person', responseFromRestApi);
44+
45+
// //////////////
46+
// RESOLVERS aka FieldConfig in GraphQL
47+
// //////////////
48+
49+
createFindByIdResolver(PersonTC, 'people');
50+
51+
createFindListByPageNumberResolver(PersonTC, 'people');
52+
53+
createFindByUrlListResolver(PersonTC);
54+
55+
PersonTC.addRelation('filmObjs', {
56+
resolver: () => FilmTC.getResolver('findByUrlList'),
57+
prepareArgs: {
58+
urls: source => source.films,
59+
},
60+
});
61+
62+
PersonTC.addRelation('homeworldObj', {
63+
resolver: () => PlanetTC.getResolver('findByUrl'),
64+
prepareArgs: {
65+
urls: source => source.homeworld,
66+
},
67+
});
68+
69+
PersonTC.addRelation('speciesObjs', {
70+
resolver: () => SpeciesTC.getResolver('findByUrlList'),
71+
prepareArgs: {
72+
urls: source => source.species,
73+
},
74+
});
75+
76+
PersonTC.addRelation('vehicleObjs', {
77+
resolver: () => VehicleTC.getResolver('findByUrlList'),
78+
prepareArgs: {
79+
urls: source => source.vehicles,
80+
},
81+
});
82+
83+
PersonTC.addRelation('starshipObjs', {
84+
resolver: () => StarshipTC.getResolver('findByUrlList'),
85+
prepareArgs: {
86+
urls: source => source.starships,
87+
},
88+
});

demo/schema/Planet.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* @flow */
2+
3+
import fetch from 'node-fetch';
4+
import {
5+
createFindByIdResolver,
6+
createFindListByPageNumberResolver,
7+
createFindByUrlListResolver,
8+
} from './../utils';
9+
import composeWithRest from '../../src/index';
10+
import { PersonTC } from './Person';
11+
import { FilmTC } from './Film';
12+
13+
const responseFromRestApi = {
14+
name: 'Alderaan',
15+
rotation_period: '24',
16+
orbital_period: '364',
17+
diameter: '12500',
18+
climate: 'temperate',
19+
gravity: '1 standard',
20+
terrain: 'grasslands, mountains',
21+
surface_water: '40',
22+
population: '2000000000',
23+
residents: [
24+
'https://swapi.co/api/people/5/',
25+
'https://swapi.co/api/people/68/',
26+
'https://swapi.co/api/people/81/',
27+
],
28+
films: ['https://swapi.co/api/films/6/', 'https://swapi.co/api/films/1/'],
29+
created: '2014-12-10T11:35:48.479000Z',
30+
edited: '2014-12-20T20:58:18.420000Z',
31+
url: 'https://swapi.co/api/planets/2/',
32+
};
33+
34+
export const PlanetTC = composeWithRest('Planet', responseFromRestApi);
35+
36+
// //////////////
37+
// RESOLVERS aka FieldConfig in GraphQL
38+
// //////////////
39+
40+
createFindByIdResolver(PlanetTC, 'planets');
41+
42+
createFindListByPageNumberResolver(PlanetTC, 'planets');
43+
44+
createFindByUrlListResolver(PlanetTC);
45+
46+
PlanetTC.addResolver({
47+
name: 'findByUrl',
48+
type: PlanetTC,
49+
args: {
50+
url: 'String!',
51+
},
52+
resolve: rp => fetch(rp.args.url).then(r => r.json()),
53+
});
54+
55+
// //////////////
56+
// RELATIONS
57+
// //////////////
58+
59+
PlanetTC.addRelation('residentObjs', {
60+
resolver: () => PersonTC.getResolver('findByUrlList'),
61+
prepareArgs: {
62+
urls: source => source.residents,
63+
},
64+
});
65+
66+
PlanetTC.addRelation('filmObjs', {
67+
resolver: () => FilmTC.getResolver('findByUrlList'),
68+
prepareArgs: {
69+
urls: source => source.films,
70+
},
71+
});

demo/schema/Schema.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* @flow */
2+
3+
import { GQC } from 'graphql-compose';
4+
import { FilmTC } from './Film';
5+
import { PersonTC } from './Person';
6+
import { PlanetTC } from './Planet';
7+
import { VehicleTC } from './Vehicle';
8+
import { SpeciesTC } from './Species';
9+
import { StarshipTC } from './Starship';
10+
11+
GQC.rootQuery().addFields({
12+
film: FilmTC.getResolver('findById'),
13+
person: PersonTC.getResolver('findById'),
14+
planet: PlanetTC.getResolver('findById'),
15+
speciesOne: SpeciesTC.getResolver('findById'),
16+
vehicle: VehicleTC.getResolver('findById'),
17+
starship: StarshipTC.getResolver('findById'),
18+
people: PersonTC.getResolver('findListByPageNumber'),
19+
planets: PlanetTC.getResolver('findListByPageNumber'),
20+
speciesMany: SpeciesTC.getResolver('findListByPageNumber'),
21+
vehicles: VehicleTC.getResolver('findListByPageNumber'),
22+
starships: StarshipTC.getResolver('findListByPageNumber'),
23+
films: FilmTC.getResolver('findListByPageNumber'),
24+
});
25+
26+
const schema = GQC.buildSchema();
27+
28+
export default schema;

0 commit comments

Comments
 (0)