Skip to content

Commit

Permalink
feat: adding optional logger for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
wjohnsto committed Jun 17, 2021
1 parent f0e5ae2 commit 0aa4bd2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
1 change: 0 additions & 1 deletion examples/next/getting-started/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { headlessConfig } from '@wpengine/headless-core';
import { HeadlessProvider } from '@wpengine/headless-next';
import Head from 'next/head';
import 'normalize.css/normalize.css';
import React from 'react';
import 'scss/main.scss';
Expand Down
7 changes: 2 additions & 5 deletions examples/next/getting-started/src/pages/posts/[postSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import { GetStaticPropsContext } from 'next';
import Head from 'next/head';

export default function Page() {
const { usePost, useGeneralSettings, useQuery } = client();
const { usePost, useGeneralSettings } = client();
const generalSettings = useGeneralSettings();
const post = usePost();

if (useQuery().$state.isLoading) {
return <>Loading...</>;
}

return (
<>
<Header
Expand All @@ -32,6 +28,7 @@ export default function Page() {
{post && (
<div dangerouslySetInnerHTML={{ __html: post.content() ?? '' }} />
)}
<div>{ post.author.node.nicename }</div>
</div>
</main>

Expand Down
7 changes: 1 addition & 6 deletions examples/next/getting-started/src/pages/posts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getNextStaticProps, client } from '@wpengine/headless-next';
import { Footer, Header, Pagination, Posts } from 'components';
import { GetStaticPropsContext } from 'next';
import Head from 'next/head';
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
import styles from 'scss/pages/home.module.scss';
Expand All @@ -12,7 +11,7 @@ export default function Page() {
query = {},
} = useRouter();
const { postSlug, postCursor } = query;
const { usePosts, useGeneralSettings, useQuery } = client();
const { usePosts, useGeneralSettings } = client();
const generalSettings = useGeneralSettings();
const isBefore = postSlug === 'before';
const posts = usePosts({
Expand All @@ -22,10 +21,6 @@ export default function Page() {
last: isBefore ? 6 : undefined,
});

if (useQuery().$state.isLoading) {
return <>Loading...</>;
}

return (
<>
<Header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Footer, Header, CTA } from 'components';
import { useRouter } from 'next/router';

export default function Page() {
const { query: { p }} = useRouter();
const { usePost, useGeneralSettings, useQuery } = client();
const generalSettings = useGeneralSettings();
const post = usePost();
Expand Down
75 changes: 72 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"typescript": "^4.3.2"
},
"dependencies": {
"@gqless/logger": "^2.0.11",
"@gqless/react": "^2.0.15",
"@wpengine/headless-core": "^0.6.9",
"@wpengine/headless-react": "^0.6.9",
Expand Down
18 changes: 18 additions & 0 deletions packages/next/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CreateReactClientOptions,
ReactClient,
} from '@gqless/react';
import type { LoggerOptions } from '@gqless/logger';
import { useRouter } from 'next/router';
import isObject from 'lodash/isObject';
import merge from 'lodash/merge';
Expand All @@ -24,6 +25,7 @@ import {
hasPostUri,
hasPostPreviewUri,
} from './utils';
import defaults from 'lodash/defaults';

/* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types */
export function client<Schema extends GeneratedSchema = GeneratedSchema>(
Expand Down Expand Up @@ -205,3 +207,19 @@ export function client<Schema extends GeneratedSchema = GeneratedSchema>(
useGeneralSettings,
};
}

export async function logQueries(options?: LoggerOptions): Promise<() => void> {
try {
const { createLogger } = await import('@gqless/logger');
const logger = createLogger(
client().client,
defaults({}, options, {
showSelections: false,
showCache: false,
} as LoggerOptions),
);
return logger.start();
} catch (e) {
return () => {};
}
}

0 comments on commit 0aa4bd2

Please sign in to comment.