-
Notifications
You must be signed in to change notification settings - Fork 1
Fetch with variation option #109
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
Open
exacs
wants to merge
7
commits into
main
Choose a base branch
from
feature/CMS-44193-fetch-variation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3406c60
Define and use filters
exacs a822db4
Extend fetchContent function
exacs 87374df
Rename and document
exacs 97d6a62
Expose fetchContentType(path), remove other internal methods
exacs 1dc24a3
Simplify variation
exacs 3b87636
Update test website
exacs 38c9f24
Update test site
exacs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
import { GraphClient } from '@episerver/cms-sdk'; | ||
import { GraphClient, GraphErrors } from '@episerver/cms-sdk'; | ||
import { OptimizelyComponent } from '@episerver/cms-sdk/react/server'; | ||
import React from 'react'; | ||
|
||
type Props = { | ||
params: Promise<{ | ||
slug: string[]; | ||
}>; | ||
// Assume that search params are correct: | ||
searchParams: Promise<{ variation?: string }>; | ||
}; | ||
|
||
export default async function Page({ params }: Props) { | ||
function handleGraphErrors(err: unknown): never { | ||
if (err instanceof GraphErrors.GraphResponseError) { | ||
console.log('Error message:', err.message); | ||
console.log('Query:', err.request.query); | ||
console.log('Variables:', err.request.variables); | ||
} | ||
if (err instanceof GraphErrors.GraphContentResponseError) { | ||
console.log('Detailed errors: ', err.errors); | ||
} | ||
|
||
throw err; | ||
} | ||
|
||
export default async function Page({ params, searchParams }: Props) { | ||
const { slug } = await params; | ||
const { variation } = await searchParams; | ||
|
||
const client = new GraphClient(process.env.OPTIMIZELY_GRAPH_SINGLE_KEY!, { | ||
graphUrl: process.env.OPTIMIZELY_GRAPH_URL, | ||
}); | ||
const c = await client.fetchContent(`/${slug.join('/')}/`); | ||
|
||
return <OptimizelyComponent opti={c} />; | ||
const content = await client | ||
.fetchContent({ path: `/${slug.join('/')}/`, variation }) | ||
.catch(handleGraphErrors); | ||
|
||
return <OptimizelyComponent opti={content} />; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/** | ||
* This module contains the TypeScript definitions of a Graph Query | ||
* and functions to build those filters based on path, | ||
* preview parameters, etc. | ||
* | ||
* This is used internally in the SDK | ||
*/ | ||
|
||
/** | ||
* Creates a {@linkcode ContentInput} object that filters results by a specific URL path. | ||
* | ||
* @param path - The URL path to filter by. | ||
* @returns A `GraphQueryArguments` object with a `where` clause that matches the given path. | ||
*/ | ||
export function pathFilter(path: string): ContentInput { | ||
return { | ||
where: { | ||
_metadata: { | ||
url: { | ||
default: { | ||
eq: path, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a {@linkcode ContentInput} object for previewing content based on key, version, and locale. | ||
* | ||
* @param params - An object containing the following properties: | ||
* @param params.key - The unique key identifying the content. | ||
* @param params.ver - The version of the content to preview. | ||
* @param params.loc - The locale of the content to preview. | ||
* | ||
* @returns A `GraphQueryArguments` object with a `where` clause filtering by key, version, and locale. | ||
*/ | ||
export function previewFilter(params: { | ||
key: string; | ||
ver: string; | ||
loc: string; | ||
}): ContentInput { | ||
return { | ||
where: { | ||
_metadata: { | ||
key: { eq: params.key }, | ||
version: { eq: params.ver }, | ||
locale: { eq: params.loc }, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export function variationFilter(value: string): ContentInput { | ||
return { | ||
variation: { | ||
include: 'SOME', | ||
value: [value], | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Arguments for querying content via the Graph API. | ||
*/ | ||
export type ContentInput = { | ||
variation?: VariationInput; | ||
where?: ContentWhereInput; | ||
}; | ||
|
||
type VariationInput = { | ||
include?: VariationIncludeMode; | ||
value?: string[]; | ||
includeOriginal?: boolean; | ||
}; | ||
|
||
type VariationIncludeMode = 'ALL' | 'SOME' | 'NONE'; | ||
|
||
type ContentWhereInput = { | ||
_and?: ContentWhereInput[]; | ||
_or?: ContentWhereInput[]; | ||
_fulltext?: StringFilterInput; | ||
_modified?: DateFilterInput; | ||
_metadata?: IContentMetadataWhereInput; | ||
}; | ||
|
||
type StringFilterInput = ScalarFilterInput<string> & { | ||
like?: string; | ||
startsWith?: string; | ||
endsWith?: string; | ||
in?: string[]; | ||
notIn?: string[]; | ||
match?: string; | ||
contains?: string; | ||
synonyms?: ('ONE' | 'TWO')[]; | ||
fuzzly?: boolean; | ||
exacs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
type DateFilterInput = ScalarFilterInput<string> & { | ||
gt?: string; | ||
gte?: string; | ||
lt?: string; | ||
lte?: string; | ||
decay?: { | ||
origin?: string; | ||
scale?: number; | ||
rate?: number; | ||
}; | ||
}; | ||
|
||
type IContentMetadataWhereInput = { | ||
key?: StringFilterInput; | ||
locale?: StringFilterInput; | ||
fallbackForLocale?: StringFilterInput; | ||
version?: StringFilterInput; | ||
displayName?: StringFilterInput; | ||
url?: ContentUrlInput<StringFilterInput>; | ||
types?: StringFilterInput; | ||
published?: DateFilterInput; | ||
status?: StringFilterInput; | ||
changeset?: StringFilterInput; | ||
created?: DateFilterInput; | ||
lastModified?: DateFilterInput; | ||
sortOrder?: IntFilterInput; | ||
variation?: StringFilterInput; | ||
}; | ||
|
||
type IntFilterInput = ScalarFilterInput<number> & { | ||
gt?: number; | ||
gte?: number; | ||
lt?: number; | ||
lte?: number; | ||
in?: number[]; | ||
notIn?: number[]; | ||
factor?: { | ||
value?: number; | ||
modifier?: 'NONE' | 'SQUARE' | 'SQRT' | 'LOG' | 'RECIPROCAL'; | ||
}; | ||
}; | ||
|
||
type ContentUrlInput<T> = { | ||
type?: T; | ||
default?: T; | ||
hierarchical?: T; | ||
internal?: T; | ||
graph?: T; | ||
base?: T; | ||
}; | ||
|
||
type ScalarFilterInput<T> = { | ||
eq?: T; | ||
notEq?: T; | ||
exist?: boolean; | ||
boost?: number; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.