Skip to content

Commit 7d30277

Browse files
authored
Expose query logger (#542)
* feat: exposing logQueries in next, updating dependencies * chore: adding changeset * fix: typo
1 parent 7b3521f commit 7d30277

File tree

9 files changed

+6175
-4003
lines changed

9 files changed

+6175
-4003
lines changed

.changeset/cyan-shirts-promise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@faustjs/next': patch
3+
---
4+
5+
`logQueries` is can now be called and will log GraphQL queries if desired.

docs/next/guides/fetching-data.mdx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,50 @@ export function PostForm() {
235235
> **NOTE**: The above code is not a complete example of how you would implement form submissions to your Headless WordPress API. It is intended to demonstrate how mutations work using the Faust.js client.
236236
237237
The code above uses `useMutation` combined with a form to create new posts by calling the [WPGraphQL](https://www.wpgraphql.com/) Headless WordPress API.
238+
239+
## Logging Queries
240+
241+
Sometimes you want to understand what GraphQL queries are made for debugging purposes. Faust.js provides this for you by exposing a `logQueries` function. The following code demonstrates how you might use it.
242+
243+
```ts title=src/client/index.ts {2, 21}
244+
import type { IncomingMessage } from 'http';
245+
import { getClient, logQueries } from '@faustjs/next';
246+
import {
247+
generatedSchema,
248+
scalarsEnumsHash,
249+
GeneratedSchema,
250+
SchemaObjectTypes,
251+
SchemaObjectTypesNames,
252+
} from './schema.generated';
253+
254+
export const client = getClient<
255+
GeneratedSchema,
256+
SchemaObjectTypesNames,
257+
SchemaObjectTypes
258+
>({
259+
schema: generatedSchema,
260+
scalarsEnumsHash,
261+
});
262+
263+
if (process.env.NODE_ENV === 'development') {
264+
logQueries(client);
265+
}
266+
```
267+
268+
The `logQueries` function returns a function that you can call to turn the log off.
269+
270+
```ts
271+
if (process.env.NODE_ENV === 'development') {
272+
const unsubscribe = logQueries(client)
273+
274+
// queries are now logging
275+
// ...
276+
277+
unsubscribe();
278+
279+
// queries no longer log
280+
// ...
281+
}
282+
```
283+
284+
> **NOTE**: It is recommended that you turn this off in production, or write code to use `process.env.NODE_ENV` to safely log queries.

0 commit comments

Comments
 (0)