-
Notifications
You must be signed in to change notification settings - Fork 466
Description
In WordPress, assets (scripts and stylesheets) can be enqueued and applied to the front end.
In an application data graph, these styles and scripts can be considered nodes and they can have connections to other nodes, such as a Post, Page.
As a user, I would like to be able to query for assets connected to existing resources.
For example, given the following code to enqueue scripts in a WordPress plugin or theme:
add_action( 'wp_enqueue_scripts', function() {
// Register the scripts
wp_register_script( 'equeue-everywhere', '/everywhere.js' );
wp_register_script( 'only-single-posts', '/single-posts.js' );
wp_register_script( 'only-single-categories', '/single-category.js' );
wp_register_script( 'only-front-page', '/front-page.js' );
// Enqueue the script EVERYWHERE
wp_enqueue_script( 'equeue-everywhere' );
// Enqueue just to single posts (of any post type)
if ( is_single() ) {
wp_enqueue_script( 'only-single-posts' );
}
// Enqueue to single categories
if ( is_category() ) {
wp_enqueue_script( 'only-single-categories' );
}
// Enqueue just on page set as the front page
if ( is_front_page() ) {
wp_enqueue_script( 'only-front-page' );
}
} );
If I visited a single post on my front end, for example site.com/post/hello-world, I would see /single-post.js added as a <script> tag to that page.
If I visited site.com/ I would see front-page.js added as a <script> tag to the page, and the same with visiting site.com/category/uncategorized and seeing /single-categories.js added as a <script> tag.
Often times, these scripts are used in conjunction with the content, to modify dom elements, such as convert unordered lists of images into slideshows, etc.
In WPGraphQL, I would like to query these connected assets.
Something like so:
query GET_POST_WITH_ENQUEUED_ASSETS {
posts(first:1) {
nodes {
title
enqueuedScripts {
nodes {
handle
src
}
}
}
}
}I might get a payload such as:
{
"data": {
"posts": {
"nodes": [
{
"title": "hello-world",
"enqueuedScripts": {
"nodes": [
{
"handle": "only-single-posts",
"src": "/single-post.js"
},
{
"handle": "everywhere",
"src": "/enqueued-everywhere.js"
}
]
}
}
]
}
}
}This way, should I want to use these assets in my consuming application, I could ask for them and have them apply to my consuming application.
I could do the same querying for terms, comments, etc.
This will allow headless consumers to use the HTML output from the post.content field with the styles and interactions intended by the CMS (such as slideshows and other dynamic interactions).