Skip to content

Commit 24b79c9

Browse files
lyskos97nodkz
authored andcommitted
docs: create a base version of README followed by code examples
1 parent 54a5c9d commit 24b79c9

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

README.md

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,88 @@
77
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
88
[![Greenkeeper badge](https://badges.greenkeeper.io/graphql-compose/graphql-compose-rest.svg)](https://greenkeeper.io/)
99

10-
1110
This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose), which derives GraphQLType from REST response. Also derives bunch of internal GraphQL Types.
1211

13-
Installation
14-
============
12+
## Getting started
13+
14+
### Demo
15+
16+
We have a [demo app](demo/) which shows how to build an API upon [SWAPI](https://swapi.co) using `graphql-compose-rest`.
17+
18+
You can run it using:
19+
20+
```js
21+
npm run demo
22+
```
23+
24+
### Installation
25+
1526
```
1627
npm install graphql graphql-compose graphql-compose-rest --save
1728
```
18-
Modules `graphql`, `graphql-compose`, are in `peerDependencies`, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
1929

20-
License
21-
=======
30+
Modules `graphql`, `graphql-compose`, are located in `peerDependencies`, so they should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
31+
32+
### Examples
33+
34+
We have a sample response object:
35+
36+
```js
37+
const responseFromRestApi = {
38+
name: 'Anakin Skywalker',
39+
birth_year: '41.9BBY',
40+
gender: 'male',
41+
homeworld: 'https://swapi.co/api/planets/1/',
42+
films: [
43+
'https://swapi.co/api/films/5/',
44+
'https://swapi.co/api/films/4/',
45+
'https://swapi.co/api/films/6/',
46+
],
47+
species: ['https://swapi.co/api/species/1/'],
48+
starships: [
49+
'https://swapi.co/api/starships/59/',
50+
'https://swapi.co/api/starships/65/',
51+
'https://swapi.co/api/starships/39/',
52+
],
53+
};
54+
```
55+
56+
which we pass to a `composeWithRest` function of `graphql-compose-rest` along with desired type name as first argument in order to generate a `GraphQL` type:
57+
58+
```js
59+
const PersonTC = composeWithRest('Person', responseFromRestApi);
60+
```
61+
62+
and successfully export for furher usage:
63+
64+
```js
65+
export PersonTC;
66+
```
67+
68+
If you want to specify new fields for your type, just use the `addField` function of `TypeComposer` type of `graphql-compose`:
69+
70+
```js
71+
PersonTC.addFields({
72+
weapon: { // standard GraphQL like field definition
73+
type: GraphQLString,
74+
resolve: (source) => source.name.toUpperCase(),
75+
},
76+
});
77+
```
78+
79+
In case you want to create a relation with another type simply use `addRelation` function of `TypeComposer`:
80+
81+
```js
82+
PersonTC.addRelation('filmObjects', { // uses shortened syntax
83+
resolver: () => FilmTC.getResolver('findByUrlList'),
84+
prepareArgs: { // you're free to define `resolve` the way you want
85+
urls: source => source.films,
86+
},
87+
});
88+
```
89+
90+
`graphql-compose` provides a vast variety of methods for types' `fields` and `resolvers` (aka `field configs` in vanilla `GraphQL`) management. To learn more visit [graphql-compose repo](https://github.com/nodkz/graphql-compose).
91+
92+
## License
93+
2294
[MIT](https://github.com/graphql-compose/graphql-compose-rest/blob/master/LICENSE.md)

demo/utils.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export function createFindByIdResolver(tc: TypeComposer, urlAddr: string): void
1010
args: {
1111
id: 'Int!',
1212
},
13-
resolve: rp => {
14-
return fetch(`https://swapi.co/api/${urlAddr}/${rp.args.id}/`).then(r => r.json());
13+
resolve: async rp => {
14+
const res = await fetch(`https://swapi.co/api/${urlAddr}/${rp.args.id}/`);
15+
const data = await res.json();
16+
return data;
1517
},
1618
});
1719
}
@@ -24,8 +26,8 @@ export function createFindListByPageNumberResolver(tc: TypeComposer, urlAddr: st
2426
page: { type: 'Int', defaultValue: 1 },
2527
},
2628
resolve: async rp => {
27-
const response = await fetch(`https://swapi.co/api/${urlAddr}/?page=${rp.args.page}`);
28-
const data = await response.json();
29+
const res = await fetch(`https://swapi.co/api/${urlAddr}/?page=${rp.args.page}`);
30+
const data = await res.json();
2931
return data.results;
3032
},
3133
});
@@ -39,7 +41,11 @@ export function createFindByUrlListResolver(tc: TypeComposer): void {
3941
urls: '[String]!',
4042
},
4143
resolve: rp => {
42-
return Promise.all(rp.args.urls.map(url => fetch(url).then(r => r.json())));
44+
return rp.args.urls.map(async url => {
45+
const res = await fetch(url);
46+
const data = await res.json();
47+
return data;
48+
});
4349
},
4450
});
4551
}

0 commit comments

Comments
 (0)