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-artisan

v0.2.2

Published

TypeScript compatible GraphQL generator.

Downloads

9

Readme

GraphQL Artisan

NPM version NPM downloads Build status Test coverage

Type safe GraphQL query generator for TypeScript. Works with typescript@>=3.0.

Installation

npm install graphql graphql-artisan

Usage

The library comes with a CLI that allows converting the GraphQL schema into a TypeScript GraphQL query generator API. Currently only .graphql schema files are supported, but support for introspection JSON and GraphQL server endpoints is to be added later.

To convert a schema file into typescript, simply run:

graphql-artisan input.graphql output.ts

The output will also depend on the graphql-artisan and graphql libraries and can be imported to your program for dynamic query generation.

Examples

All the examples below are written using the following schema:

type User { id: ID! name: String! age: Int }
interface Message { id: ID! createdBy: ID! title: String! }
type Headline implements Message { id: ID! createdBy: ID! title: String! tags: [String] }
type Article implements Message { id: ID! createdBy: ID! title: String! content: String }
input UserEdit { id: ID! name: String age: Int }

type Query {
  user(id: ID!): User
  userMessages(id: ID): [Message!]!
}
type Mutation {
  updateUser(user: UserEdit!): User
}
schema {
  query: Query
  mutation: Mutation
}

Simple queries

You can create anonymous queries with a simple syntax:

schema.query().select(
  Query.user({ id: "123" }).select(
    User.id(),
    User.name()
  )
);

This can be serialized into the following GraphQL query:

{
  user(id: "123") {
    id
    name
  }
}

This might not seem very interesting, but notice that you can also generate queries like this:

function users(...userIds: number[]) {
  return userIds.map(id =>
    Query.user({ id }).select(
      User.id(),
      User.name()
    ).as(`user${id}`)
  );
}
schema.query().select(users(1, 2, 3));

The query above will be serialized as:

{
  user1: user(id: 1) {
    id
    name
  }
  user2: user(id: 2) {
    id
    name
  }
  user3: user(id: 3) {
    id
    name
  }
}

All without any ugly string concatenations or unsafe operations.

Named queries

Just like above, you can also name your queries, which is required to be able to add directives:

schema.query("queryName").select(
  Query.user({ id: "123" }).select(
    User.id(),
    User.name()
  )
);

The query name will be added to the serialized query as expected:

query queryName {
  user(id: "123") {
    id
    name
  }
}

Queries with directives

Because @ sign is not allowed in JavaScript/TypeScript names, $ is used instead for directives. You can for example write the following query:

schema.query().select(
  Query.user({ id: "123" }).select(
    User.id($skip({ if: true })),
    User.name()
  )
);

It will result in the following query:

{
  user(id: "123") {
    id @skip(if: true)
    name
  }
}

Queries containing interfaces

If the query requires selecting an interface and you only want to select its interface fields, you can write the query as expected:

schema.query().select(
  Query.userMessages({ id: "123" }).select(
    Message.id(),
    Message.createdBy(),
    Message.title()
  )
);

The serialized version has no surprises either:

{
  userMessages(id: "123") {
    id
    createdBy
    title
  }
}

However, if you want to also query for some fields that implement that interface, you need to be able to use fragments. Don't worry, this is not difficult either:

schema.query().select(
  Query.userMessages({ id: "123" }).select(
    Message.id(),
    Message.createdBy(),
    Message.title(),
    Headline$InlineFragment().select(
      Headline.tags()
    ),
    Article$InlineFragment().select(
      Article.content()
    )
  )
);

The inline fragments will now be serialized into the query as specified:

{
  userMessages(id: "123") {
    id
    createdBy
    title
    ... on Headline {
      tags
    }
    ... on Article {
      content
    }
  }
}

Mutations

Mutations work just like queries and the arguments are also typed as expected. If doing multiple mutations you can also always rely that the order of the selections in code is the same as in the resulting query. An example mutation could go like this:

schema.mutation("mutationName").select(
  Mutation.updateUser({
    user: {
      id: "123",
      name: "John Doe"
    }
  }).select(
    User.id(),
    User.name()
  )
);

You will then get your mutation as a GraphQL query string when serialized:

mutation mutationName {
  updateUser(user: {id: "123", name: "John Doe"}) {
    id
    name
  }
}

License

MIT