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

@sequelize-graphql/core

v1.0.17

Published

Lightweight library to sync sequelize and graphql

Downloads

30

Readme

sequelize-graphql

NPM Version GitHub Actions Workflow Status GitHub Release Date - Published_At

Opinionated zero dependency library to sync Sequelize and GraphQL.

[!WARNING] The library is in WIP until v2 is released. Issues and PRs are welcomed.

Key features

  • [x] Define your model in one place and have the Sequelize model as well as the GraphQL type auto-generated.
  • [x] GraphQL oriented (e.g A model must have a primary key named id wich is by default a UUID mapped to a GraphQLID).
  • [x] Association handling (e.g An hasMany association will be exposed as a pagination).
  • [x] Pagination utility which is orderable, filterable, and type safe.
  • [x] N+1 query handling. Caching and batching is automatically handled.
  • [x] Sequelize utilities.
  • [x] GraphQL utilities.

Getting Started

Install sequelize-graphql from npm

With npm:

npm install --save @sequelize-graphql/core sequelize graphql dataloader

or using yarn:

yarn add @sequelize-graphql/core sequelize graphql dataloader

See Peer dependencies breakdown for more information.

Setup the context and you are good to go:

import { makeContext } from '@sequelize-graphql/core';

// Apollo Server example
const { url } = await startStandaloneServer(server, {
  context: async ({ req, res }) => ({
    ...makeContext(), // A simple object containing everyting sequelize-graphql needs to work properly.
    authToken: req.headers.authorization, // Other stuff you want to place in the context like an auth token.
  }),
});

Example usage

A simple Library API.

Author.model.ts

import { CreationOptional } from 'sequelize';
import { Model, STRING, type InferSequelizeModel } from '@sequelize-graphql/core';

export interface AuthorModel extends InferSequelizeModel<AuthorModel> {
  id: CreationOptional<string>;
  firstname: string;
  lastname: string;
}

const Author: Model<AuthorModel> = new Model({
  name: 'Author',
  columns: {
    firstname: { type: STRING, allowNull: false, exposed: true, description: '...' },
    lastname: { type: STRING, allowNull: false, exposed: true },
  },
  fields: {
    fullname: {
      type: new GraphQLNonNull(GraphQLString),
      resolve(author, args, ctx) {
        const { firstname, lastname } = author;
        return `${firstname} ${lastname}`;
      },
    },
  },
  associations: () => ({
    books: {
      model: Book,
      type: 'hasMany',
      exposed: true,
    },
  }),
});

Book.model.ts

import { CreationOptional } from 'sequelize';
import { Model, ENUM, ID, STRING, type InferSequelizeModel } from '@sequelize-graphql/core';
import { Author } from './models';

export enum Genre {
  Thriller = 'Thriller',
  Horror = 'Horror',
}

export const GenreEnum = ENUM({
  name: 'Genre',
  values: Genre,
});

export interface BookModel extends InferSequelizeModel<BookModel> {
  id: CreationOptional<string>;
  authorId: ForeignKey<string>;
  title: string;
  genre: Genre;
}

const Book: Model<BookModel> = new Model({
  name: 'Book',
  columns: {
    authorId: { type: ID, allowNull: false, exposed: true },
    title: { type: STRING, allowNull: false, exposed: true },
    genre: { type: GenreEnum, allowNull: false, exposed: true },
  },
  associations: () => ({
    author: {
      model: Author,
      type: 'belongsTo',
      exposed: true,
    },
  }),
});

query.ts

import { exposeModel } from '@sequelize-graphql/core';
import { Author } from './models';

export default new GraphQLObjectType({
  name: 'Query',
  fields: {
    ...exposeModel(Author, {
      findById: 'author',
      findByIds: 'authorsByIds',
      pagination: 'authors',
    }),
  },
});

book.mutation.ts

import { Book, Author, GenreEnum } from './models';

export default new GraphQLObjectType({
  name: 'BookMutation',
  fields: {
    create: {
      type: new GraphQLNonNull(Book.type),
      args: {
        input: {
          type: new GraphQLNonNull(new GraphQLInputObjectType({
            name: 'CreateBookInput',
            fields: {
              authorId: { type: new GraphQLNonNull(GraphQLID) },
              title: { type: new GraphQLNonNull(GraphQLString) },
              genre: { type: new GraphQLNonNull(GenreEnum.gqlType) },
            },
          })),
        },
      },
      async resolve(_, args) {
        const { input: { authorId, title, genre } } = args;
        await Author.ensureExistence(authorId);
        return Book.model.create({ authorId, title, genre });
      },
    },