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

graphql-schema-to-query

v1.2.0

Published

Generate parametised graphql templates from graphql schema files.

Downloads

10

Readme

graph-schema-to-query-templates

This repo contains code that enables conversion of graphql schema files (.gql / .graphql) into query template strings.

Notes

  • Supports mjs and cjs imports
  • Supports nested queries and mutation types, if searchTokens is provided
  • Currently supports building templates from Mutation and Query types. Templates are built recursively.

Usage Example

Installation

npm i graphql-schema-to-query

For example with a schema file test.graphql

type Query {
    foo: String
    bar: Int
    animals: AnimalQueries
}

type AnimalQueries {
    birds: BirdQueries
    getAnimals(names: [String!]!): [Animal]

}

type BirdQueries {
    parrot: String
    chicken: String
    birdsWithFeathers: [Animal]
}

type Animal {
    name: String
    species: String
}

type Mutation {
    updateBird(id: String): Animal
}

Set your input, output paths and serviceName, then call buildCollection and exportCollection.

import {GqlToTemplate} from 'graphql-schema-to-query'

new GqlToTemplate({
  // Link to your source .gql or .graphql file
  inputPath: "output/test.graphql",
  // Link to folder where files will be saved
  outputPath: "output",
  // Name of your service, this is arbitrary but should not contain hyphens
  serviceName: "BIRD_SERVICE",
  // defaults to ["query", "queries", "mutation", "mutations"]
  //   searchTokens: []
})
  .buildCollection()
  .exportCollection({
    // js or ts
    extension: "ts",
    // logs templates
    debug: true
  })

Produces the following files in the output folder

BIRD_SERVICE__bar.ts
BIRD_SERVICE__birdsWithFeathers.ts
BIRD_SERVICE__chicken.ts
BIRD_SERVICE__foo.ts
BIRD_SERVICE__getAnimals.ts
BIRD_SERVICE__parrot.ts
BIRD_SERVICE__updateBird.ts

with the following contents


export const template = `query BIRD_SERVICE____foo {foo }`
export const template = `query BIRD_SERVICE____bar {bar }`
export const template = `query BIRD_SERVICE__animals__birds__parrot {animals { birds { parrot  } }}`
export const template = `query BIRD_SERVICE__animals__birds__chicken {animals { birds { chicken  } }}`
export const template = `query BIRD_SERVICE__animals__birds__birdsWithFeathers {animals { birds { birdsWithFeathers {name  species } } }}`
export const template = `query BIRD_SERVICE__animals__getAnimals($names: [String!]!) {animals { getAnimals(names: $names) {name  species } }}`
export const template = `mutation BIRD_SERVICE____updateBird($id: String) {updateBird(id: $id) {name  species }}`