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

[gatsby-source-wordpress] Cannot destructure property 'typeInfo' of 'queryInfo' as it is undefined. #35460

Closed
2 tasks done
tsdexter opened this issue Apr 20, 2022 · 11 comments · Fixed by #35467 or #35817
Closed
2 tasks done
Labels
topic: source-plugins Relates to the Gatsby source plugins (e.g. -filesystem) topic: source-wordpress Related to Gatsby's integration with WordPress type: bug An issue or pull request relating to a bug in Gatsby

Comments

@tsdexter
Copy link
Contributor

Preliminary Checks

Description

i'm getting the following error during development:

TypeError: Cannot destructure property 'typeInfo' of 'queryInfo' as it is undefined.
    at fetchReferencedMediaItemsAndCreateNodes (.../node_modules/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js:528:11)

I am assuming it is because I removed the RootQuery.mediaItems type from WPGraphQL (due to not having a reliable way to tell gatsby-source-wordpress not to source it which is drastically slowing down my build times and I don't need the mediaItems on my gatsby site) via:

add_filter( 'graphql_RootQuery_fields', function( $fields ) {   	 	
  unset($fields['mediaItems']);
  return $fields;
}, 99, 1 );

Can this be changed to check for undefined first and/or create a plugin option to ignore certain nodes entirely on the RootQuery?

Reproduction Link

https://github.com/tsdexter/gatsby-repro

Steps to Reproduce

  1. run npm run develop
  2. change wordpress data
  3. develop process crashes

In order to run it with your own WP instance you will likely need to add to your functions.php file:

add_filter( 'graphql_NodeWithFeaturedImage_fields', function( $fields ) {
		unset($fields['featuredImage']);
    return $fields;
}, 99, 1 );

add_filter( 'graphql_User_fields', function( $fields ) {
		unset($fields['mediaItems']);
    return $fields;
}, 99, 1 );

add_filter( 'graphql_RootQuery_fields', function( $fields ) {   	 	
		unset($fields['mediaItems']);
    return $fields;
}, 99, 1 );

as it's likely the related code thats breaking it.

Expected Result

build process shouldn't crash

Actual Result

build process crashes with error message

Environment

System:
    OS: macOS 12.2
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  Languages:
    Python: 2.7.18 - /usr/bin/python
  Browsers:
    Chrome: 100.0.4896.127
    Firefox: 97.0.2
    Safari: 15.3
  npmPackages:
    gatsby: ^4.10.0-next.3 => 4.10.0-next.3 
    gatsby-plugin-fastify: ^0.7.4 => 0.7.4 
    gatsby-plugin-image: ^2.10.0-next.3 => 2.10.0-next.3 
    gatsby-plugin-sharp: ^4.10.0-next.3 => 4.10.0-next.3 
    gatsby-source-wordpress: ^6.9.1 => 6.9.1 
    gatsby-transformer-sharp: ^4.10.0-next.2 => 4.10.0-next.2 
  npmGlobalPackages:
    gatsby-cli: 4.12.1

Config Flags

No response

@tsdexter tsdexter added the type: bug An issue or pull request relating to a bug in Gatsby label Apr 20, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Apr 20, 2022
@LekoArts LekoArts added topic: source-wordpress Related to Gatsby's integration with WordPress topic: source-plugins Relates to the Gatsby source plugins (e.g. -filesystem) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Apr 21, 2022
@TylerBarnes
Copy link
Contributor

Hi there 👋 what you're attempting wont work for a few reasons even beyond that error. I opened a PR and released a canary allowing you to exclude MediaItem nodes. You can try it with this version gatsby-source-wordpress@6.14.0-alpha-wp-exclude-media-items. And this is how you use it.

@TylerBarnes
Copy link
Contributor

This will go out in the next oss release (next week)

@tsdexter
Copy link
Contributor Author

thanks so much @TylerBarnes for the fast response and fix 🚀🚀

@tsdexter
Copy link
Contributor Author

tsdexter commented May 3, 2022

@TylerBarnes I'm getting this error again using "gatsby-source-wordpress": "^6.14.0-alpha-wp-exclude-media-items.0", along with type: {MediaItem: {exclude: true}} and no customizations to WPGraphQL MediaItem or RootQuery

It doesn't happen during a clean build or a cached build with no wordpress updates...only during a cached build with updates to fetch during the pull updates since last build step.

success  gatsby-source-wordpress  ensuring plugin requirements are met - 0.024s
success  gatsby-source-wordpress  diff schemas - 2.654s
success  gatsby-source-wordpress  ingest WPGraphQL schema - 2.778s
success createSchemaCustomization - 2.851s
success  gatsby-source-wordpress  fetch root fields - 1.140s

ERROR
 
 TypeError: Cannot destructure property 'typeInfo' of 'queryInfo' as it is
 undefined.
     at fetchReferencedMediaItemsAndCreateNodes
 (/builder/workspace/repo/node_modules/gatsby-source-wordpress/src/steps/source-n
 odes/fetch-nodes/fetch-referenced-media-items.js:552:11)
     at createSingleNode (/builder/workspace/repo/node_modules/gatsby-source-word
 press/src/steps/source-nodes/update-nodes/wp-actions/update.js:182:9)
     at processTicksAndRejections (node:internal/process/task_queues:96:5)
     at fetchAndCreateSingleNode (/builder/workspace/repo/node_modules/gatsby-sou
 rce-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js:130:39)
     at wpActionUPDATE (/builder/workspace/repo/node_modules/gatsby-source-wordpr
 ess/src/steps/source-nodes/update-nodes/wp-actions/update.js:311:20)
     at handleWpActions (/builder/workspace/repo/node_modules/gatsby-source-wordp
 ress/src/steps/source-nodes/update-nodes/wp-actions/index.js:60:7)
     at fetchAndRunWpActions (/builder/workspace/repo/node_modules/gatsby-source-
 wordpress/src/steps/source-nodes/update-nodes/wp-actions/index.js:102:7)
     at fetchAndApplyNodeUpdates (/builder/workspace/repo/node_modules/gatsby-sou
 rce-wordpress/src/steps/source-nodes/update-nodes/fetch-node-updates.js:44:36)
     at sourceNodes (/builder/workspace/repo/node_modules/gatsby-source-wordpress
 /src/steps/source-nodes/index.ts:60:5)
     at runSteps (/builder/workspace/repo/node_modules/gatsby-source-wordpress/sr
 c/utils/run-steps.ts:41:9)
     at runAPI (/builder/workspace/repo/node_modules/gatsby/src/utils/api-runner-
 node.js:462:16)
 
 
 �[2K�[1A�[2K�[G
  ERROR #gatsby-source-wordpress_112003
 
  gatsby-source-wordpress
 
 	Encountered a critical error when running the sourceNodes.sourceNodes build
 step.
 	See above for more information.

not finished source and transform nodes - 2.862s
not finished  gatsby-source-wordpress  pull updates since last build - 2.730s

@TylerBarnes
Copy link
Contributor

Can you try removing the caret? "gatsby-source-wordpress": "6.14.0-alpha-wp-exclude-media-items.0",

Also I believe this may have been released on the latest stable release as well

@tsdexter
Copy link
Contributor Author

tsdexter commented May 4, 2022

@TylerBarnes I am still getting the error without the caret. I updated package, did a clean rebuild, updated a wordpress page and then attempted to build and it failed on pull updates since last build again.

success  gatsby-source-wordpress  fetch root fields - 1.476s
 
  ERROR
 
 TypeError: Cannot destructure property 'typeInfo' of 'queryInfo' as it is
 undefined.
     at fetchReferencedMediaItemsAndCreateNodes
 (/builder/workspace/repo/node_modules/gatsby-source-wordpress/src/steps/source-n
 odes/fetch-nodes/fetch-referenced-media-items.js:552:11)
     at createSingleNode (/builder/workspace/repo/node_modules/gatsby-source-word
 press/src/steps/source-nodes/update-nodes/wp-actions/update.js:182:9)
     at processTicksAndRejections (node:internal/process/task_queues:96:5)
     at fetchAndCreateSingleNode (/builder/workspace/repo/node_modules/gatsby-sou
 rce-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js:130:39)
     at wpActionUPDATE (/builder/workspace/repo/node_modules/gatsby-source-wordpr
 ess/src/steps/source-nodes/update-nodes/wp-actions/update.js:311:20)
     at handleWpActions (/builder/workspace/repo/node_modules/gatsby-source-wordp
 ress/src/steps/source-nodes/update-nodes/wp-actions/index.js:60:7)
     at fetchAndRunWpActions (/builder/workspace/repo/node_modules/gatsby-source-
 wordpress/src/steps/source-nodes/update-nodes/wp-actions/index.js:102:7)
     at fetchAndApplyNodeUpdates (/builder/workspace/repo/node_modules/gatsby-sou
 rce-wordpress/src/steps/source-nodes/update-nodes/fetch-node-updates.js:44:36)
     at sourceNodes (/builder/workspace/repo/node_modules/gatsby-source-wordpress
 /src/steps/source-nodes/index.ts:60:5)
     at runSteps (/builder/workspace/repo/node_modules/gatsby-source-wordpress/sr
 c/utils/run-steps.ts:41:9)
     at runAPI (/builder/workspace/repo/node_modules/gatsby/src/utils/api-runner-
 node.js:462:16)
 
 
 �[2K�[1A�[2K�[G
  ERROR #gatsby-source-wordpress_112003
 
  gatsby-source-wordpress
 
 	Encountered a critical error when running the sourceNodes.sourceNodes build
 step.
 	See above for more information.
 
 
not finished source and transform nodes - 3.163s
 
not finished  gatsby-source-wordpress  pull updates since last build - 2.983s

NPM still shows 6.13.0 as the current version, was it pushed into that or should I be looking for 6.14.0?

@tsdexter
Copy link
Contributor Author

tsdexter commented May 5, 2022

@TylerBarnes just FYI, this is reproducing both locally and on gatsby cloud as well

@TylerBarnes
Copy link
Contributor

@tsdexter did you remove the code you originally added to your WP instance?

add_filter( 'graphql_RootQuery_fields', function( $fields ) {   	 	
  unset($fields['mediaItems']);
  return $fields;
}, 99, 1 );

@tsdexter
Copy link
Contributor Author

@TylerBarnes, sorry for the delay, i was on vacation... I have removed that code. This also happens during npm run develop the initial build runs fine and development starts then when a change is made in WP during the develop session, it crashes on the same error..

Here is all of the custom code relating to WPGraphQL:

<?php

// save "rendered" content to speed up graphql queries
add_action( 'save_post', function ( $post_id ) {
  // if this is a revision returnn
  if ( wp_is_post_revision( $post_id ) )
    return;

  // get post
  $post = get_post( $post_id );

  if ($post->post_type === "action_monitor") return;

  // apply fitlers too post_content
  $filtered_content = apply_filters( 'the_content', $post->post_content );

  // append prerender info console log
  $now = date('d F Y, h:i:s A');
  $filtered_content = "<script type='text/javascript'>console.log({renderer: 'save_post', time: '{$now}'});</script>" . $filtered_content;

  // save to post meta
  update_post_meta( $post_id, 'post_content_filtered', $filtered_content );

  //@TODO: when posts that may be included elsewhere change, update the including file
  // for example, if reusable block is changed, check for posts with "ref: block-id" and render/save them
  // or if a news post is changed, check for posts with news feeds and render/save them

  //@TODO: don't save pre-rendered content for any post with reusable blocks or server rendered blocks (can check post content or have to use blacklist?)
} );

/**
 * Remove unnecessary fields from the RootQuery
 */
add_filter( 'graphql_RootQuery_fields', function( $fields ) {   	 			
    // unset($fields['allSettings']);
    // // unset($fields['categories']);
    // unset($fields['category']);
    // unset($fields['comment']);
    // unset($fields['comments']);
    // // unset($fields['contentNode']);
    // unset($fields['contentNodes']);
    // unset($fields['contentType']);
    // unset($fields['contentTypes']);
    // unset($fields['discussionSettings']);
    // unset($fields['generalSettings']);
    // // unset($fields['isWpGatsby']);
    // unset($fields['mediaItem']);
    // unset($fields['mediaItemBy']);
    // // unset($fields['mediaItems']);
    // unset($fields['menu']);
    // unset($fields['menuItem']);
    // // unset($fields['menuItems']);
    // // unset($fields['menus']);
    // unset($fields['node']);
    // unset($fields['nodeByUri']);
    // unset($fields['page']);
    // unset($fields['pageBy']);
    // // unset($fields['pages']);
    // unset($fields['plugin']);
    // unset($fields['plugins']);
    // unset($fields['post']);
    // unset($fields['postBy']);
    // unset($fields['postFormat']);
    // unset($fields['postFormats']);
    // // unset($fields['posts']);
    // unset($fields['readingSettings']);
    // unset($fields['registeredScripts']);
    // unset($fields['registeredStylesheets']);
    // unset($fields['revisions']);
    // unset($fields['schemaMd5']);
    // unset($fields['seo']);
    // unset($fields['tag']);
    // // unset($fields['tags']);
    // unset($fields['taxonomies']);
    // unset($fields['taxonomy']);
    // unset($fields['termNode']);
    // unset($fields['terms']);
    // unset($fields['theme']);
    // unset($fields['themes']);
    // unset($fields['user']);
    // unset($fields['userRole']);
    // unset($fields['userRoles']);
    // unset($fields['users']);
    // unset($fields['viewer']);
    // // unset($fields['wpGatsby']);
    // // unset($fields['wpGatsbyCompatibility']);
    // unset($fields['writingSettings']);
    return $fields;
}, 99, 1 );

/**
 * Return pre-rendered content in ContentNode
 */
// add_filter( 'graphql_PostObjectFieldFormatEnum_values', function($values) {
//   $values['PRERENDERED'] = [
//     'name'        => 'PRERENDERED',
//     'description' => __( 'Gets the content from the post_content_filtered postmeta field', 'wp-graphql' ),
//     'value'       => 'prerendered',
//   ];
//   return $values;
// }, 99, 1 ); 
add_filter( 'graphql_Page_fields', function( $fields ) {    
    $fields['content']['resolve'] = contentResolver;
    return $fields;
}, 99, 1 );
add_filter( 'graphql_Post_fields', function( $fields ) {    
    $fields['content']['resolve'] = contentResolver;
    return $fields;
}, 99, 1 );
add_filter( 'graphql_ReusableBlock_fields', function( $fields ) {    
    $fields['content']['resolve'] = contentResolver;
    return $fields;
}, 99, 1 );

/**
 * resolver to return pre-rendered content
 */
function contentResolver( $root, $args, $context, $info ) {
    $content = get_post_meta( $root->ID, 'post_content_filtered', true );
    if ( !$content || $content == '' || strpos($content, '[vc_row') !== false ) {
        $post = get_post( $root->ID );
        if (class_exists('WPBMap')) WPBMap::addAllMappedShortcodes();
        $content = apply_filters( 'the_content', $post->post_content );
        $now = date('d F Y, h:i:s A');
        $content = "<script type='text/javascript'>console.log({renderer: 'contentResolver', time: '{$now}'});</script>" . $content;
        update_post_meta( $root->ID, 'post_content_filtered', $content );
    }
    return $content; //'FROM POSTMETA' . $content;
};

 
/**
 * Disable post types from WPGraphQL
 */
add_filter( 'register_post_type_args', function($args, $post_type) {
  if ( $post_type == "wp_block" ) {
    $args['show_in_graphql'] = true;
    $args['publicly_queryable'] = true;
    $args['graphql_single_name'] = 'reusableBlock';
    $args['graphql_plural_name'] = 'reusableBlocks';
  } else if ( $post_type == "pts_program") {
    $args['show_in_graphql'] = false;
    $args['graphql_single_name'] = 'wpPtsProgram';
    $args['graphql_plural_name'] = 'wpPtsPrograms';  
  } else if ( $post_type == "pts_course") {
    $args['show_in_graphql'] = true;
    $args['graphql_single_name'] = 'wpPtsCourse';
    $args['graphql_plural_name'] = 'wpPtsCourses';  
  } else if ( $post_type == "alerts") {
    $args['show_in_graphql'] = false;
    $args['graphql_single_name'] = 'wpAlert';
    $args['graphql_plural_name'] = 'wpAlerts';  
  } else if ( $post_type == "catalog_program") {
    $args['show_in_graphql'] = false;
    $args['graphql_single_name'] = 'wpProgram';
    $args['graphql_plural_name'] = 'wpPrograms';  
  } else if ( $post_type == "link") {
    $args['show_in_graphql'] = false;
    $args['graphql_single_name'] = 'wpLink';
    $args['graphql_plural_name'] = 'wpLinks';  
  } else if ( $post_type == "tour") {
    $args['show_in_graphql'] = false;
    $args['graphql_single_name'] = 'wpTour';
    $args['graphql_plural_name'] = 'wpTours';  
  } else if ( $post_type == "equivalencies") {
    $args['show_in_graphql'] = false;
    $args['graphql_single_name'] = 'wpEquivalency';
    $args['graphql_plural_name'] = 'wpEquivalencies';  
  }
  return $args;
}, 20, 2 );

add_action('graphql_register_types', function() {
  register_graphql_field( 'ContentNode', 'topParentId', [
		'type' => 'Number',
		'description' => __( 'The ID of the topmost parent page', 'georgian' ),
		'resolve' => function( \WPGraphQL\Model\Post $post, $args, $context, $info ) {
      $wppost = get_post( $post->databaseId );
      if ($wppost->post_parent)	{
        $ancestors=get_post_ancestors($wppost->ID);
        $root=count($ancestors)-1;
        $topparent = $ancestors[$root];
      } else {
        $topparent = $wppost->ID;
      }
		  return $topparent;
		}
	]);
  
  register_graphql_field( 'ContentNode', 'localParentId', [
		'type' => 'Number',
		'description' => __( 'The ID of the local parent page', 'georgian' ),
		'resolve' => function( \WPGraphQL\Model\Post $post, $args, $context, $info ) {
      $wppost = get_post( $post->databaseId );
      if ($wppost->post_parent)	{
        $ancestors=get_post_ancestors($wppost->ID);
        $count=count($ancestors);
        if($count == 1)
        {
          $localParent = $ancestors[$count-1];
        } else {
          $localParent = $ancestors[$count-2];
        }
      } else {
        $localParent = $wppost->ID;
      }
		  return $localParent;
		}
	]);

  register_graphql_connection( [
    'fromType'           => 'MenuItem',
    'toType'             => 'ReusableBlock',
    'fromFieldName'      => 'reusableBlock',
    'connectionTypeName' => 'MenuItemNodeToReusableBlockNodeConnection',
    'description'        => 'The reusable block content for the Mega Menu',
    'oneToOne'           => true,
    'resolve'            => function ( WPGraphQL\Model\MenuItem $post, $args, WPGraphQL\AppContext $context, GraphQL\Type\Definition\ResolveInfo $info ) {

      $blockId = gc_get_field('reusable_block_id', $post->databaseId);
      if ( ! isset( $blockId ) ) {
        return null;
      }
      $contentPost = get_post($blockId);
      $resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $post, $args, $context, $info );
      $resolver->set_query_arg( 'p', $blockId );
      $resolver->set_query_arg( 'post_type', 'wp_block' );

      return $resolver->one_to_one()->get_connection();      
    },
  ] );
});

@tsdexter
Copy link
Contributor Author

@TylerBarnes I just updated to 6.15.0 and this issue still occurs - only on partial sourcing (ie: after a content change during develop, or during a cached build)

@tsdexter
Copy link
Contributor Author

tsdexter commented Jun 1, 2022

@TylerBarnes I've fixed this, hopefully in an acceptable way, in PR #35817

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: source-plugins Relates to the Gatsby source plugins (e.g. -filesystem) topic: source-wordpress Related to Gatsby's integration with WordPress type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
3 participants