Skip to content

Commit

Permalink
Logging queries guide
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewilson committed Nov 5, 2021
1 parent c719d7c commit 664ba05
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/next/guides/logging-queries.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
slug: /next/guides/logging-queries
title: Logging Queries
description: You can log all the queries that are executed in Faust.js
---

In a development environment, you may want to log GraphQL queries that are being executed in your application. Faust.js provides a function, `logQueries`, to do this.

`logQueries` is called within your `client/index.ts` file. Take the following `client/index.ts` example:

```ts title="client/index.ts" {23-25}
/**
* GQTY: You can safely modify this file and Query Fetcher based on your needs
*/
import type { IncomingMessage } from 'http';
import { getClient } from '@faustjs/next';
import {
generatedSchema,
scalarsEnumsHash,
GeneratedSchema,
SchemaObjectTypes,
SchemaObjectTypesNames,
} from './schema.generated';

export const client = getClient<
GeneratedSchema,
SchemaObjectTypesNames,
SchemaObjectTypes
>({
schema: generatedSchema,
scalarsEnumsHash,
});

if (process.env.NODE_ENV === 'development') {
logQueries(client);
}

export function serverClient(req: IncomingMessage) {
return getClient<GeneratedSchema, SchemaObjectTypesNames, SchemaObjectTypes>({
schema: generatedSchema,
scalarsEnumsHash,
context: req,
});
}

export * from './schema.generated';
```

Note the conditional check for the node environment. It is recommended that you only log queries in a dev environment, and not in production.

Additionally, the `logQueries` function returns a function than can be called to turn off the logger:

```ts
if (process.env.NODE_ENV === 'development') {
const unsubscribe = logQueries(client);

// queries are now logging
// ...

unsubscribe();

// queries no longer log
// ...
}
```
5 changes: 5 additions & 0 deletions internal/website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ module.exports = {
label: 'Authentication',
id: 'next/guides/authentication',
},
{
type: 'doc',
label: 'Logging Queries',
id: 'next/guides/logging-queries',
},
{
type: 'doc',
label: '404s',
Expand Down

2 comments on commit 664ba05

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Branch site-dev could not be deployed
Your environment Development of app faustjs-site could not be updated
View build logs: https://my.wpengine.com/atlas#/faustjs-site/cixzyt38dn5ak04xxcqc36lf/pqlgc0rr4hu1vahmvde54gq2
View your environment URL: https://hcixzyt38dn5ak04xxcqc36lf.js.wpenginepowered.com

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Branch site-dev could not be deployed
Your environment Development of app faustjs-site could not be updated
View build logs: https://my.wpengine.com/atlas#/faustjs-site/cixzyt38dn5ak04xxcqc36lf/mvwpr86gfr2s07phch7jgoda
View your environment URL: https://hcixzyt38dn5ak04xxcqc36lf.js.wpenginepowered.com

Please sign in to comment.