|
7 | 7 | [](http://commitizen.github.io/cz-cli/) |
8 | 8 | [](https://greenkeeper.io/) |
9 | 9 |
|
10 | | - |
11 | 10 | 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. |
12 | 11 |
|
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 | + |
15 | 26 | ``` |
16 | 27 | npm install graphql graphql-compose graphql-compose-rest --save |
17 | 28 | ``` |
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. |
19 | 29 |
|
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 | + |
22 | 94 | [MIT](https://github.com/graphql-compose/graphql-compose-rest/blob/master/LICENSE.md) |
0 commit comments