|
24 | 24 | <a href="https://github.com/SurveyMonkey/graphql-introfields"> |
25 | 25 | </a> |
26 | 26 |
|
27 | | - <h3 align="center">GraphQL Ergonomock</h3> |
| 27 | + <h3 align="center">GraphQL Introfields</h3> |
28 | 28 |
|
29 | 29 | <p align="center"> |
30 | | - 🔮 Developer-friendly automagical mocking for GraphQL |
| 30 | + 🏑 GraphQL resolver utilities based on fields introspection. |
31 | 31 | <br /> |
32 | 32 | <a href="https://github.com/SurveyMonkey/graphql-introfields/issues">Report Bug</a> |
33 | 33 | · |
|
36 | 36 | </p> |
37 | 37 |
|
38 | 38 |
|
39 | | - |
40 | 39 | <!-- TABLE OF CONTENTS --> |
41 | 40 | ## Table of Contents |
42 | 41 |
|
43 | 42 | - [Table of Contents](#table-of-contents) |
44 | 43 | - [About The Project](#about-the-project) |
45 | | - - [Basic Example](#basic-example) |
46 | 44 | - [Built With](#built-with) |
47 | 45 | - [Getting Started](#getting-started) |
48 | 46 | - [Installation](#installation) |
49 | 47 | - [Usage](#usage) |
| 48 | + - [Basic Example](#basic-example) |
| 49 | + - [`resolveIDFieldFromRoot`](#resolveidfieldfromroot) |
50 | 50 | - [Roadmap](#roadmap) |
51 | 51 | - [Contributing](#contributing) |
52 | 52 | - [License](#license) |
|
58 | 58 | <!-- ABOUT THE PROJECT --> |
59 | 59 | ## About The Project |
60 | 60 |
|
61 | | -TBD |
62 | | - |
63 | | -### Basic Example |
64 | | - |
65 | | -TBD |
| 61 | +This library provides utilities to facilitate the resolution of GraphQL based on which fields are requested. |
66 | 62 |
|
67 | 63 | ### Built With |
68 | 64 | * [Typescript](https://www.typescriptlang.org/) |
69 | 65 | * [GraphQL](https://graphql.org) |
70 | 66 | * [Jest](https://jestjs.io) |
71 | 67 |
|
72 | 68 |
|
73 | | - |
74 | 69 | <!-- GETTING STARTED --> |
75 | 70 | ## Getting Started |
76 | 71 |
|
77 | 72 | ### Installation |
78 | 73 |
|
79 | | -TBD |
| 74 | +``` |
| 75 | +npm install graphql-introfields |
| 76 | +``` |
80 | 77 |
|
81 | 78 | <!-- USAGE EXAMPLES --> |
82 | 79 | ### Usage |
83 | 80 |
|
84 | | -TBD |
| 81 | +#### Basic Example |
| 82 | + |
| 83 | +With the following schema: |
| 84 | +```graphql |
| 85 | +type Employee { |
| 86 | + id: ID! |
| 87 | + name: String! |
| 88 | +} |
| 89 | + |
| 90 | +type VideoStore { |
| 91 | + id: ID! |
| 92 | + employees: [Employee!]! |
| 93 | + manager: Employee! |
| 94 | +} |
| 95 | + |
| 96 | +type Query { |
| 97 | + videostore: VideoStore |
| 98 | +} |
| 99 | + |
| 100 | +schema { |
| 101 | + query: Query |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +And with the following resolvers: |
| 106 | + |
| 107 | +```javascript |
| 108 | +import { resolveBasedOnFields } from 'graphql-introfields'; |
| 109 | + |
| 110 | +const resolvers = { |
| 111 | + Query: { |
| 112 | + videostore: getVideoStoreData, |
| 113 | + }, |
| 114 | + VideoStore: { |
| 115 | + manager: resolveBasedOnFields((fields) => { |
| 116 | + // if you know that only the `id` field is asked for, and it's present on the root, you can prevent |
| 117 | + // unnecessary resolution of the manager field (which may be an over-the-network request). |
| 118 | + if (fields.length === 1 && fields[0].name.value === 'id') { |
| 119 | + return (root, _args, _ctx, _info) => ({ id: root['managerId'] }); |
| 120 | + } |
| 121 | + return (root, _args, _ctx, _info) => getEmployeeById(root.managerId); |
| 122 | + }), |
| 123 | + }, |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +The following query doesn't actually call `getEmployeeById`: |
| 128 | + |
| 129 | +```graphql |
| 130 | +query { |
| 131 | + videostore { |
| 132 | + manager { |
| 133 | + id |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +#### `resolveIDFieldFromRoot` |
| 140 | + |
| 141 | +The above use-case is actually available as a provided utility. |
| 142 | + |
| 143 | +```js |
| 144 | +import { resolveIDFieldFromRoot } from 'graphql-introfields'; |
| 145 | +const resolvers = { |
| 146 | + videostore: { |
| 147 | + manager: resolveIDFieldFromRoot( |
| 148 | + 'managerId', |
| 149 | + (root) => getEmployeeById(root.managerId) |
| 150 | + ) |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
85 | 154 |
|
86 | 155 | <!-- ROADMAP --> |
87 | 156 | ## Roadmap |
@@ -119,7 +188,6 @@ Project Link: [https://github.com/SurveyMonkey/graphql-introfields](https://gith |
119 | 188 | <!-- ACKNOWLEDGEMENTS --> |
120 | 189 | ## Acknowledgements |
121 | 190 |
|
122 | | -* [A new approach to mocking GraphQL Data](https://www.freecodecamp.org/news/a-new-approach-to-mocking-graphql-data-1ef49de3d491/) |
123 | 191 | * [GraphQL-Tools](https://github.com/apollographql/graphql-tools) |
124 | 192 | * [GraphQL-JS](https://github.com/graphql/graphql-js) |
125 | 193 |
|
|
0 commit comments