import { BaseModel, column } from "@adonisjs/lucid/orm";
import { ObjectType, Field } from "@foadonis/graphql/decorators";
import { DateTime } from "luxon";
@ObjectType()
export default class Recipe extends BaseModel {
@column({ isPrimary: true })
@Field((type) => ID)
declare id: string;
@column()
@Field()
declare title: string;
@column()
@Field({ nullable: true })
declare description: string | null;
@column()
@Field(() => [String])
declare ingredients: string[];
}
import Recipe from "#models/recipe";
import { Arg, Args, Int, Mutation, Query, Resolver } from "type-graphql";
@Resolver(Recipe)
export default class RecipeResolver {
@Query(() => Recipe)
recipe(@Arg("id") id: number) {
return Recipe.findOrFail(id);
}
@Query(() => [Recipe])
recipes(@Args() { page, perPage }: RecipeArgs) {
return Recipe.query().paginate(page, perPage);
}
@Mutation(() => Recipe)
addRecipe(@Arg("newRecipeData") newRecipeData: NewRecipeInput) {
return Recipe.create(newRecipeData);
}
@Mutation(() => Boolean)
async removeRecipe(@Arg("id") id: number) {
const recipe = await Recipe.find(id);
if (!recipe) {
return false;
}
await recipe.delete();
return true;
}
}
Heard some of you are into #GraphQL?
Thanks to @martin-paucot.fr, you can now compose your Object Type from our Lucid Model and perform CRUD operations on it!
🔗 friendsofadonis.com/docs/graphql