Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple queries in a Faust template #1626

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 68 additions & 50 deletions examples/next/faustwp-getting-started/wp-templates/single.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,65 @@ import {
FeaturedImage,
SEO,
} from '../components';
import { useContext } from 'react';
import { useFaustQuery, FaustContext } from '@faustwp/core';

const GET_LAYOUT_QUERY = gql`
${BlogInfoFragment}
${NavigationMenu.fragments.entry}
query GetLayout(
$headerLocation: MenuLocationEnum
$footerLocation: MenuLocationEnum
) {
generalSettings {
...BlogInfoFragment
}
headerMenuItems: menuItems(where: { location: $headerLocation }) {
nodes {
...NavigationMenuItemFragment
}
}
footerMenuItems: menuItems(where: { location: $footerLocation }) {
nodes {
...NavigationMenuItemFragment
}
}
}
`;

const GET_POST_QUERY = gql`
${FeaturedImage.fragments.entry}
query GetPost($databaseId: ID!, $asPreview: Boolean = false) {
post(id: $databaseId, idType: DATABASE_ID, asPreview: $asPreview) {
title
content
date
author {
node {
name
}
}
...FeaturedImageFragment
}
}
`;

export default function Component(props) {
const context = useContext(FaustContext);

const { post } = useFaustQuery(GET_POST_QUERY);
const { generalSettings, headerMenuItems, footerMenuItems } =
useFaustQuery(GET_LAYOUT_QUERY);

// Loading state for previews
if (props.loading) {
return <>Loading...</>;
}

const { title: siteTitle, description: siteDescription } =
props?.data?.generalSettings;
const primaryMenu = props?.data?.headerMenuItems?.nodes ?? [];
const footerMenu = props?.data?.footerMenuItems?.nodes ?? [];
const { title, content, featuredImage, date, author } = props.data.post;
const { title: siteTitle, description: siteDescription } = generalSettings;
const primaryMenu = headerMenuItems?.nodes ?? [];
const footerMenu = footerMenuItems?.nodes ?? [];
const { title, content, featuredImage, date, author } = post ?? {};

return (
<>
Expand Down Expand Up @@ -55,48 +102,19 @@ export default function Component(props) {
);
}

Component.query = gql`
${BlogInfoFragment}
${NavigationMenu.fragments.entry}
${FeaturedImage.fragments.entry}
query GetPost(
$databaseId: ID!
$headerLocation: MenuLocationEnum
$footerLocation: MenuLocationEnum
$asPreview: Boolean = false
) {
post(id: $databaseId, idType: DATABASE_ID, asPreview: $asPreview) {
title
content
date
author {
node {
name
}
}
...FeaturedImageFragment
}
generalSettings {
...BlogInfoFragment
}
headerMenuItems: menuItems(where: { location: $headerLocation }) {
nodes {
...NavigationMenuItemFragment
}
}
footerMenuItems: menuItems(where: { location: $footerLocation }) {
nodes {
...NavigationMenuItemFragment
}
}
}
`;

Component.variables = ({ databaseId }, ctx) => {
return {
databaseId,
headerLocation: MENUS.PRIMARY_LOCATION,
footerLocation: MENUS.FOOTER_LOCATION,
asPreview: ctx?.asPreview,
};
};
Component.queries = [
{
query: GET_LAYOUT_QUERY,
variables: (seedNode, ctx) => ({
headerLocation: MENUS.PRIMARY_LOCATION,
footerLocation: MENUS.FOOTER_LOCATION,
}),
},
{
query: GET_POST_QUERY,
variables: ({ databaseId }, ctx) => ({
databaseId,
asPreview: ctx?.asPreview,
}),
},
];
180 changes: 180 additions & 0 deletions package-lock.json

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

25 changes: 14 additions & 11 deletions packages/faustwp-core/src/components/FaustProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApolloProvider } from '@apollo/client';
import React from 'react';
import { ApolloProvider } from '@apollo/client';
// eslint-disable-next-line import/extensions
import { useRouter } from 'next/router';
// eslint-disable-next-line import/extensions
Expand All @@ -8,6 +8,7 @@ import { useApollo } from '../client.js';
import { Toolbar } from './Toolbar/index.js';
import { SeedNode } from '../queries/seedQuery.js';
import { getConfig } from '../config/index.js';
import { FaustContext } from '../store/FaustContext.js';

export type FaustPageProps = AppProps['pageProps'] & {
__SEED_NODE__?: SeedNode;
Expand All @@ -23,15 +24,17 @@ export function FaustProvider(props: {
const apolloClient = useApollo(pageProps);

return (
<ApolloProvider client={apolloClient}>
{experimentalToolbar && (
<Toolbar
key={`faust-toolbar-${router.asPath}`} // Required in order to load each route's own seed node.
// eslint-disable-next-line no-underscore-dangle
seedNode={pageProps.__SEED_NODE__}
/>
)}
{children}
</ApolloProvider>
<FaustContext.Provider value={pageProps}>
<ApolloProvider client={apolloClient}>
{experimentalToolbar && (
<Toolbar
key={`faust-toolbar-${router.asPath}`} // Required in order to load each route's own seed node.
// eslint-disable-next-line no-underscore-dangle
seedNode={pageProps.__SEED_NODE__}
/>
)}
{children}
</ApolloProvider>
</FaustContext.Provider>
);
}
Loading
Loading