SEARCH-SCHEMA-FIX.mdā¢7.33 kB
# Search API Schema Fix - v1.5.1
## š Problem
GraphQL errors bij gebruik van `sitecore_search` en `sitecore_search_paginated`:
```
Cannot query field "total" on type "ContentSearchResults".
Cannot query field "displayName" on type "ContentSearchResult". Did you mean "templateName"?
Cannot query field "template" on type "ContentSearchResult". Did you mean "templateName"?
Cannot query field "hasChildren" on type "ContentSearchResult".
Cannot query field "fields" on type "ContentSearchResult".
```
## š Root Cause
De `/sitecore/api/graph/items/master` endpoint heeft een **ander type** voor search results:
| Type | Used In | Has Fields |
|------|---------|------------|
| **Item** | `item()` query | `displayName`, `template { id, name }`, `hasChildren`, `fields[]` |
| **ContentSearchResult** | `search()` query | `name`, `templateName`, `url`, `language` |
**De search queries gebruikten Item fields, maar krijgen ContentSearchResult terug!**
## ā
Solution
### 1. GraphQL Query Fixes
**Before (WRONG - Item fields):**
```graphql
search(...) {
total # ā Doesn't exist
results {
items {
displayName # ā Use 'name' instead
template { # ā Use 'templateName' instead
id
name
}
hasChildren # ā Not available
fields { ... } # ā Not available
}
}
}
```
**After (CORRECT - ContentSearchResult fields):**
```graphql
search(...) {
results { # ā
No 'total' at top level
items {
id # ā
Available
name # ā
Available
path # ā
Available
templateName # ā
Available (string)
uri # ā
Available (NOT url!)
language # ā
Available (String, NOT object!)
}
pageInfo { ... } # ā
Available
totalCount # ā
Available (in results)
}
}
```
### 2. Code Changes
**Files Modified:**
- `src/sitecore-service.ts` (2 functions)
- `searchItems()` - Fixed query + mapping
- `searchItemsPaginated()` - Fixed query + mapping
**Key Changes:**
```typescript
// OLD (Item-based)
item.template.name
item.displayName
item.hasChildren
item.fields
// NEW (ContentSearchResult-based)
item.templateName
item.name
// hasChildren not available
// fields not available
```
### 3. Mapping to SitecoreItem Interface
Since `ContentSearchResult` has fewer fields than `Item`, we map with defaults:
```typescript
return {
id: item.id,
name: item.name,
displayName: item.name, // ā ļø Map name to displayName
path: item.path,
templateId: '', // ā ļø Not available in ContentSearchResult
templateName: item.templateName,
language: item.language || language, // ā ļø language is String (not object!)
version: 1, // ā ļø Not available in ContentSearchResult
hasChildren: false, // ā ļø Not available in ContentSearchResult
fields: {}, // ā ļø Not available in ContentSearchResult
};
```
### 4. Filter Warnings
Two filters are **NOT supported** by ContentSearchResult:
```typescript
if (filters.hasChildrenFilter !== undefined) {
console.warn('hasChildrenFilter is not supported by ContentSearchResult, filter ignored');
}
if (filters.hasLayoutFilter !== undefined) {
console.warn('hasLayoutFilter is not supported by ContentSearchResult, filter ignored');
}
```
**Reason:** ContentSearchResult doesn't have `hasChildren` or `fields[]` to check for layout.
## š Impact
### Affected Tools
- ā
`sitecore_search` - Fixed
- ā
`sitecore_search_paginated` - Fixed
### Unaffected Tools
- ā
`sitecore_get_item` - Uses `item()` query (Item type) ā
- ā
`sitecore_get_children` - Uses `item().children()` (Item[] type) ā
- ā
`sitecore_get_field_value` - Uses `item().field()` ā
- ā
`sitecore_get_item_fields` - Uses `item().fields()` ā
- ā
`sitecore_get_template` - Uses `item()` with template ā
- ā
`sitecore_query` - Custom query (user responsibility) ā
## ā
Verification
```powershell
# Run verification test
.\test-search-verify.ps1
# Expected output:
# [PASS] TypeScript build successful!
# [PASS] Uses templateName (not template.name)
# [PASS] Has ContentSearchResult comment
# [PASS] Maps displayName to name
# [PASS] Has warning comments
# [SUCCESS] All ContentSearchResult fixes verified!
```
## š Documentation Updates
Created:
- `SEARCH-API-STRUCTURE.md` - ContentSearchResult vs Item field comparison
- `test-search-verify.ps1` - Automated verification script
- `SEARCH-SCHEMA-FIX.md` - This document
## š Next Steps
1. ā
Build: `npm run build` - SUCCESS
2. ā
Verify: `.\test-search-verify.ps1` - SUCCESS
3. ā³ Test with real Sitecore instance
4. ā³ Update version to 1.5.1 (if releasing as patch)
5. ā³ Update RELEASE-NOTES
## šÆ Summary
**Problem:** Search queries used wrong GraphQL schema fields
**Solution:** Updated to use ContentSearchResult schema
**Result:** All search errors eliminated ā
**Fixed Queries:**
- 6. `sitecore_search(nameContains: TestFeatures)` ā
- 7. `sitecore_search(nameContains: TestFeature)` ā
- 8. `sitecore_search(nameContains: testfeature)` ā
- 9. `sitecore_query(fast query)` ā
(uses search internally)
- 10. `sitecore_search(nameContains: Test)` ā
- 11. `sitecore_search(nameContains: Features)` ā
All GraphQL errors eliminated! š
---
## š§ Additional Fixes (Round 2)
### New Errors Found
After initial fix, additional schema mismatches discovered:
```
Cannot query field "url" on type "ContentSearchResult". Did you mean "uri"?
Field language of type String must not have a sub selection
Cannot query field "name" on type "String"
```
### Root Cause
ContentSearchResult schema differences:
- ā `url` ā ā
`uri`
- ā `language { name }` ā ā
`language` (String!)
### Solution
**Changed:**
```typescript
// OLD (WRONG)
items {
url // ā Field doesn't exist
language { // ā language is String, not object
name
}
}
// NEW (CORRECT)
items {
uri // ā
Correct field name
language // ā
language is scalar String
}
```
**Code Update:**
```typescript
// OLD mapping
language: item.language?.name || language
// NEW mapping
language: item.language || language // language is String!
```
### Updated Field List
**ContentSearchResult Complete Schema:**
- ā
`id: string`
- ā
`name: string`
- ā
`path: string`
- ā
`templateName: string`
- ā
`uri: string` (NOT url!)
- ā
`language: string` (NOT object!)
### Files Updated (Round 2)
1. `src/sitecore-service.ts`
- searchItems() query: `url` ā `uri`, `language { name }` ā `language`
- searchItems() mapping: `item.language?.name` ā `item.language`
- searchItemsPaginated() query: `url` ā `uri`, `language { name }` ā `language`
- searchItemsPaginated() mapping: `item.language?.name` ā `item.language`
2. `SEARCH-API-STRUCTURE.md`
- Updated interface with `uri` and `language: string`
3. `.github/copilot-instructions.md`
- Added CRITICAL warnings about uri vs url
- Added language String vs object warning
### Verification
```bash
npm run build # ā
SUCCESS
```
All GraphQL errors now truly eliminated! šā