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

@anchan828/graphql-decorator

v1.0.21

Published

Creates GraphQL Schema from decorated TypeScript Class. [Note] This package forked from https://github.com/Quramy/graphql-decorator

Downloads

72

Readme

graphql-decorator Build Status npm version

Helps to build GraphQL schema with TypeScript.

It provide the following features:

  • Decorators(@ObjectType, @Schema, @NonNull, and more...) corresponding to GraphQL type system.
  • A function to create GraphQL Schema from decorated TypeScript class.

Getting started

This tool requires Node.js v4.4.0 or later.

npm i @anchan828/graphql-decorator

This tool uses ES.next Decorators and Reflect, so create tsconfig.json :

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": false,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
    "exclude": [
        "node_modules"
    ]
}

And write .ts code such as:

/* main.ts */

import { Schema, Query, ObjectType, Field, schemaFactory } from "graphql-decorator";
const graphql = require("graphql").graphql;

// @ObjectType creates GraphQLObjectType from a class
@ObjectType()
class QueryType {
    @Field() greeting(): string {
        return "Hello, world!";
    }
}

// @Schema creates GraphQLSchema from a class.
// The class should have a field annotated by @Query decorator.
@Schema()
class SchemaType {
    @Query() query: QueryType;
}

async function main() {

    // create schema from annotated class
    const schema = schemaFactory(SchemaType);

    const result = await graphql(schema, `query { greeting } `);
    console.log(result.data.greeting);
}

main();

Finally, execute the above schema:

tsc main.ts && node main.js
# -> Hello, world!

Guide

Schema

You can declare a GraphQL schema class with @Schema, @Query and @Mutation decorators.

For example:

import { Schema, Query, Mutation } from "graphql-decorator";

@Schema()
class MySchema {
  @Query() query: RootQuery;
  @Mutation() mutation: Mutations;
}

A schema class should a field annotated by @Query, which represents that the type of this filed will be a root query of GraphQL. And the type of this field should be annotated by @ObjectType.

The field annotated by @Mutation also represents mutation of your GraphQL schema.

Object Type

You can annotate your class with @ObjectType()

For example:

@ObjectType()
class SomeObject {
  @Field() title: string;

  @Field() greeting(): string {
    return "Hello";
  }
}

The above example has 2 fields, the one is title and the another is greeting.

You can set the @Field decorator to your class's properties and methods. The fields annotated by @Field will be exposed as fields of this object in GraphQL schema. And when you set @Field to methods, the methods will work as the resolver function in schema.

Type of field

By the default, @Field detects GraphQLScalarType corresponding to the field type.

You can explicitly configure the type of the fields using type option.

@ObjectType() class User {
  @Field() name: string;
}

@ObjectType() class SomeObject {
  @Field({type: User}) user: User;
}

NonNull, List

You can use @NonNull and @List decorator. For example:

@ObjectType()
class User {
  @NonNull() @Field({type: graphql.GraphQLID})
  id: string;
}

@ObjectType()
class Query {
  @List() @Field({type: User}) getAllUsers(): Promise<User[]> {
    /* implementation for fetch all users */
  }
}

Resolver's arguments

You can use @Arg for declare arguments of resolver function. For example:

@ObjectType()
class MutationType {
  @Field({type: User}) deleteUser(
    @Arg({name: "id"}) id: string
  ) {
    /* implementation for delete user */
  }
}

And you can declare GraphQL InputObjectType with @InputObjectType decorator.

@InputObjectType()
class UserForUpdate {
  @Field() name: string;
  @Field() emal: string;
}

@ObjectType()
class MutationType {
  @Field({type: User}) updateUser(
    @Arg({name: "id"}) id: string,
    @Arg({name: "input"}) input: UserForUpdate
  ) {
    /* implementation for delete user */
  }
}

API Usage

T.B.D.

Examples

Please checkout exmaples folder in this repository.

License

This software is released under the MIT License, see LICENSE.txt.