Skip to content

Commit

Permalink
Merge pull request #375 from NDLANO/config-lists
Browse files Browse the repository at this point in the history
Handle multiple configuration types from learningpath-api backend
  • Loading branch information
jnatten authored Nov 9, 2023
2 parents 810fd73 + 9cb067c commit 35b8471
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@graphql-codegen/typescript-operations": "^2.5.6",
"@graphql-codegen/typescript-resolvers": "^2.7.6",
"@graphql-eslint/eslint-plugin": "^3.12.0",
"@ndla/types-backend": "^0.2.23",
"@ndla/types-backend": "^0.2.26",
"@ndla/types-embed": "^4.0.0",
"@ndla/types-taxonomy": "^1.0.19",
"@types/bunyan": "^1.8.5",
Expand Down
14 changes: 10 additions & 4 deletions src/api/learningpathApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,19 @@ export async function fetchLearningpath(
};
}

export const fetchExamLockStatus = async (
export const fetchConfig = async (
configKey: string,
context: Context,
): Promise<IConfigMetaRestricted> => {
const response = await fetch(
'/learningpath-api/v1/config/MY_NDLA_WRITE_RESTRICTED',
`/learningpath-api/v1/config/${configKey}`,
context,
);
const examLockStatus: IConfigMetaRestricted = await resolveJson(response);
return examLockStatus;
const config: IConfigMetaRestricted = await resolveJson(response);
return config;
};

export const fetchExamLockStatus = async (
context: Context,
): Promise<IConfigMetaRestricted> =>
fetchConfig('MY_NDLA_WRITE_RESTRICTED', context);
24 changes: 20 additions & 4 deletions src/resolvers/learningpathResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*
*/

import { IConfigMetaRestricted } from '@ndla/types-backend/learningpath-api';
import { fetchLearningpath, fetchNode, fetchOembed } from '../api';
import { fetchExamLockStatus } from '../api/learningpathApi';
import { fetchConfig, fetchExamLockStatus } from '../api/learningpathApi';
import {
GQLConfigMetaBoolean,
GQLConfigMetaStringList,
GQLLearningpath,
GQLLearningpathStep,
GQLLearningpathStepOembed,
Expand All @@ -31,8 +32,23 @@ export const Query = {
_: any,
__: any,
context: ContextWithLoaders,
): Promise<IConfigMetaRestricted> {
return fetchExamLockStatus(context);
): Promise<GQLConfigMetaBoolean> {
const config = await fetchExamLockStatus(context);
if (typeof config.value !== 'boolean') {
throw new Error('Invalid exam lock status');
}

return { key: config.key, value: config.value };
},
async arenaEnabledOrgs(
_: any,
__: any,
context: ContextWithLoaders,
): Promise<GQLConfigMetaStringList> {
const config = await fetchConfig('ARENA_ENABLED_ORGS', context);
if (typeof config.value === 'boolean')
throw new Error('Invalid arena enabled orgs');
return { key: config.key, value: config.value };
},
};

Expand Down
14 changes: 11 additions & 3 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,18 @@ export const typeDefs = gql`
id: Int!
favoriteSubjects: [String!]!
role: String!
arenaEnabled: Boolean!
organization: String!
}
type ConfigMetaRestricted {
type ConfigMetaBoolean {
key: String!
value: String!
value: Boolean!
}
type ConfigMetaStringList {
key: String!
value: [String!]!
}
type ResourceMetaData {
Expand Down Expand Up @@ -1323,7 +1330,8 @@ export const typeDefs = gql`
allFolderResources(size: Int): [FolderResource!]!
personalData: MyNdlaPersonalData!
image(id: String!): ImageMetaInformationV2
examLockStatus: ConfigMetaRestricted!
examLockStatus: ConfigMetaBoolean!
arenaEnabledOrgs: ConfigMetaStringList
resourceEmbed(id: String!, type: String!): ResourceEmbed!
resourceEmbeds(resources: [ResourceEmbedInput!]!): ResourceEmbed!
arenaCategories: [ArenaCategory!]!
Expand Down
41 changes: 31 additions & 10 deletions src/types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,16 @@ export type GQLConceptResult = {
totalCount: Scalars['Int'];
};

export type GQLConfigMetaRestricted = {
__typename?: 'ConfigMetaRestricted';
export type GQLConfigMetaBoolean = {
__typename?: 'ConfigMetaBoolean';
key: Scalars['String'];
value: Scalars['String'];
value: Scalars['Boolean'];
};

export type GQLConfigMetaStringList = {
__typename?: 'ConfigMetaStringList';
key: Scalars['String'];
value: Array<Scalars['String']>;
};

export type GQLContributor = {
Expand Down Expand Up @@ -949,8 +955,10 @@ export type GQLMutationUpdatePersonalDataArgs = {

export type GQLMyNdlaPersonalData = {
__typename?: 'MyNdlaPersonalData';
arenaEnabled: Scalars['Boolean'];
favoriteSubjects: Array<Scalars['String']>;
id: Scalars['Int'];
organization: Scalars['String'];
role: Scalars['String'];
};

Expand Down Expand Up @@ -1063,6 +1071,7 @@ export type GQLQuery = {
allFolderResources: Array<GQLFolderResource>;
arenaCategories: Array<GQLArenaCategory>;
arenaCategory?: Maybe<GQLArenaCategory>;
arenaEnabledOrgs?: Maybe<GQLConfigMetaStringList>;
arenaRecentTopics: Array<GQLArenaTopic>;
arenaTopic?: Maybe<GQLArenaTopic>;
arenaTopicsByUser: Array<GQLArenaTopic>;
Expand All @@ -1076,7 +1085,7 @@ export type GQLQuery = {
conceptSearch?: Maybe<GQLConceptResult>;
coreElement?: Maybe<GQLCoreElement>;
coreElements?: Maybe<Array<GQLCoreElement>>;
examLockStatus: GQLConfigMetaRestricted;
examLockStatus: GQLConfigMetaBoolean;
filmfrontpage?: Maybe<GQLFilmFrontpage>;
folder: GQLFolder;
folderResourceMeta?: Maybe<GQLFolderResourceMeta>;
Expand Down Expand Up @@ -1880,7 +1889,8 @@ export type GQLResolversTypes = {
ConceptFolderResourceMeta: ResolverTypeWrapper<GQLConceptFolderResourceMeta>;
ConceptLicense: ResolverTypeWrapper<GQLConceptLicense>;
ConceptResult: ResolverTypeWrapper<GQLConceptResult>;
ConfigMetaRestricted: ResolverTypeWrapper<GQLConfigMetaRestricted>;
ConfigMetaBoolean: ResolverTypeWrapper<GQLConfigMetaBoolean>;
ConfigMetaStringList: ResolverTypeWrapper<GQLConfigMetaStringList>;
Contributor: ResolverTypeWrapper<GQLContributor>;
Copyright: ResolverTypeWrapper<GQLCopyright>;
CoreElement: ResolverTypeWrapper<GQLCoreElement>;
Expand Down Expand Up @@ -2027,7 +2037,8 @@ export type GQLResolversParentTypes = {
ConceptFolderResourceMeta: GQLConceptFolderResourceMeta;
ConceptLicense: GQLConceptLicense;
ConceptResult: GQLConceptResult;
ConfigMetaRestricted: GQLConfigMetaRestricted;
ConfigMetaBoolean: GQLConfigMetaBoolean;
ConfigMetaStringList: GQLConfigMetaStringList;
Contributor: GQLContributor;
Copyright: GQLCopyright;
CoreElement: GQLCoreElement;
Expand Down Expand Up @@ -2489,9 +2500,15 @@ export type GQLConceptResultResolvers<ContextType = any, ParentType extends GQLR
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GQLConfigMetaRestrictedResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ConfigMetaRestricted'] = GQLResolversParentTypes['ConfigMetaRestricted']> = {
export type GQLConfigMetaBooleanResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ConfigMetaBoolean'] = GQLResolversParentTypes['ConfigMetaBoolean']> = {
key?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
value?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
value?: Resolver<GQLResolversTypes['Boolean'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type GQLConfigMetaStringListResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ConfigMetaStringList'] = GQLResolversParentTypes['ConfigMetaStringList']> = {
key?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
value?: Resolver<Array<GQLResolversTypes['String']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand Down Expand Up @@ -2981,8 +2998,10 @@ export type GQLMutationResolvers<ContextType = any, ParentType extends GQLResolv
};

export type GQLMyNdlaPersonalDataResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['MyNdlaPersonalData'] = GQLResolversParentTypes['MyNdlaPersonalData']> = {
arenaEnabled?: Resolver<GQLResolversTypes['Boolean'], ParentType, ContextType>;
favoriteSubjects?: Resolver<Array<GQLResolversTypes['String']>, ParentType, ContextType>;
id?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
organization?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
role?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
Expand Down Expand Up @@ -3096,6 +3115,7 @@ export type GQLQueryResolvers<ContextType = any, ParentType extends GQLResolvers
allFolderResources?: Resolver<Array<GQLResolversTypes['FolderResource']>, ParentType, ContextType, Partial<GQLQueryAllFolderResourcesArgs>>;
arenaCategories?: Resolver<Array<GQLResolversTypes['ArenaCategory']>, ParentType, ContextType>;
arenaCategory?: Resolver<Maybe<GQLResolversTypes['ArenaCategory']>, ParentType, ContextType, RequireFields<GQLQueryArenaCategoryArgs, 'categoryId' | 'page'>>;
arenaEnabledOrgs?: Resolver<Maybe<GQLResolversTypes['ConfigMetaStringList']>, ParentType, ContextType>;
arenaRecentTopics?: Resolver<Array<GQLResolversTypes['ArenaTopic']>, ParentType, ContextType>;
arenaTopic?: Resolver<Maybe<GQLResolversTypes['ArenaTopic']>, ParentType, ContextType, RequireFields<GQLQueryArenaTopicArgs, 'page' | 'topicId'>>;
arenaTopicsByUser?: Resolver<Array<GQLResolversTypes['ArenaTopic']>, ParentType, ContextType, RequireFields<GQLQueryArenaTopicsByUserArgs, 'userSlug'>>;
Expand All @@ -3109,7 +3129,7 @@ export type GQLQueryResolvers<ContextType = any, ParentType extends GQLResolvers
conceptSearch?: Resolver<Maybe<GQLResolversTypes['ConceptResult']>, ParentType, ContextType, Partial<GQLQueryConceptSearchArgs>>;
coreElement?: Resolver<Maybe<GQLResolversTypes['CoreElement']>, ParentType, ContextType, RequireFields<GQLQueryCoreElementArgs, 'code'>>;
coreElements?: Resolver<Maybe<Array<GQLResolversTypes['CoreElement']>>, ParentType, ContextType, Partial<GQLQueryCoreElementsArgs>>;
examLockStatus?: Resolver<GQLResolversTypes['ConfigMetaRestricted'], ParentType, ContextType>;
examLockStatus?: Resolver<GQLResolversTypes['ConfigMetaBoolean'], ParentType, ContextType>;
filmfrontpage?: Resolver<Maybe<GQLResolversTypes['FilmFrontpage']>, ParentType, ContextType>;
folder?: Resolver<GQLResolversTypes['Folder'], ParentType, ContextType, RequireFields<GQLQueryFolderArgs, 'id'>>;
folderResourceMeta?: Resolver<Maybe<GQLResolversTypes['FolderResourceMeta']>, ParentType, ContextType, RequireFields<GQLQueryFolderResourceMetaArgs, 'resource'>>;
Expand Down Expand Up @@ -3544,7 +3564,8 @@ export type GQLResolvers<ContextType = any> = {
ConceptFolderResourceMeta?: GQLConceptFolderResourceMetaResolvers<ContextType>;
ConceptLicense?: GQLConceptLicenseResolvers<ContextType>;
ConceptResult?: GQLConceptResultResolvers<ContextType>;
ConfigMetaRestricted?: GQLConfigMetaRestrictedResolvers<ContextType>;
ConfigMetaBoolean?: GQLConfigMetaBooleanResolvers<ContextType>;
ConfigMetaStringList?: GQLConfigMetaStringListResolvers<ContextType>;
Contributor?: GQLContributorResolvers<ContextType>;
Copyright?: GQLCopyrightResolvers<ContextType>;
CoreElement?: GQLCoreElementResolvers<ContextType>;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1506,10 +1506,10 @@
resolved "https://registry.yarnpkg.com/@ndla/licenses/-/licenses-7.1.3.tgz#3a826083fdf29cafe66b6197087c455d80a7a0ec"
integrity sha512-3GNJxmAlFHnc4Q77sJL+RapEUcxxyBe9jDaFZ9f4miVpu3cIiZUhSXdqWR6E4C9EOKk86tuGftbr9BXIqREa+Q==

"@ndla/types-backend@^0.2.23":
version "0.2.23"
resolved "https://registry.yarnpkg.com/@ndla/types-backend/-/types-backend-0.2.23.tgz#dbd64746100e9ba4fc89332987ba3d89ed9fd503"
integrity sha512-jPU9uj/ItVyNnBCuPIqbSnOhfhVqC1nmEQzbV9cLCF0ijp7KmHpQ7UzfbG3zOg8/3GfuKVZT1Tk3vpVzlS4waQ==
"@ndla/types-backend@^0.2.26":
version "0.2.26"
resolved "https://registry.npmjs.org/@ndla/types-backend/-/types-backend-0.2.26.tgz#cf0e5ff76ce2893d7b54a475df4fa4865bf7a0a7"
integrity sha512-xSl2C20vXo1VvcOH7qWfsgg+T9sSmxInuPTcWgZPChKSX+Mo8nIZlz1Arz2gw9pg9YYDrMnMwKVD8B7i9xOihQ==

"@ndla/types-embed@^4.0.0":
version "4.0.0"
Expand Down

0 comments on commit 35b8471

Please sign in to comment.