Created
November 25, 2022 15:22
-
-
Save coderdiaz/d444fd8bb18b557858344e1fc63049b6 to your computer and use it in GitHub Desktop.
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
import { Strapi } from '@strapi/strapi'; | |
export default (strapi: Strapi) => ({ nexus }) => ({ | |
types: [ | |
nexus.extendType({ | |
type: 'Query', | |
definition(t) { | |
t.field('courseById', { | |
type: 'CourseEntity', | |
args: { | |
id: nexus.nonNull('ID') | |
}, | |
resolve: async(_, args) => { | |
const course = await strapi.entityService.findOne( | |
'api::course.course', | |
args.id, { | |
populate: { | |
city: true, | |
}, | |
publicationState: 'live', // Only fetch published content | |
} | |
); | |
return course; | |
}, | |
}) | |
}, | |
}), | |
nexus.extendType({ | |
type: 'Query', | |
definition(t) { | |
t.list.field('coursesByCity', { | |
type: 'CourseEntity', | |
args: { | |
identifier: nexus.nonNull('String'), | |
locale: nexus.stringArg('I18NLocaleCode'), | |
}, | |
resolve: async (ctx, args) => { | |
const locale = args.locale ?? 'en'; | |
const city = await strapi.db | |
.query('api::city.city') | |
.findOne({ | |
where: { | |
identifier: args.identifier | |
}, | |
}); | |
// Return null if city doesn't exists | |
if (!city) { | |
return null; | |
} | |
const courses = await strapi.entityService.findMany('api::course.course', { | |
filters: { | |
city: { | |
id: city.id, | |
}, | |
}, | |
locale, | |
publicationState: 'live', // Only fetch published content | |
populate: { city: true }, | |
}); | |
return courses; | |
}, | |
}); | |
}, | |
}), | |
], | |
plugins: [], | |
resolversConfig: { | |
'Query.courseById': { | |
auth: false, | |
}, | |
'Query.coursesByCity': { | |
auth: false, | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment