Skip to content

Commit

Permalink
All making context specific requests using the data module
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 24, 2021
1 parent 2ba7b67 commit 8b29ace
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/core-data/src/queried-data/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export function getQueryParts( query ) {
value = parts.fields.join();
}

// The context allows us to consider the item incomplete.
if ( key === 'context' ) {
parts.context = value;
}

// While it could be any deterministic string, for simplicity's
// sake mimic querystring encoding for stable key.
//
Expand Down
7 changes: 5 additions & 2 deletions packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ export function itemIsComplete( state = {}, action ) {
}

// An item is considered complete if it is received without an associated
// fields query. Ideally, this would be implemented in such a way where the
// fields query and if we didn't specific a context which can change the shape of the item.
// Ideally, this would be implemented in such a way where the
// complete aggregate of all fields would satisfy completeness. Since the
// fields are not consistent across all entity types, this would require
// introspection on the REST schema for each entity to know which fields
// compose a complete item for that entity.
const queryParts = query ? getQueryParts( query ) : {};
const isCompleteQuery =
! query || ! Array.isArray( getQueryParts( query ).fields );
! query ||
( ! Array.isArray( queryParts.fields ) && ! queryParts.context );

return {
...state,
Expand Down
15 changes: 12 additions & 3 deletions packages/core-data/src/queried-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ const queriedItemsCacheByState = new WeakMap();
* @return {?Array} Query items.
*/
function getQueriedItemsUncached( state, query ) {
const { stableKey, page, perPage, include, fields } = getQueryParts(
query
);
const {
stableKey,
page,
perPage,
include,
fields,
context,
} = getQueryParts( query );
let itemIds;

if ( Array.isArray( include ) && ! stableKey ) {
// If the parsed query yields a set of IDs, but otherwise no filtering,
// it's safe to consider targeted item IDs as the include set. This
Expand Down Expand Up @@ -76,6 +82,9 @@ function getQueriedItemsUncached( state, query ) {
const value = get( item, field );
set( filteredItem, field, value );
}
} else if ( context ) {
// For context specific queries, take the whole item as a result.
filteredItem = item;
} else {
// If expecting a complete item, validate that completeness, or
// otherwise abort.
Expand Down
12 changes: 12 additions & 0 deletions packages/core-data/src/queried-data/test/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,16 @@ describe( 'getQueryParts', () => {
include: null,
} );
} );

it( 'returns the context as a dedicated query part', () => {
const parts = getQueryParts( { context: 'view' } );

expect( parts ).toEqual( {
page: 1,
perPage: 10,
stableKey: 'context=view',
include: null,
context: 'view',
} );
} );
} );
15 changes: 15 additions & 0 deletions packages/core-data/src/queried-data/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ describe( 'itemIsComplete', () => {
} );
} );

it( 'should assign received items as incomplete if a context is provided', () => {
const original = deepFreeze( {} );
const state = itemIsComplete( original, {
type: 'RECEIVE_ITEMS',
query: {
context: 'view',
},
items: [ { id: 1, content: 'chicken' } ],
} );

expect( state ).toEqual( {
1: false,
} );
} );

it( 'should defer to existing completeness when receiving filtered query', () => {
const original = deepFreeze( {
1: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export function* getEntityRecords( kind, name, query = {} ) {
}

const path = addQueryArgs( entity.baseURL, {
...entity.baseURLParams,
...query,
context: 'edit',
} );

let records = Object.values( yield apiFetch( { path } ) );
Expand All @@ -218,7 +218,7 @@ export function* getEntityRecords( kind, name, query = {} ) {
// When requesting all fields, the list of results can be used to
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
// See https://github.com/WordPress/gutenberg/pull/26575
if ( ! query?._fields ) {
if ( ! query?._fields && ! query.context ) {
const key = entity.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
Expand Down
14 changes: 12 additions & 2 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,18 @@ describe( 'getEntityRecords', () => {
page: { slug: 'page', id: 2 },
};
const ENTITIES = [
{ name: 'postType', kind: 'root', baseURL: '/wp/v2/types' },
{ name: 'postType', kind: 'root', baseURL: '/wp/v2/types' },
{
name: 'postType',
kind: 'root',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
},
{
name: 'postType',
kind: 'root',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
},
];

it( 'yields with requested post type', async () => {
Expand Down

0 comments on commit 8b29ace

Please sign in to comment.