### Start Demo Client (Bash) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/demo-client/README.md Navigates to the demo client directory, installs its dependencies, copies the environment file, and starts the client application. ```bash cd ./demo-client npm i cp .env.example .env pm start ``` -------------------------------- ### Start Test Server (Bash) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/demo-client/README.md Installs project dependencies and starts the test GraphQL server. The server playground will be available at http://localhost:1234/graphql. ```bash npm i npm run test-server ``` -------------------------------- ### Installing Dependencies for ra-data-graphql-simple-mongest-resolver (Bash) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/README.md Commands to install the necessary peer dependencies (like mongodb, mongoose, NestJS modules, Apollo) and the ra-data-graphql-simple-mongest-resolver library itself using npm. ```bash npm install --save mongodb mongoose @nestjs/mongoose @apollo/gateway @nestjs/graphql apollo-server-core apollo-server-express graphql mongest-service npm install --save ra-data-graphql-simple-mongest-resolver ``` -------------------------------- ### Example GraphQL Query for Cats (GraphQL) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/README.md An example GraphQL query demonstrating how to fetch data for 'allCats', including basic fields and fields specific to potential discriminator types ('StrayCat', 'HomeCat') using inline fragments. ```gql query { allCats { id name ...on StrayCat { territorySize } ...on HomeCat { humanSlave } } } ``` -------------------------------- ### Generated Mongo Projection Example (JavaScript) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/README.md Shows the MongoDB projection object automatically generated by the resolver based on the fields requested in the corresponding GraphQL query, ensuring only necessary fields are fetched from the database. ```js { _id: true, name: true, territorySize: true, humanSlave: true, age: true } ``` -------------------------------- ### Defining Basic Entity, Service, and Resolver (TypeScript) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/README.md Demonstrates the minimal setup for creating a Mongoose schema (@Schema, @Prop), registering it with Mongest (registerEntityClassForSchema), building a basic Mongest service (BuildMongestService), and creating a default ra-data-graphql-simple-compatible resolver (BuildMongestRaResolver) in NestJS. ```ts @Schema() export class Cat { _id!: ObjectId; @Field(() => String) @Prop({ required: true, type: String }) name!: string; } export const CatSchema = SchemaFactory.createForClass(Cat); registerEntityClassForSchema(Cat, CatSchema); @Injectable() export class CatsService extends BuildMongestService(Cat) { // Expandable service! } @Resolver(() => Cat) export class CatsResolver extends BuildMongestRaResolver(Cat) { constructor(service: CatsService) { super(service); } // Expandable resolver! } ``` -------------------------------- ### Customizing Resolver Options and Adding Fields (TypeScript) Source: https://github.com/oodelally/ra-data-graphql-simple-mongest-resolver/blob/master/README.md Illustrates how to customize the generated resolver using the options object (MongestRaResolverOptions). This includes adding custom filters, defining virtual fields, specifying required fields for discriminators, configuring endpoint behavior (enabling/disabling, adding guards/interceptors, omitting arguments), and adding custom GraphQL fields using @ResolveField. ```ts // Add any custom filter. It will be added to the `allCats` endpoint arguments. @InputType() class CatFilter { @Field(() => String, { nullable: true }) nameRegexp?: string; } // Tell how the CatFilter should be translated into a mongo filter const graphqlFilterToMongoFilter = async ({ nameRegexp }: CatFilter): Promise> => { // The filter builder can be synchronous or asynchronous const filter: FilterQuery = {}; if (nameRegexp) { filter.name = new RegExp(escapeRegExp(nameRegexp), 'i'); } return filter; }; const CatsResolverOptions: MongestRaResolverOptions = { filter: { classRef: CatFilter, filterBuilder: graphqlFilterToMongoFilter, }, virtualFields: { fancyName: { dependsOn: ['name'] }, // Include `name` if the virtual field `fancyName` needs it to resolve (see the resolver below). }, discriminatorRequiredExtraFields: ['age'], // e.g. if `age` is required in your graphql resolveType(). endpoints: { update: { enable: true, // By default, for safety, only the readonly endpoints are enabled. }, delete: { enable: true, guard: myPermissionGuard, // Add a guard to the `delete` endpoint. interceptor: myLoggerInterceptor, // Add an interceptor to the `delete` endpoint. }, create: { args: { omitFields: ['color'], // Dont include `color` as `create`'s argument. }, }, }, }; @Resolver(() => Cat) export class CatsResolver extends BuildMongestRaResolver(Cat, CatsResolverOptions) { constructor(service: CatsService) { super(service); } @ResolveField(() => String) async fancyName(@Parent() parent: { name: string }) { return `Fancy ${parent.name}`; } // Add new custom endpoints here } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.