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

maj-graphql-client

v1.1.0

Published

An easy to use TypeScript GraphQL client

Downloads

4

Readme

Maj GraphQL Client

Install

Run npm i maj-graphql-client

Usage

With the following GraphQL schema :

type Article {
    id: ID!
    title: String!
    content: String!
    comments: [Comment]!
}

type Comment {
    id: ID!
    text: String!
}

input ArticleInput {
    title: String!
    content: String!
}

input CommentInput {
    text: String!
}

type Query {
    articles: [Article]!
    articleById(id: ID): Article!
}

type Mutation {
    addArticle(article: ArticleInput!): Article
    addComment(articleId: ID!, comment: CommentInput!): Comment
}

Building queries

const fetchArticles: Query = new QueryBuilder('FetchArticles')
    .withPath('articles') // Query#articles from the schema.
    .withProperties(['id', 'title'])
    .buildQuery();

const fetchArticleById: Query = new QueryBuilder('FetchArticleById')
    .withPath('articleById') // Query#articleById from the schema.
    .withArgs([{name: 'id', type: 'ID!'}])
    .withProperties(['id', 'title', 'content', {'comments': ['id', 'text']}])
    .buildQuery();
console.log(fetchArticles.toGQL());

Outputs :

query FetchArticles {
    articles {
        id,
        title
    }
}
console.log(fetchArticleById.toGQL());

Outputs :

query FetchArticleById($id: ID!) {
    articleById(id: $id) {
        id,
        title,
        content,
        comments {
            id,
            text
        }
    }
}

Building mutations

const addArticle: Mutation = new QueryBuilder('AddArticle')
    .withPath('addArticle') // Mutation#addArticle from the schema.
    .withArgs([{name: 'article', type: 'ArticleInput!'}])
    .withProperties(['id', 'title'])
    .buildMutation();

const addComment: Mutation = new QueryBuilder('AddComment')
    .withPath('addComment') // Mutation#addComment from the schema.
    .withArgs([{name: 'articleId', type: 'ID!'}, {name: 'comment', type: 'CommentInput!'}])
    .withProperties(['id', 'text'])
    .buildMutation();
console.log(addArticle.toGQL());

Outputs :

mutation AddArticle($article: ArticleInput!) {
    addArticle(article: $article) {
        id,
        title
    }
}
console.log(addComment.toGQL());

Outputs :

mutation AddComment($articleId: ID!, $comment: CommentInput!) {
    addComment(articleId: $articleId, comment: $comment) {
        id,
        text
    }
}

Minifying GQL queries

The Query#toGQL() method returns the query string in a readable format, with linebreaks, whitespaces and indentations, which is great for debugging. But when sending the query through HTTP, those linebreaks, whitespaces and indentations are unnecessary and increase the payload size.

When sending the query string through HTTP, you can use the Query#toMinifiedGQL() method that will remove every unnecessary linebreak, whitespace and indentation but still return a valid GQL query.

Sending a query through HTTP

When using your favorite HTTP client, maybe axios, fetch or Angular's own HttpClient, send a POST request to the GraphQL server with a body such as :

{
    query: string;
    variables: [key: string]: any
}

Example

const fetchArticleById: Query = new QueryBuilder('FetchArticleById')
    .withPath('articleById') // Query#articleById from the schema.
    .withArgs([{name: 'id', type: 'ID!'}])
    .withProperties(['id', 'title', 'content', {'comments': ['id', 'text']}])
    .buildQuery();

const id = 1;

http.post('graphql-server-url', {
    query : fetchArticleById.toMinifiedGQL(),
    variables: {
        id: 1
    }
})

Additional notes

Query and Mutation use lazy attributes to store the resulting GQL query strings, Query#toGQL() and Query#toMinifiedGQL() will only compute the GQL query string the first time they are called, after that, stored query strings will be returned directly.