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

@tenry/graphql-decorators

v0.1.5

Published

Generate GraphQL schema with annotated classes.

Downloads

1

Readme

@tenry/graphql-decorators

This package provides useful TypeScript decorators for easy creation of GraphQL types and inputs as well as controllers for reading and writing data.

Example

// import the library
import {Manager, decorators} from '@tenry/graphql-decorators';

// define an output type
@decorators.type()
class UserType {
  @decorators.field('ID')
  id: string;

  // primitive types like string or number is automatically detected,
  // if the emitDecoratorMetadata flag is enabled
  @decorators.field()
  name: string;

  @decorators.field('JSON')
  data: Object;

  // use this syntax, if the data type is an array of something
  @decorators.field({list: UserType})
  friends: UserType[];
}

// define an input type
@decorators.input()
class UserInput {
  @decorators.field()
  name: string;

  @decorators.field('JSON')
  data: Object;

  @decorators.field({list: UserInput})
  friends: UserInput[];
}

// define a controller
class UserController {
  @decorators.query({
    parameters: {
      id: {
        type: String,
      },
    },
    returnType: UserEntity,
    list: true, // the returnType is returned as a list
  })
  users({id}) {
    if(id) {
      return [getUserById(id)];
    } else {
      return getAllUsers();
    }
  }

  @decorators.mutation({
    parameters: {
      user: {
        type: UserInput,
      },
    },
    returnType: UserType,
  })
  addUser({user}) {
    return createUser(user);
  }

  @decorators.mutation({
    parameters: {
      id: {
        type: String,
      },
      user: {
        type: UserInput,
      },
    },
    returnType: UserType,
  })
  updateUser({id, user}) {
    return updateUser(id, user);
  }

  @decorators.mutation({
    parameters: {
      id: {
        type: String,
      },
    },
    returnType: UserType,
  })
  removeUser({id}) {
    return removeUser(id);
  }
}

// now it's time to set everything up!
const manager = new Manager();
const userController = new UserController();

// register all available controllers
manager.registerObject(userController);

// get GraphQL schema
const schema = manager.createSchema();

// now do whatever you would do with a GraphQL schema
graphql(schema, someAwesomeGraphqlQuery).then(response => {
  console.log(response);
});

// or (using express and express-graphql):
const app = express();

app.use('/graphql', graphqlHTTP({
  schema,
}));

app.listen(8080);

Using the example above, you can use GraphQL queries like the following:

query {
  users {
    id
    name
    data
    friends {
      id
      name
      data
    }
  }
}
mutation {
  addUser(user: {name: "Max"}) {
    id
  }
}

Installation and Usage

Use npm to install the package:

$ npm install graphql graphql-type-json @tenry/graphql-decorators

Now import the Manager class and the decorators:

import {Manager, decorators} from '@tenry/graphql-decorators';

const manager = new Manager();

// define types, inputs and controllers here
// register controllers to the manager via manager.registerObject(new Controller()); here

// retrieve GraphQL schema
const schema = manager.createSchema();

Decorators

@type(options?: string | TypeOptions)

Use this decorator for a class to declare it as an output type.

TypeOptions

  • name: alternative name for the type. By default, the class name is used.

@input(options?: string | TypeOptions)

Use this decorator for a class to declare it as an input type.

TypeOptions

  • name: alternative name for the type. By default, the class name is used.

@entity()

Use this decorator for a class to declare it as both, an input and output type.

@field(options?: string | PrimitiveConstructor | FieldOptions)

Use this decorator for a class property. options can either be an existing type name (such as 'ID', 'Int', 'JSON'), a primitive type (Boolean, String or Number) or FieldOptions.

FieldOptions

  • list: if the field type is a list of another type, use this option to define the base type.

@query(options?: QueryOptions)

Use this decorator for a controller method to declare it as a query handler.

QueryOptions

  • parameters: object, which defines the parameters.
  • returnType: return type (see @field options for possible values)
  • list: true, if returnType is returned as a list

@mutation(options?: MutationOptions)

Use this decorator for a controller method to declare it as a mutation handler.

MutationOptions

  • parameters: object, which defines the parameters.
  • returnType: return type (see @field options for possible values)
  • list: true, if returnType is returned as a list

License

@tenry/graphql-decorators is licensed under the MIT License.