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

nodejs-react-typescript--graphql-code-generator

v1.0.24

Published

___ #### add gen script to your `package.json` file ___

Downloads

12

Readme

Setup server side


add gen script to your package.json file


{
   "scripts": {
      "gen": "node ./node_modules/nodejs-react-typescript--graphql-code-generator --server --dist='./src/generated' --if='../interface'",
   },
}

where: --server - means generate server code; --dist - where generated code will be located; --if - path to folder with your interfaces IQuery.ts IMutation.ts ISubscription.ts;


add server.ts file


import {resolvers} from './generated/resolvers';
import {typeDefs} from './generated/mergedGQLSchemas';
import http from 'http';

const express = require('express');
const { ApolloServer } = require('apollo-server-express');

const PORT = 4001;

const app = express();

const server = new ApolloServer({
   typeDefs,
   resolvers,
});

server.applyMiddleware({
   app
});

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(PORT, () => {
  console.log(`🚀 server ready at http://localhost:${PORT}${server.graphqlPath}`);
  console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
});

THAN RUN NEXT USUAL COMMANDS

npm run gen
npm run build
npm run start

Setup client side

add gen script to your package.json file


{
   "scripts": {
      "gen": "node ./node_modules/nodejs-react-typescript--graphql-code-generator --client --dist='./src/generated' --if='../interface'",
   },
}

where: --client - means generate client code; --dist - where generated code will be located; --if - path to folder with your interfaces IQuery.ts IMutation.ts ISubscription.ts;


THAN RUN NEXT USUAL COMMANDS

npm run gen
npm run build
npm run start

Simple usage use-case IQuery

This code generator generates all server and client side code. You need just to specify interface for communication in typescript style. Create file IQuery.ts in interfaces folder with the following content:

interface IUser {
   id: string;
   name: string;
   age: number;
}
interface IShortProject {
   id: string;
   name: string;
}
interface IProject {
   id: string;
   name: string;
   users: IUser[];
   user: (id: string) => IUser;
}
interface IAccount {
   project: (id: string) => IProject|IShortProject;
   projects: IShortProject[];
}
interface IQuery {
   account: (token: string) => IAccount;
}

Run code generation

npm run gen

Than you will have next requests to use

// all generated code will be here in the generated folder
import {query} from "./generated/query.requests";
import {IAccount, IUser} from "./generated/query.interfaces";
import {IProject} from "./generated/query.interfaces";
import {IShortProject} from "./generated/query.interfaces";

class SomeComponent extends React.Component<any, any> {
   // this method shows that not all generated requests you will want to use
   fetchWholeAccount = async () => {
      const accountToken = 'some token';
      const projectId = 'some id';
      const userId = 'some id';

      // I see no one case where you will need to fetch all account data, 
      // but anywhere this function will be available in the generated code

      const account: IAccount = await query.account(accountToken).fetchIAccount(projectId, userId);
      console.log(account.project);
      console.log(account.project.user.id);
      console.log(account.project.user.name);
      console.log(account.project.user.age);

      account.project.users.map((user: IUser) => {
         console.log(`here will be available all IUser fields ${user.id} ${user.name} ${user.age}`);
      });

      account.projects.map((prj: IShortProject) => {
         console.log(`here will be available only two fields from IShortProject interfaces ${prj.id} ${prj.name}`);
      });
   };
   // this method shows that you can define short interfaces if you want fetch not all data from node
   fetchShortProject = async () => {
      const accountToken = 'some token';
      const projectId = 'some id';

      const shortProject: IShortProject = await query.account(accountToken).project(projectId).fetchIShortProject();
      console.log(shortProject.id);
      console.log(shortProject.name);
   };
   // this method shows that not all generated requests you will want to use
   fetchProject = async () => {
      const accountToken = 'some token';
      const projectId = 'some id';
      const userId = 'some id';

      // this call also not useful, because it is same situation with fetching all account data
      // too math data will be fetched and then available
      const project: IProject = await query.account(accountToken).project(projectId).fetchIProject(userId);
      console.log(project);
      console.log(project.user.id);
      console.log(project.user.name);
      console.log(project.user.age);

      project.users.map((user: IUser) => {
         console.log(`here will be available all IUser fields ${user.id} ${user.name} ${user.age}`);
      });
   };
   // this method shows how to fetch concrete user data
   fetchShortProjects = async () => {
      const accountToken = 'some token';

      const projects: IShortProject[] = await query.account(accountToken).projects().fetchArrayIShortProject();
      projects.map((prj: IShortProject) => {
         console.log(`here will be available only two fields from IShortProject interfaces ${prj.id} ${prj.name}`);
      });
   };
   // this method shows how to fetch concrete user data
   fetchUser = async () => {
      const accountToken = 'some token';
      const projectId = 'some id';
      const userId = 'some id';

      const user: IUser = await query.account(accountToken).project(projectId).user(userId).fetchIUser();
      console.log(user.id);
      console.log(user.name);
      console.log(user.age);
   };
   // this method shows how to fetch all users
   fetchUsers = async () => {
      const accountToken = 'some token';
      const projectId = 'some id';
      const userId = 'some id';

      const users: IUser[] = await query.account(accountToken).project(projectId).users().fetchArrayIUser();
      users.map((user: IUser) => {
         console.log(`here will be available all IUser fields ${user.id} ${user.name} ${user.age}`);
      });
   };
}