diff --git a/package.json b/package.json index c88659f..31e320c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@team-fuho/wt", - "version": "2.2.4", + "version": "2.2.5", "description": "powered by `wagtail`. awesome!", "type": "module", "main": "dist/index.js", diff --git a/src/index.spec.ts b/src/index.spec.ts index 73eb00f..8b1cc2e 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -38,4 +38,6 @@ console.log( data.blogList?.items?.flatMap((t) => t?.involved?.map((i) => i?.name)) ) +console.log(data.blogList?.items?.map((k) => k?.title)) + console.log('tree view:', data, null, 5) diff --git a/src/index.ts b/src/index.ts index d5d6576..167dcb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,24 @@ import { // import persisted from './graphql/persisted-documents.json' +// __typename is not a real field +type NodeType = { __typename: T } + +/** + * Assume on GraphQL Codegen abstraction. + * For query that has a lot of overload, this helps. + * The reason this is not wrapped in queries already, is because the root one is okay, but not the underlying. + * @param t `__typename` assumption + * @param v hinted object (must be hinted) + */ +export function assume< + Hinted, + Assumed extends Hinted & { __typename: string }, + Infered extends Assumed['__typename'] +>(t: Infered, v: Hinted) { + return v as Hinted & NodeType +} + export interface GraphQLClientConfig { endpoint: string headers?: Record @@ -75,11 +93,13 @@ export default class WTClient { * List all blog page */ async listBlogs(page = 0, perPage = 1024, order = '-id') { - return await execute(this.config, BlogPaginatedListViewDocument, { - page: page, - perPage: perPage, - order: order - }) + return ( + await execute(this.config, BlogPaginatedListViewDocument, { + page: page, + perPage: perPage, + order: order + }) + ).blogs } /** @@ -87,19 +107,7 @@ export default class WTClient { * @param token urlparsed token query (added by wagtail addon) */ async getBlogPreview(token: string) { - return ( - await execute( - { - ...this.config, - headers: { - ...this.config.headers, - Authorization: `Bearer ${token}` - } - }, - BlogPreviewViewDocument, - { token } - ) - ).blog + return (await execute(this.config, BlogPreviewViewDocument, { token })).blog } /**