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

@micimize/graphql-codegen-compiler

v0.9.0-alpha.1

Published

GraphQL code generator compiler

Downloads

8

Readme

graphql-codegen-compiler

This package compiles the output of graphql-codegen-core along with GeneratorConfig and Settings, and compiles the template, returns an array of FileOutput.

The main entry point of the package is compile method, and you can import it directly and use it without the CLI package.

We are using Handlebars as template compiler, with some custom Handlebars helpers that helps to generate GraphQL related code easily.

GraphQL-related Template Helpers

toPrimitive(type: string)

Accepts a string with a GraphQL type and converts it to the language primitive as specified in the template config, if the type isn't a primitive - it just returns it.

Example:

type MyType {
  f1: String
}
{{#each types}}
    Type {{ name }} fields:
    {{#each fields}}
        Field {{ name }} type is: {{ toPrimitive type }}
    {{/each}}
{{/each}}

Output:

Type MyType fields:
    Field f1 type is: string

ifDirective(context: any, directiveName: string)

Special GraphQL helper that accepts any GraphQL entity, and extracts the GraphQL Directives of the entity.

The compiled context is the arguments values of the entity.

Example:

type MyType @addName(name: "Dotan") {
  f1: String
}

directive @addName(name: String!) on OBJECT
{{#each types}}
    Type name: {{ name }}
    Extra name? {{#ifDirective this "addName"}}Yes! and the name is: {{ name }}{{/ifDirective}}

{{/each}}

Output:

Type name: MyType
Extra name? Yes! and the name is: Dotan

unlessDirective(context: any, directiveName: string)

The opposite of ifDirective.

Example:

type MyType {
  f1: String
}

directive @addName(name: String!) on OBJECT
{{#each types}}
    Type name: {{ name }}
    Extra name? {{#unlessDirective this "addName"}}No!{{/unlessDirective}}

{{/each}}

Output:

Type name: MyType
Extra name? No!

withGql(type: string, name: string)

Locates GraphQL element of type with name name, use it if you need to access GraphQL specific element, for example "MyType" of "type".

Super useful when you need to access the actual object inside withImports or in any other case.

type MyType {
  f1: String
}
{{#withGql "type" "MyType"}}
    {{ name }}
{{/withGql}}

Output:

MyType

eachImport(context)

Locates all external uses of context, and returns an array of: { name: string, type: string, filename: string }, so you can use it to create imports when generating multiple files.

context can be any GraphQL available object.

type MyType {
  f: OtherType
}
{{#eachImport this }}
import { {{ name }} } from './{{ file }}';
{{/eachImport}}

Output:

import { OtherType } from './othertype.type';

toComment(str: string)

Prints a string as comment with /* ... */, and also trims multiple lines into a single line.

Useful for description field of GraphQL entities.

Example:

{{toComment "hi"}}

Output:

/* hi */

eachImport(element: any)

Iterates over a calculated array of imports (file names) that in use by the element.

Example:

{{#eachImport type}}
    import { {{ name }} } from './{{file}}';
{{/eachImport}}

Other Template Helpers

times(count: number)

Returns the template child string count times, the execution context of the child content is the i/times.

Example:

{{#times 3}}
    Hello {{ this }}!
{{/times}}

Output:

Hello 0
Hello 1
Hello 2

for(from: number, to: number, incr: number)

Similar to for loop.

Returns the template child string amount of times according to from to to by increasing incr, the execution context of the child content is the i/times.

Example:

{{#for 3 6 1}}
    Hello {{ this }}!
{{/times}}

Output:

Hello 3
Hello 4
Hello 5

limitedEach(from: number, to: number, incr: number)

Similar to for loop.

Returns the template child string amount of times according to from to to by increasing incr, the execution context of the child content is the i/times.

Example:

{{#for 3 6 1}}
    Hello {{ this }}!
{{/times}}

Output:

Hello 3
Hello 4
Hello 5

toLowerCase(str: string)

Return a lowercase version of the string.

Example:

{{toLowerCase "Hello" }}

Output:

hello

toUpperCase(str: string)

Return an uppercase version of the string.

Example:

{{toUpperCase "Hello" }}

Output:

HELLO

toSnakeCase(str: string)

Return an snake case version of the string.

Example:

{{toSnakeCase "doSomething" }}

Output:

do-something

toTitleCase(str: string)

Return an title case version of the string.

Example:

{{toTitleCase "doSomething" }}

Output:

Do Something

toCamelCase(str: string)

Return an camel case version of the string.

Example:

{{toCamelCase "DoSomething" }}

Output:

doSomething

multilineString(str: string)

Converts a multiline string into a string with line breaks, to prevent code from being broken.

Example:

{{toCamelCase "myString
other line" }}

Output:

"myString" + 
"other line"

ifCond(p1: any, comparator: string, p2: any)

Executes a simple if command of two parameters, using comparator.

Available comparators: ===, ==, !==, !=, <, <=, >, >=, &&, ||.

Example:

{{#ifCond "test" "===" "test"}}
   Hi!
{{/ifCond}}

Output:

    Hi!