Skip to content

Commit 38c369e

Browse files
authored
Merge pull request #56 from ivansglazunov/versions_fix
Enhance generator logic for Next.js build context handling
2 parents e5ec906 + f612110 commit 38c369e

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

lib/generator.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,11 @@ export function Generator(schema: any): Generate { // We take the __schema objec
760760

761761
// Check if we're in a browser environment (for SSR/build safety)
762762
const isBrowser = typeof window !== 'undefined';
763+
// Check if we're in Next.js build/SSR context (not just any Node.js environment like tests)
764+
const isNextBuild = typeof process !== 'undefined' &&
765+
(process.env.NEXT_PHASE !== undefined ||
766+
process.env.__NEXT_PRIVATE_PREBUNDLED_REACT !== undefined ||
767+
(process.env.NODE_ENV === 'production' && process.env.NEXT_RUNTIME !== undefined));
763768

764769
function generate(opts: GenerateOptions): GenerateResult {
765770
let varCounter = opts.varCounter || 1;
@@ -788,20 +793,24 @@ export function Generator(schema: any): Generate { // We take the __schema objec
788793
if (operation === 'insert' || operation === 'update' || operation === 'delete') {
789794
targetRoot = mutationRoot || queryRoot;
790795
} else if (operation === 'subscription' || operation === 'stream') {
791-
// For subscription/stream, try subscription root first, but allow fallback to query
792-
// During SSR/build, always use query root to avoid errors
793-
if (subscriptionRoot && isBrowser) {
796+
// For subscription/stream, prefer subscription root if available
797+
// Only fallback to query_root if subscriptionRoot is missing AND we're in Next.js build context
798+
if (subscriptionRoot) {
799+
// Use subscription root if available (works in tests, browser, and most SSR contexts)
794800
targetRoot = subscriptionRoot;
795-
} else {
796-
// No subscription root available or SSR/build context, fallback to query
801+
} else if (isNextBuild) {
802+
// Only in Next.js build context, fallback to query_root to avoid build errors
797803
targetRoot = queryRoot;
798804
fallbackToQuery = true;
799805
actualOperation = 'query'; // Use query operation when falling back to query_root
800-
if (!isBrowser) {
801-
debug(`[generator] SSR/build context detected, using query_root with query operation instead of subscription_root for ${operation} operation`);
802-
} else {
803-
debug(`[generator] No subscription_root found, falling back to query_root with query operation for ${operation} operation`);
804-
}
806+
debug(`[generator] Next.js build context detected, using query_root with query operation instead of subscription_root for ${operation} operation`);
807+
} else {
808+
// In tests or other contexts without subscriptionRoot, still try to use subscription_root structure
809+
// but this will likely fail - we'll handle it in the fallback logic below
810+
targetRoot = queryRoot;
811+
fallbackToQuery = true;
812+
actualOperation = 'query';
813+
debug(`[generator] No subscription_root found, falling back to query_root with query operation for ${operation} operation`);
805814
}
806815
}
807816

@@ -878,10 +887,10 @@ export function Generator(schema: any): Generate { // We take the __schema objec
878887
}
879888

880889
if (!queryInfo) {
881-
// In SSR/build context, if subscription falls back to query_root and field is not found,
890+
// In Next.js build context, if subscription falls back to query_root and field is not found,
882891
// create a minimal valid queryInfo to prevent build failures
883-
if (!isBrowser && (operation === 'subscription' || operation === 'stream') && fallbackToQuery) {
884-
debug(`[generator] Field not found in query_root for ${operation} operation during SSR/build. Using minimal fallback query.`);
892+
if (isNextBuild && (operation === 'subscription' || operation === 'stream') && fallbackToQuery) {
893+
debug(`[generator] Field not found in query_root for ${operation} operation during Next.js build. Using minimal fallback query.`);
885894
// Create a minimal queryInfo structure that won't break the build
886895
// This will generate a query that may not work at runtime, but won't break the build
887896
queryName = table;

0 commit comments

Comments
 (0)