How to use the @graphql-modules/di.ProviderScope.Session function in @graphql-modules/di

To help you get started, we’ve selected a few @graphql-modules/di examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github maapteh / graphql-modules-app / packages / server / src / modules / product / providers / product.ts View on Github external
import { Injectable, ProviderScope } from '@graphql-modules/di';
import dataloader from 'dataloader';
import nodeFetch, { Response } from 'node-fetch';
import { Products, Product } from '../../../_graphql';
import { checkStatus } from '../../../helpers';
import { CREDENTIALS, BOL_API } from '../../../constants';
import { productDataLoader } from './product-data-loader';

@Injectable({
    scope: ProviderScope.Session,
})
export class ProductProvider {
    private dataLoaderProduct: any;

    constructor() {
        this.dataLoaderProduct = new dataloader(keys =>
            productDataLoader(keys),
        );
    }

    /**
     * Example 1: not dataloader used while it should
     */
    public async getProducts(id: string): Promise {
        const url = `${BOL_API}/lists/?ids=${id}&limit=12&format=json&${CREDENTIALS}`;
        console.log(`[BAD] no dataloader: ${url.slice(0, -40)}`);
github Urigo / WhatsApp-Clone-Server / modules / users / auth.provider.ts View on Github external
import { Injectable, Inject, ProviderScope } from '@graphql-modules/di';
import { ModuleSessionInfo } from '@graphql-modules/core';
import { Response } from 'express';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { secret, expiration } from '../../env';
import { validateLength, validatePassword } from '../../validators';
import { Users } from './users.provider';
import { User } from '../../db';

@Injectable({
  scope: ProviderScope.Session,
})
export class Auth {
  @Inject() private users: Users;
  @Inject() private module: ModuleSessionInfo;

  private get req() {
    return this.module.session.req || this.module.session.request;
  }

  private get res(): Response {
    return this.module.session.res;
  }

  async signIn({ username, password }: { username: string; password: string }) {
    const user = await this.users.findByUsername(username);
github Urigo / WhatsApp-Clone-Server / modules / users / users.provider.ts View on Github external
import { Injectable, Inject, ProviderScope } from '@graphql-modules/di';
import sql from 'sql-template-strings';
import bcrypt from 'bcrypt';
import { Database } from '../common/database.provider';

const DEFAULT_PROFILE_PIC = 'https://raw.githubusercontent.com/Urigo/WhatsApp-Clone-Client-React/legacy/public/assets/default-profile-pic.jpg'

@Injectable({
  scope: ProviderScope.Session,
})
export class Users {
  @Inject() private db: Database;

  async findById(userId: string) {
    const db = await this.db.getClient();
    const { rows } = await db.query(
      sql`SELECT * FROM users WHERE id = ${userId}`
    );

    return rows[0] || null;
  }

  async findAllExcept(userId: string) {
    const db = await this.db.getClient();
    const { rows } = await db.query(
github Urigo / WhatsApp-Clone-Server / modules / chats / chats.provider.ts View on Github external
type ChatsByUser = { userId: string };
type ChatByUser = { userId: string; chatId: string };
type ChatById = { chatId: string };
type ChatsKey = ChatById | ChatByUser | ChatsByUser;

function isChatsByUser(query: any): query is ChatsByUser {
  return query.userId && !query.chatId;
}

function isChatByUser(query: any): query is ChatByUser {
  return query.userId && query.chatId;
}

@Injectable({
  scope: ProviderScope.Session,
})
export class Chats {
  @Inject() private db: Database;
  @Inject() private pubsub: PubSub;

  private chatsCache = new Map();
  private loaders = {
    chats: new DataLoader(keys => {
      return Promise.all(
        keys.map(async query => {
          if (isChatsByUser(query)) {
            return this._findChatsByUser(query.userId);
          }

          if (this.chatsCache.has(query.chatId)) {
            return [this._readChatFromCache(query.chatId)];

@graphql-modules/di

MIT
Latest version published 4 years ago

Package Health Score

66 / 100
Full package analysis

Similar packages