|
| 1 | +# Modular Resolvers and TypeDefs |
| 2 | + |
| 3 | +This `graphql-yoga` example written in Typescript illustrates how you can have complete freedom |
| 4 | +over the structure of your **typeDef** and **resolver** files by using the |
| 5 | +[`merge-graphql-schemas` utility library](https://github.com/okgrow/merge-graphql-schemas). |
| 6 | + |
| 7 | +## Get Started |
| 8 | + |
| 9 | +**Clone the repository:** |
| 10 | + |
| 11 | +```sh |
| 12 | +git clone https://github.com/graphcool/graphql-yoga.git |
| 13 | +cd graphql-yoga/examples/modular-resolvers |
| 14 | +``` |
| 15 | + |
| 16 | +**Install dependencies and run the app:** |
| 17 | + |
| 18 | +```sh |
| 19 | +npm install # or yarn install |
| 20 | +npm start # or yarn start |
| 21 | +``` |
| 22 | + |
| 23 | +## Run the Queries |
| 24 | + |
| 25 | +Open your browser at [http://localhost:4004](http://localhost:4004) and start sending queries in the Playground. |
| 26 | + |
| 27 | +**In the left pane, run the `welcome` Query:** |
| 28 | + |
| 29 | +```graphql |
| 30 | +{ |
| 31 | + welcome(yourNickname:"Superstar") |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The server returns the following response: |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "data": { |
| 40 | + "welcome": "Welcome, Superstar!" |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +**Query a specific `user` by id:** |
| 46 | + |
| 47 | +```graphql |
| 48 | +{ |
| 49 | + user(id:2){ |
| 50 | + id |
| 51 | + userName |
| 52 | + firstName |
| 53 | + lastName |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The server returns the following response: |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "data": { |
| 63 | + "user": { |
| 64 | + "id": 2, |
| 65 | + "firstName": "Kimberly", |
| 66 | + "lastName": "Jones", |
| 67 | + "userName": "kim" |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +**Query all `users`:** |
| 74 | + |
| 75 | +```graphql |
| 76 | +{ |
| 77 | + users{ |
| 78 | + userName |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +The server returns the following response: |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "data": { |
| 88 | + "users": [ |
| 89 | + { |
| 90 | + "userName": "john" |
| 91 | + }, |
| 92 | + { |
| 93 | + "userName": "kim" |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Implementation |
| 101 | + |
| 102 | +The merging takes place in the [/resolvers/index.ts](./resolvers/index.ts) |
| 103 | +and the [/typeDefs/index.ts](./typeDefs/index.ts) files. |
| 104 | + |
| 105 | +Using this approach, you're free to structure resolver and typeDef files as you see fit. |
| 106 | + |
| 107 | +>To avoid issues, unique naming of Queries, Mutations and Subscriptions is your responsibility. |
| 108 | +
|
| 109 | +Now you can structure by **function**... |
| 110 | +``` |
| 111 | ++-- graphql |
| 112 | +| +-- resolvers |
| 113 | +| | +-- user.resolvers.js/ts |
| 114 | +| | +-- welcome.resolvers.js/ts |
| 115 | +| | +-- index.ts << Merges all `*.resolvers.*` files |
| 116 | +| +-- typeDefs |
| 117 | +| | +-- user.graphql |
| 118 | +| | +-- welcome.graphql |
| 119 | +| | +-- index.ts <<< Merges all `typeDef` files |
| 120 | +``` |
| 121 | + |
| 122 | +Or by **type**... |
| 123 | +``` |
| 124 | ++-- graphql |
| 125 | +| +-- entity |
| 126 | +| | +-- user |
| 127 | +| | | +-- user.graphql |
| 128 | +| | | +-- user.resolvers.js/ts |
| 129 | +| | +-- welcome |
| 130 | +| | | +-- welcome.graphql |
| 131 | +| | | +-- welcome.resolvers.js/ts |
| 132 | +| | +-- index.ts << Merges all `*.resolvers.*` and typeDef files |
| 133 | +``` |
0 commit comments