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

@sparklink-pro/apant-codegen

v1.0.0

Published

Apant graphql code generator based on graphql-code-generator

Downloads

4

Readme

Apant Codegen

Install

yarn add @sparklink-pro/apant-codegen

Description

This plugin generate operations code and default type fragments in an opiniated way from a graphql schema and a document containing fragments.
We identify two kinds of Object type: Regular Object types and Operation payload types (ie. The output type of an operation usually used only for the said operation).

A Type is considered as a Operation payload type, if it ends by Payload.

  • The type User would be considered as a regular type.
  • The type BooksPayload would be considered as an operation payload type.

The default fragment for a given type is _<Type name>.

  • The default fragment for a type User would be _User.
  • The default fragment for a type BooksPayload would be _BooksPayload.

The alias of the result of an operation is always res.

The generation consists of two steps:

  • First, the generator generates default fragments if they don't exist for every regular object types.
  • Second, the generator generates operations code for every defined operation in the schema.

Step 1 : Fragments generation

For every type non identified as a regular type (ie. That doesn't end with Payload), the generator will generated a default fragment for that type.
The generated fragment will include all scalar fields of the type but not the fields of the type's subtypes.
If there is no scalar fields in the fragment, the fragment is skipped.
The fragment who doesn't match a real type are removed.

Step 2 : Operations code generation

For every operation defined in the schema, the generator will generate a query or mutation code based on if a fragment for the operation or the payload exists or not.
If a fragment exists with the name of the operation, it will be used. If a fragment exists for the operation payload, it is used as resultset. Otherwise, the resultset is the list of fields of the operation payload type.
If the resultset is a list of fields, the generator will go one level deep to generate the fields selection.

Examples

Given the following schema:

# schema.graphql

type Book {
  id: ID!
  name: string
  rank: Int
}

type BooksPayload {
  books: [Books!]!
  count: Int!
}

query {
  TopBooks: BooksPayload
}

Case 1 : The operation fragment exists

# fragments.graphql

fragment TopBooks on BooksPayload {
  count
  books {
    id
    title
    rank
  }
}
...

The resulting query code will be:


# operations.graphql

query TopBooks {
  res: TopBooks {
    ...TopBooks
  }
}

Case 2 : The payload fragment exists

# fragments.graphql

fragment _BooksPayload on BooksPayload {
  count
  books {
    id
    title
  }
}
...

The resulting query code will be:


# operations.graphql

query TopBooks {
  res: TopBooks {
    ..._BooksPayload
  }
}

Case 3 : Neither the operation fragment, nor the payload fragment exist

# fragments.graphql
...

The resulting query code will be:


# operations.graphql

query TopBooks {
  res: TopBooks {
    count
    books {
      ..._Book
    }
  }
}