Skip to content

Commit 09c6337

Browse files
committed
fix: adjust readme and release
1 parent 39c83fa commit 09c6337

File tree

1 file changed

+81
-13
lines changed

1 file changed

+81
-13
lines changed

README.md

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<a href="https://github.com/SurveyMonkey/graphql-introfields">
2525
</a>
2626

27-
<h3 align="center">GraphQL Ergonomock</h3>
27+
<h3 align="center">GraphQL Introfields</h3>
2828

2929
<p align="center">
30-
🔮 Developer-friendly automagical mocking for GraphQL
30+
🏑 GraphQL resolver utilities based on fields introspection.
3131
<br />
3232
<a href="https://github.com/SurveyMonkey/graphql-introfields/issues">Report Bug</a>
3333
·
@@ -36,17 +36,17 @@
3636
</p>
3737

3838

39-
4039
<!-- TABLE OF CONTENTS -->
4140
## Table of Contents
4241

4342
- [Table of Contents](#table-of-contents)
4443
- [About The Project](#about-the-project)
45-
- [Basic Example](#basic-example)
4644
- [Built With](#built-with)
4745
- [Getting Started](#getting-started)
4846
- [Installation](#installation)
4947
- [Usage](#usage)
48+
- [Basic Example](#basic-example)
49+
- [`resolveIDFieldFromRoot`](#resolveidfieldfromroot)
5050
- [Roadmap](#roadmap)
5151
- [Contributing](#contributing)
5252
- [License](#license)
@@ -58,30 +58,99 @@
5858
<!-- ABOUT THE PROJECT -->
5959
## About The Project
6060

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.
6662

6763
### Built With
6864
* [Typescript](https://www.typescriptlang.org/)
6965
* [GraphQL](https://graphql.org)
7066
* [Jest](https://jestjs.io)
7167

7268

73-
7469
<!-- GETTING STARTED -->
7570
## Getting Started
7671

7772
### Installation
7873

79-
TBD
74+
```
75+
npm install graphql-introfields
76+
```
8077

8178
<!-- USAGE EXAMPLES -->
8279
### Usage
8380

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+
```
85154

86155
<!-- ROADMAP -->
87156
## Roadmap
@@ -119,7 +188,6 @@ Project Link: [https://github.com/SurveyMonkey/graphql-introfields](https://gith
119188
<!-- ACKNOWLEDGEMENTS -->
120189
## Acknowledgements
121190

122-
* [A new approach to mocking GraphQL Data](https://www.freecodecamp.org/news/a-new-approach-to-mocking-graphql-data-1ef49de3d491/)
123191
* [GraphQL-Tools](https://github.com/apollographql/graphql-tools)
124192
* [GraphQL-JS](https://github.com/graphql/graphql-js)
125193

0 commit comments

Comments
 (0)