Skip to content

Instantly share code, notes, and snippets.

@coderdiaz
Created November 25, 2022 15:22
Show Gist options
  • Save coderdiaz/d444fd8bb18b557858344e1fc63049b6 to your computer and use it in GitHub Desktop.
Save coderdiaz/d444fd8bb18b557858344e1fc63049b6 to your computer and use it in GitHub Desktop.
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