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

feat: allow overloading of query from snapwp configs #83

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/nextjs/starter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [ "node_modules", "src/__generated" ]
"exclude": [ "node_modules" ]
}
20 changes: 4 additions & 16 deletions package-lock.json

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

17 changes: 16 additions & 1 deletion packages/core/src/config/snapwp-config-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use snapWPConfig';
import { isValidUrl, generateGraphqlUrl } from '@/utils';
import { Logger } from '@/logger';
import type { BlockDefinitions } from '@snapwp/types';
import type {
BlockDefinitions,
CurrentTemplateDocument,
GlobalStylesDocument,
} from '@snapwp/types';
import type { HTMLReactParserOptions } from 'html-react-parser';

export interface SnapWPEnv {
Expand Down Expand Up @@ -46,6 +50,13 @@ export interface SnapWPConfig {
* html-react-parser overload options
*/
parserOptions?: HTMLReactParserOptions;
/**
* Queries
*/
queries?: {
globalStylesDocument?: GlobalStylesDocument;
currentTemplateDocument?: CurrentTemplateDocument;
};
}

/**
Expand Down Expand Up @@ -116,6 +127,10 @@ class SnapWPConfigManager {
type: 'object',
required: false,
},
queries: {
type: 'object',
required: false,
},
};

/**
Expand Down
19 changes: 17 additions & 2 deletions packages/query/src/query-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,17 @@ export class QueryEngine {
QueryEngine.initialize();
}

const { queries } = getConfig();

let query = GetGlobalStylesDocument;

if ( queries?.globalStylesDocument ) {
query = queries.globalStylesDocument;
}

try {
const data = await QueryEngine.apolloClient.query( {
query: GetGlobalStylesDocument,
query,
fetchPolicy: 'network-only', // @todo figure out a caching strategy, instead of always fetching from network
errorPolicy: 'all',
} );
Expand Down Expand Up @@ -99,9 +107,16 @@ export class QueryEngine {
}
const variables = { uri };

const { queries } = getConfig();

let query = GetCurrentTemplateDocument;

if ( queries?.currentTemplateDocument ) {
query = queries.currentTemplateDocument;
}
try {
const data = await QueryEngine.apolloClient.query( {
query: GetCurrentTemplateDocument,
query,
variables,
fetchPolicy: 'network-only', // @todo figure out a caching strategy, instead of always fetching from network
errorPolicy: 'all',
Expand Down
3 changes: 2 additions & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/react": "*",
"resolve-tspaths": "*",
"tsc-watch": "*",
"typescript": "*"
"typescript": "*",
"@apollo/client": "^3.11.4"
}
}
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './blocks';
export * from './queries';
4 changes: 4 additions & 0 deletions packages/types/src/queries/current-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { TypedDocumentNode } from '@apollo/client';

// Until we figure out generic types for GraphQL queries, we'll use `any` for now.
export type CurrentTemplateDocument = TypedDocumentNode< any, any >;

Check failure on line 4 in packages/types/src/queries/current-template.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check failure on line 4 in packages/types/src/queries/current-template.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
4 changes: 4 additions & 0 deletions packages/types/src/queries/global-styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { TypedDocumentNode } from '@apollo/client';

// Until we figure out generic types for GraphQL queries, we'll use `any` for now.
Copy link
Collaborator

@justlevine justlevine Feb 21, 2025

Choose a reason for hiding this comment

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

Why any vs unknown? Beyond my personal hatred of any, what are the implications of each ?

export type GlobalStylesDocument = TypedDocumentNode< any, any >;

Check failure on line 4 in packages/types/src/queries/global-styles.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type

Check failure on line 4 in packages/types/src/queries/global-styles.ts

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected any. Specify a different type
2 changes: 2 additions & 0 deletions packages/types/src/queries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './current-template';
export * from './global-styles';
Loading