npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ra-data-graphql-simple-mongest-resolver

v0.1.2

Published

Mongest resolver for React Admin ra-data-graphql-simple

Downloads

3

Readme

Mongest NestJS Resolver for ra-data-graphql-simple (BETA)

Automatic ReactAdmin GraphQL NestJS resolver

This is a BETA, and therefore you may encounter bugs. Please post an issue if needed.

TL;DR

  • Out-of-the-box GraphQL NestJS resolver CRUD endpoints for ra-data-graphql-simple.
  • Optimized: Uses mongo projections to only fetch the fields asked by the GraphQL query, no less, no more.
  • Fully customizable, overridable and expandable NestJS resolver.

Demo

A cat-based demo of both backend and react-admin client is available.

Clone this repo on your machine and follow the demo instructions.

Setup

Install (if not already there) the peer dependencies:

npm install --save mongodb mongoose @nestjs/mongoose @apollo/gateway @nestjs/graphql apollo-server-core apollo-server-express graphql mongest-service

Then install the ra-data-graphql-simple-mongest-resolver lib:

npm install --save ra-data-graphql-simple-mongest-resolver

Now you can create your entity, your service, and your resolver:

@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!
}

Features

Out-of-the-box GraphQL NestJS resolver CRUD endpoints.

Just declare your entity, the service and the resolver will have automatic endpoints with the appropriate fields. The generated endpoints are described in ra-data-graphql-simple.

Only fetches the required data

query {
  allCats {
    id
    name
    ...on StrayCat {
      territorySize
    }
    ...on HomeCat {
      humanSlave
    }
  }
}

generates the following mongo projection

{
  _id: true,
  name: true,
  territorySize: true,
  humanSlave: true,
  age: true
}

so that only the required data is transfered from the DB to the final client.

Fully customizable, overridable and expandable NestJS resolver.


// 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<FilterQuery<Cat>> => {
  // The filter builder can be synchronous or asynchronous
  const filter: FilterQuery<Cat> = {};
  if (nameRegexp) {
    filter.name = new RegExp(escapeRegExp(nameRegexp), 'i');
  }
  return filter;
};

const CatsResolverOptions: MongestRaResolverOptions<Cat, CatFilter> = {
  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
}

Delightfully-typed Mongoose-wrapper NestJS-service

These resolvers are built on top of Mongest Service, which means you can also expand the NestJS service in order to reuse it anywhere in your app.

  • NestJS Service that delicately wraps Mongoose methods for your favorite entities.
  • All returned documents are leans, but casted as instance of their entity class.
  • Amazing discriminator-based polymorphism!
  • Precise and safe typing in and out for all mongoose functions (sensitive to projection!).
  • Fully overridable and expandable NestJS service.