Skip to content

Commit a7dc69e

Browse files
committed
document codegen for localstate
1 parent c13a81d commit a7dc69e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

docs/source/local-state/local-resolvers.mdx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,123 @@ new LocalState<Resolvers, ContextValue>({
165165
});
166166
```
167167

168+
#### Generating types for local resolvers
169+
170+
When using TypeScript, you can generate resolver types for your local resolvers using the `@apollo/client-graphql-codegen` package. This package provides a specialized GraphQL Code Generator plugin that creates resolver types compatible with Apollo Client's `LocalState`.
171+
172+
##### Installation
173+
174+
Install the GraphQL Code Generator plugin for local state:
175+
176+
```bash
177+
npm install -D @apollo/client-graphql-codegen
178+
```
179+
180+
##### Configuration
181+
182+
Add the local-state plugin to your `codegen.ts` configuration. It's recommended to use a separate schema file containing only your local fields to avoid generating resolver types for your entire remote schema:
183+
184+
```ts title="codegen.ts"
185+
import type { CodegenConfig } from '@graphql-codegen/cli';
186+
import type { LocalStatePluginConfig } from '@apollo/client-graphql-codegen/local-state';
187+
188+
const config: CodegenConfig = {
189+
// ... your existing configuration
190+
generates: {
191+
// ... your existing generates
192+
'./src/__generated__/local-resolvers.ts': {
193+
schema: ['./src/local-schema.graphql'],
194+
plugins: [
195+
'typescript',
196+
'@apollo/client-graphql-codegen/local-state',
197+
],
198+
config: {
199+
// Ensures you return a `__typename` for any `@client` fields that
200+
// return object or array types
201+
nonOptionalTypename: true,
202+
// Required if your local schema extends existing schema types
203+
baseTypesPath: './src/__generated__/graphql',
204+
// If you provide a `context` function to customize the context value,
205+
// provide the path or type here
206+
contextType: './src/context#MyContextType',
207+
} satisfies LocalStatePluginConfig,
208+
},
209+
},
210+
};
211+
212+
export default config;
213+
```
214+
215+
##### Creating your local schema
216+
217+
Create a GraphQL schema file that defines your local-only fields:
218+
219+
```graphql title="src/local-schema.graphql"
220+
extend type Query {
221+
isLoggedIn: Boolean!
222+
cartItems: [String!]!
223+
}
224+
225+
extend type Mutation {
226+
login(token: String!): Boolean!
227+
addToCart(item: String!): [String!]!
228+
}
229+
230+
extend type Product {
231+
isInCart: Boolean!
232+
}
233+
```
234+
235+
##### Using generated types
236+
237+
After running the code generator, you can use the generated `Resolvers` type with your `LocalState` instance:
238+
239+
```ts
240+
import { LocalState } from "@apollo/client/local-state";
241+
import type { Resolvers } from "./src/__generated__/local-resolvers";
242+
243+
const localState =
244+
new LocalState() <
245+
Resolvers >
246+
{
247+
resolvers: {
248+
Query: {
249+
isLoggedIn: () => {
250+
return !!localStorage.getItem("token");
251+
},
252+
cartItems: () => {
253+
return JSON.parse(localStorage.getItem("cart") || "[]");
254+
},
255+
},
256+
Mutation: {
257+
login: (_, { token }) => {
258+
localStorage.setItem("token", token);
259+
return true;
260+
},
261+
addToCart: (_, { item }) => {
262+
const cart = JSON.parse(localStorage.getItem("cart") || "[]");
263+
cart.push(item);
264+
localStorage.setItem("cart", JSON.stringify(cart));
265+
return cart;
266+
},
267+
},
268+
Product: {
269+
isInCart: (product) => {
270+
const cart = JSON.parse(localStorage.getItem("cart") || "[]");
271+
return cart.includes(product.id);
272+
},
273+
},
274+
},
275+
};
276+
277+
const client = new ApolloClient({
278+
cache: new InMemoryCache(),
279+
localState,
280+
});
281+
```
282+
283+
This approach ensures that your local resolver functions are properly typed and checked against your local schema, providing better development experience and catching type errors at compile time.
284+
168285
### Error handling
169286

170287
Throwing errors in a resolver will set the field value as `null` and add an error to the response's `errors` array. This follows GraphQL's standard error handling behavior:

0 commit comments

Comments
 (0)