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

typed-graphql-class

v1.0.4

Published

Query builder for GraphQL Queries/Mutations in TypeScript using classes.

Downloads

10

Readme

Typed GraphQL Class

This package depends of the typed-graphqlify. The goal is to simplify write the Query or Mutation, writing the query as an array.

To install: npm i typed-graphql-class

Write the queries as array

Usage

Solution

  • Create the (types)[https://graphql.org/learn/schema/#type-system] with the interface IColumnType.
  • Define the query or mutation class.
  • Call the query or mutation class with the columns and/or variables in an array, and get the query ready to send to the server.

Simple query example:

File TextType.tsx in graphql/types;

import { types } from 'typed-graphql-class';
import { IColumnType } from 'typed-graphql-class/dist/interfaces';
const textType: IColumnType[] = [
  {
    name: 'idLang',
    resolve: types.string
  },
  {
    name: 'text',
    resolve: types.string
  },
  {
    name: 'idTranslation',
    resolve: types.number
  },
];
export default textType;

File: TextQuery.tsx in 'graphql/queries'

import { Query } from 'typed-graphql-class';
import { textType } from 'graphql/types';
class TextQuery extends Query {
  constructor() {
    super(
      'text',
      textType,
      { idLang: 'String' }
    );
  }
}
export default new Text();

How to use it:

const query = textQuery.toString({
  columns: ['text', 'idLang','idTranslation'],
  variables: ['idLang']
});
console.log(query);

Result

query text($idLang: String) {
  text(idLang: $idLang) {
    text
    idLang
    idTranslation
  }
}

Query of queries example:

File TranslationType.tsx in graphql/types;

import { types, GraphQL } from 'typed-graphql-class';
import { IColumnType } from 'typed-graphql-class/dist/interfaces';
const translationType: IColumnType[] = [
  {
    name: 'idTranslation',
    resolve: types.number
  },
  {
    name: 'code',
    resolve: types.string
  },
  {
    name: 'text',
    resolve: (args: ColVarInterface) => {
      const graphQL = new GraphQL(textType, { idLang: 'String' });
      return graphQL.resolve(args);
    }
  },
  {
    name: 'texts',
    resolve: (args: ColVarInterface) => {
      const graphQL = new GraphQL(textType);
      return graphQL.resolve(args);
    }
  },
];

File: TranslationQuery.tsx in 'graphql/queries'

import { Query } from 'typed-graphql-class';
class Translation extends Query{
  constructor() {
    super(
      'translation',
      translationType,
      {
        idTranslation: server.int,
        idLang: server.string,
      }
    );
  }
}
export default new Translation();

How to use it:

const query = translationQuery.toString({
  columns: [
    'idTranslation',
    'code',
    {
      'text': {
        columns: ['text', 'idLang'],
        variables: ['idLang'],
      }
    }
  ],
  variables: ['idTranslation']
});
console.log(query);

Result

query translation($idLang: String, $idTranslation: Int) {
  translation(idTranslation: $idTranslation) {
    idTranslation
    code
    text(idLang: $idLang) {
      text
      idLang
    }
  }
}

How to use it with Ajax library

Using the Query of queries example.

interface ITranslationArgs {
  idLang: string;
  idTranslation: number;
}
const getTranslation = async (variables: ITranslationArgs) => {
  const query = translationQuery.toString({
    columns: [
      'idTranslation',
      'code',
      {
        'text': {
          columns: ['text', 'idLang'],
          variables: ['idLang'],
        }
      }
    ],
    variables: Object.keys(variables)
  });
  const url = 'http://mydomain.com/graphql';
  const response = await axios.post(url, { query, variables });
  console.log(response);
}

Autocomplete queries

There are two examples to create the type to create the array typed.

Simple query Example

export interface IText {
  idText: number;
  text: string;
  idLang: string;
  idTranslation: number;
}

export type TTextKeys = keyof IText;

Query of queries example:

export interface ITranslation {
  idTranslation: number;
  code: string;
  text: IText;
}

export interface ITextArgs {
  columns: TTextKeys[],
  variables: ['idLang'];
}

type TTranslationSimpleKeys = keyof Omit<ITranslation, 'text'>;

export type TTranslationKeys = (
  TTranslationSimpleKeys |
  { text: ITextArgs }
);

Operation Class

The Operation class return two values that are needed to the ajax call. For instance

const columns: TTranslationKeys[] = [
  'idTranslation',
    {
      text: {
        columns: ['idText', 'text'],
        variables: ['idLang'],
      },
    },
];
const idTranslation = 5;
const operation = new Operation(
  { idTranslation },
  columns,
  translationQuery
);
const params = operation.params();

The params variable has the query and the variables values:

const received = {
  query: `
    query translation($idLang: String, $idTranslation: Int) {
      translation(idTranslation: $idTranslation) {
        idTranslation
        code
        text(idLang: $idLang) {
          text
          idLang
        }
      }
    }`,
  variables: { "idTranslation": 5 },
};

So you now you can do it, in order to call the endpoint:

const response = await axios.post(url, operation.params());

😀🙂