Skip to content

Commit

Permalink
added image-search methods and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MaPoKen committed Nov 19, 2024
1 parent 968cd2f commit 4adfbda
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 20 deletions.
16 changes: 10 additions & 6 deletions src/api/imageApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { IImageMetaInformationV2, IImageMetaInformationV3, Sort } from "@ndla/types-backend/image-api";
import { IImageMetaInformationV2, IImageMetaInformationV3, ISearchResultV3, Sort } from "@ndla/types-backend/image-api";
import { GQLQuerySearchImagesArgs } from "../types/schema";
import { fetch, resolveJson } from "../utils/apiHelpers";

Expand All @@ -26,11 +26,15 @@ export async function fetchImageV3(imageId: string, context: Context): Promise<I
return await resolveJson(response);
}

export async function searchImages(params: GQLQuerySearchImagesArgs, context: Context) {
const response = await fetch("/image-api/v3/images", context, {
method: "POST",
body: JSON.stringify(params),
});
export async function searchImages(params: GQLQuerySearchImagesArgs, context: Context): Promise<ISearchResultV3> {
const args = {
...(params.page && { pageSize: params.page.toString() }),
...(params.pageSize && { pageSize: params.pageSize.toString() }),
...(params.query && { query: params.query })
}
const a = new URLSearchParams(args).toString()
console.log(a)
const response = await fetch("/image-api/v3/images", context);
return await resolveJson(response);
}

Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/imageResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
*/
import { IImageMetaInformationV2 } from "@ndla/types-backend/image-api";
import { IImageMetaInformationV2, ISearchResultV3 } from "@ndla/types-backend/image-api";
import { fetchImage, searchImages } from "../api/imageApi";
import { GQLQueryImageArgs, GQLQuerySearchImagesArgs } from "../types/schema";

Expand All @@ -17,7 +17,7 @@ export const Query = {
_: any,
args: GQLQuerySearchImagesArgs,
context: ContextWithLoaders,
): Promise<IImageMetaInformationV2> {
): Promise<ISearchResultV3> {
return searchImages(args, context);
},
};
Expand Down
35 changes: 34 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,39 @@ export const typeDefs = gql`
sharedFolders: [SharedFolder!]!
}
type ImageV3 {
filename: String!
size: Int!
contentType: String!
imageUrl: String!
dimensions: ImageDimensions!
language: String!
}
type ImageMetaInformationV3 {
id: String!
metaUrl: String!
title: Title!
alttext: ImageAltText!
copyright: Copyright!
tags: Tags!
caption: Caption!
supportedLanguages: [String!]
created: String!
createdBy: String!
modelRelease: String!
editorNotes: [EditorNote!]
image: ImageV3!
}
type ImageSearch {
totalCount: Int!
page: Int!
pageSize: Int!
language: String!
results: [ImageMetaInformationV3!]
}
type Query {
node(id: String, rootId: String, parentId: String, contextId: String): Node
nodes(
Expand Down Expand Up @@ -1608,7 +1641,7 @@ export const typeDefs = gql`
listArenaUserV2(page: Int, pageSize: Int, query: String, filterTeachers: Boolean): PaginatedArenaUsers!
subjectCollection(language: String!): [Subject!]
myLearningpaths: [Learningpath!]!
searchImages(query: String, page: Int, pageSize: Int): [ImageMetaInformation]!
searchImages(query: String, page: Int, pageSize: Int): ImageSearch
}
type Mutation {
Expand Down
85 changes: 83 additions & 2 deletions src/types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,42 @@ export type GQLImageMetaInformationV2 = {
title: GQLTitle;
};

export type GQLImageMetaInformationV3 = {
__typename?: 'ImageMetaInformationV3';
alttext: GQLImageAltText;
caption: GQLCaption;
copyright: GQLCopyright;
created: Scalars['String'];
createdBy: Scalars['String'];
editorNotes?: Maybe<Array<GQLEditorNote>>;
id: Scalars['String'];
image: GQLImageV3;
metaUrl: Scalars['String'];
modelRelease: Scalars['String'];
supportedLanguages?: Maybe<Array<Scalars['String']>>;
tags: GQLTags;
title: GQLTitle;
};

export type GQLImageSearch = {
__typename?: 'ImageSearch';
language: Scalars['String'];
page: Scalars['Int'];
pageSize: Scalars['Int'];
results?: Maybe<Array<GQLImageMetaInformationV3>>;
totalCount: Scalars['Int'];
};

export type GQLImageV3 = {
__typename?: 'ImageV3';
contentType: Scalars['String'];
dimensions: GQLImageDimensions;
filename: Scalars['String'];
imageUrl: Scalars['String'];
language: Scalars['String'];
size: Scalars['Int'];
};

export type GQLLearningpath = {
__typename?: 'Learningpath';
canEdit: Scalars['Boolean'];
Expand Down Expand Up @@ -1605,7 +1641,7 @@ export type GQLQuery = {
resourceEmbeds: GQLResourceEmbed;
resourceTypes?: Maybe<Array<GQLResourceTypeDefinition>>;
search?: Maybe<GQLSearch>;
searchImages: Array<Maybe<GQLImageMetaInformation>>;
searchImages?: Maybe<GQLImageSearch>;
searchWithoutPagination?: Maybe<GQLSearchWithoutPagination>;
sharedFolder: GQLSharedFolder;
subject?: Maybe<GQLSubject>;
Expand Down Expand Up @@ -2586,6 +2622,9 @@ export type GQLResolversTypes = {
ImageLicense: ResolverTypeWrapper<GQLImageLicense>;
ImageMetaInformation: ResolverTypeWrapper<GQLImageMetaInformation>;
ImageMetaInformationV2: ResolverTypeWrapper<GQLImageMetaInformationV2>;
ImageMetaInformationV3: ResolverTypeWrapper<GQLImageMetaInformationV3>;
ImageSearch: ResolverTypeWrapper<GQLImageSearch>;
ImageV3: ResolverTypeWrapper<GQLImageV3>;
Int: ResolverTypeWrapper<Scalars['Int']>;
Learningpath: ResolverTypeWrapper<GQLLearningpath>;
LearningpathCopyright: ResolverTypeWrapper<GQLLearningpathCopyright>;
Expand Down Expand Up @@ -2755,6 +2794,9 @@ export type GQLResolversParentTypes = {
ImageLicense: GQLImageLicense;
ImageMetaInformation: GQLImageMetaInformation;
ImageMetaInformationV2: GQLImageMetaInformationV2;
ImageMetaInformationV3: GQLImageMetaInformationV3;
ImageSearch: GQLImageSearch;
ImageV3: GQLImageV3;
Int: Scalars['Int'];
Learningpath: GQLLearningpath;
LearningpathCopyright: GQLLearningpathCopyright;
Expand Down Expand Up @@ -3642,6 +3684,42 @@ export type GQLImageMetaInformationV2Resolvers<ContextType = any, ParentType ext
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GQLImageMetaInformationV3Resolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ImageMetaInformationV3'] = GQLResolversParentTypes['ImageMetaInformationV3']> = {
alttext?: Resolver<GQLResolversTypes['ImageAltText'], ParentType, ContextType>;
caption?: Resolver<GQLResolversTypes['Caption'], ParentType, ContextType>;
copyright?: Resolver<GQLResolversTypes['Copyright'], ParentType, ContextType>;
created?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
createdBy?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
editorNotes?: Resolver<Maybe<Array<GQLResolversTypes['EditorNote']>>, ParentType, ContextType>;
id?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
image?: Resolver<GQLResolversTypes['ImageV3'], ParentType, ContextType>;
metaUrl?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
modelRelease?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
supportedLanguages?: Resolver<Maybe<Array<GQLResolversTypes['String']>>, ParentType, ContextType>;
tags?: Resolver<GQLResolversTypes['Tags'], ParentType, ContextType>;
title?: Resolver<GQLResolversTypes['Title'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GQLImageSearchResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ImageSearch'] = GQLResolversParentTypes['ImageSearch']> = {
language?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
page?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
pageSize?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
results?: Resolver<Maybe<Array<GQLResolversTypes['ImageMetaInformationV3']>>, ParentType, ContextType>;
totalCount?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GQLImageV3Resolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ImageV3'] = GQLResolversParentTypes['ImageV3']> = {
contentType?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
dimensions?: Resolver<GQLResolversTypes['ImageDimensions'], ParentType, ContextType>;
filename?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
imageUrl?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
language?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
size?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GQLLearningpathResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['Learningpath'] = GQLResolversParentTypes['Learningpath']> = {
canEdit?: Resolver<GQLResolversTypes['Boolean'], ParentType, ContextType>;
copyright?: Resolver<GQLResolversTypes['LearningpathCopyright'], ParentType, ContextType>;
Expand Down Expand Up @@ -4113,7 +4191,7 @@ export type GQLQueryResolvers<ContextType = any, ParentType extends GQLResolvers
resourceEmbeds?: Resolver<GQLResolversTypes['ResourceEmbed'], ParentType, ContextType, RequireFields<GQLQueryResourceEmbedsArgs, 'resources'>>;
resourceTypes?: Resolver<Maybe<Array<GQLResolversTypes['ResourceTypeDefinition']>>, ParentType, ContextType>;
search?: Resolver<Maybe<GQLResolversTypes['Search']>, ParentType, ContextType, Partial<GQLQuerySearchArgs>>;
searchImages?: Resolver<Array<Maybe<GQLResolversTypes['ImageMetaInformation']>>, ParentType, ContextType, Partial<GQLQuerySearchImagesArgs>>;
searchImages?: Resolver<Maybe<GQLResolversTypes['ImageSearch']>, ParentType, ContextType, Partial<GQLQuerySearchImagesArgs>>;
searchWithoutPagination?: Resolver<Maybe<GQLResolversTypes['SearchWithoutPagination']>, ParentType, ContextType, Partial<GQLQuerySearchWithoutPaginationArgs>>;
sharedFolder?: Resolver<GQLResolversTypes['SharedFolder'], ParentType, ContextType, RequireFields<GQLQuerySharedFolderArgs, 'id'>>;
subject?: Resolver<Maybe<GQLResolversTypes['Subject']>, ParentType, ContextType, RequireFields<GQLQuerySubjectArgs, 'id'>>;
Expand Down Expand Up @@ -4635,6 +4713,9 @@ export type GQLResolvers<ContextType = any> = {
ImageLicense?: GQLImageLicenseResolvers<ContextType>;
ImageMetaInformation?: GQLImageMetaInformationResolvers<ContextType>;
ImageMetaInformationV2?: GQLImageMetaInformationV2Resolvers<ContextType>;
ImageMetaInformationV3?: GQLImageMetaInformationV3Resolvers<ContextType>;
ImageSearch?: GQLImageSearchResolvers<ContextType>;
ImageV3?: GQLImageV3Resolvers<ContextType>;
Learningpath?: GQLLearningpathResolvers<ContextType>;
LearningpathCopyright?: GQLLearningpathCopyrightResolvers<ContextType>;
LearningpathCoverphoto?: GQLLearningpathCoverphotoResolvers<ContextType>;
Expand Down
18 changes: 9 additions & 9 deletions src/utils/apiHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function fetchHelper(path: string, context: Context, options?: RequestOpti
});

const accessTokenAuth = context.token ? { Authorization: `Bearer ${context.token.access_token}` } : null;
const feideAuthorization = context.feideAuthorization ? { feideAuthorization: context.feideAuthorization } : null;
const feideAuthorization = context.feideAuthorization ? { eideAuthorization: context.feideAuthorization } : null;
const versionHash = context.versionHash ? { versionhash: context.versionHash } : null;
const cacheHeaders = !context.shouldUseCache ? { "Cache-Control": "no-cache" } : null;

Expand Down Expand Up @@ -108,11 +108,11 @@ function externalsToH5pMetaData(obj: any) {
processors: [] as any[],
rightsholders: i.h5p.authors
? i.h5p.authors.map((author: { role: any; name?: string }) => {
return {
type: roleMapper(author.role || ""),
name: author.name || "",
};
})
return {
type: roleMapper(author.role || ""),
name: author.name || "",
};
})
: [],
origin: i.h5p.source || "",
};
Expand Down Expand Up @@ -182,9 +182,9 @@ export function learningpathToMeta(learningpath: ILearningPathSummaryV2): GQLMet
lastUpdated: learningpath.lastUpdated,
metaImage: learningpath.coverPhotoUrl
? {
url: learningpath.coverPhotoUrl,
alt: learningpath.introduction.introduction,
}
url: learningpath.coverPhotoUrl,
alt: learningpath.introduction.introduction,
}
: undefined,
};
}
Expand Down

0 comments on commit 4adfbda

Please sign in to comment.