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

transform-graphql

v0.0.4

Published

Generate GraphQL Schema from another schema with directives

Downloads

12

Readme

Transform GraphQL

npm npm downloads npm downloads

We use GraphQL transformers. Examples are Graphback, Dgraph, AWS Amplify. This library provides function that given any GraphQL schema creates new GraphQL schemas basing on transformer functions.

Installation

npm i transform-graphql

How it works

Provide original schema with your transformer directives and an array of transformer functions defined by TransformerDef type


import { TransformGraphQLSchema } from 'transform-graphql';

const transformedSchema = TransformGraphQLSchema({ 
    schema: inputSchema, 
    transformers: [transformerCRUD] 
});

This short example simply shows what transform GraphQL is about:

Given the schema:

type Post @model{
    name: String!
    content: String!
    createdAt: String!
}

type Query{
    version:String
}
type Mutation{
    version:String
}

directive @model on OBJECT

where model is our actual transformer

We expect schema to be transformed into


directive @model on OBJECT

input CreatePost{
    name: String!
    content: String!
    createdAt: String!
}

input DetailsPost{
    id: String!
}

type Mutation{
    version: String
    post: PostMutation
}

type Post @model{
    name: String!
    content: String!
    createdAt: String!
}

type PostMutation{
    create(
            post: CreatePost
    ): String!
    update(
            post: UpdatePost
            details: DetailsPost
    ): String!
    remove(
            details: DetailsPost
    ): String!
}

type PostQuery{
    list: [Post!]!
    getByDetails(
            details: DetailsPost
    ): Post
}

type Query{
    version: String
    post: PostQuery
}
input UpdatePost{
    name: String!
    content: String!
    createdAt: String!
}
schema{
    query: Query,
    mutation: Mutation
}

And the transformer code should look like this

const inputSchema = `
type Post @model{
    name: String!
    content: String!
    createdAt: String!
}

type Query{
    version:String
}
type Mutation{
    version:String
}`
const transformerCRUD: TransformerDef = {
  transformer: ({ field, operations }) => {
    if (!field.args) {
      throw new Error('Model can be used only for types');
    }
    if (!operations.query) {
      throw new Error('Query type required');
    }
    if (!operations.mutation) {
      throw new Error('Query type required');
    }
    return `
      input Create${field.name}{
          ${TreeToGraphQL.parse({ nodes: field.args })}
      }
      input Update${field.name}{
          ${TreeToGraphQL.parse({ nodes: field.args })}
      }
      input Details${field.name}{
          id: String!
      }
      type ${field.name}Query{
          list: [${field.name}!]!
          getByDetails(details: Details${field.name}): ${field.name}
      }
      type ${field.name}Mutation{
          create( ${field.name[0].toLowerCase() + field.name.slice(1)}: Create${field.name} ): String!
          update( ${field.name[0].toLowerCase() + field.name.slice(1)}: Update${field.name}, details: Details${
      field.name
    } ): String!
          remove( details: Details${field.name} ): String!
      }
      extend type ${operations.query.name}{
          ${field.name[0].toLowerCase() + field.name.slice(1)}: ${field.name}Query
      }
      extend type ${operations.mutation.name}{
          ${field.name[0].toLowerCase() + field.name.slice(1)}: ${field.name}Mutation
      }
      `;
  },
  directiveName: 'model',
};
const transformedSchema = TransformGraphQLSchema({ schema: GraphQLTransform, transformers: [transformerCRUD] });
//transfomed schema should look like in the example

Roadmap

  • provide CLI
  • provide examples