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

v1.0.0-alpha4

Published

A @decorator based GraphQL schema builder.

Downloads

12

Readme

graphql-party makes it easy to create GraphQL schemas and resolvers from javascript classes using @decorators.

Install

yarn add graphql-party

Usage

Quickstart

import { GraphQLServer } from 'graphql-yoga';
import { Query, Arg, Field, Types, buildSchema } from 'graphql-party';

class Hello {
  @Query(Types.String)
  hello(
    @Arg('hello', Types.String)
    hello: string
  ) {
    return hello;
  }
}

const server = new GraphQLServer({
  schema: buildSchema(Hello),
});

server.start(() => console.log('Server is running on http://localhost:4000!'));

API

Class Decorators

  • @ObjectType({ name?: string; description?: string })
  • @InputType({ name?: string; description?: string })

Property Decorators

  • @Field(type, { name?: string; description?: string })

Method Decorators

  • @Query(type, { name?: string; description?: string })
  • @Mutation(type, { name?: string; description?: string })
  • @FieldResolver(typeFor, outputType, { name?: string; description?: string })

Method Parameter Decorators

  • @Arg(field: string, type: GraphQLPartyType)
  • @Context(field?: string): returns entire context without "field"

Utility functions

  • buildSchema(classesOrGlobs, config?: { cwd: string }): can be a glob of matching files for auto inclusion, or an array of classes that contain graphql-party metadata.
  • setInstance(Class, constructedClass): By default, any decorated class other than @ObjectType() or @InputType() get instantiated without any constructor arguments. You can instantiate it before hand and that will be reused from then on.

@ObjectType()

import { ObjectType, Field, Types } from 'graphql-party';

@ObjectType()
class Author {
  @Field(Types.NonNullable(Types.ID))
  id: string;

  @Field(Types.NonNullable(Types.String))
  name: string;
}

@ObjectType()
class Message {
  @Field(Types.NonNullable(Types.ID))
  id: string;

  @Field(Types.NonNullable(Types.String))
  content: string;

  @Field(Types.NonNullable(Types.String))
  author: string;
}

@InputType()

Decorated @InputType() classes are to be used where @Arg() is used.

import { InputType, Field, Types } from 'graphql-party';

@InputType()
class MessageInput {
  @Field(Types.NonNullable(Types.String))
  content: string;

  @Field(Types.NonNullable(Types.String))
  author: string;
}

@Query() and @Mutation

Class methods with @Query() and @Mutation() get combined into a Query or Mutation object type. The can live anywhere. Here is an example of one living inside of a service class.

import { Query, Mutation, Arg, Types, setInstance } from 'graphql-party';
import { Author } from '../models';
import { AuthorRepository } from '../repositories';
import { AuthorInput } from '../inputs';

class AuthorService {
  constructor(private authorRepository: AuthorRepository) {}

  @Query(Types.List(Author))
  async authors(): Promise<Author> {
    return await this.authorRepository.findAll();
  }

  @Mutation(Types.List(Author))
  async createAuthor(
    @Arg('input', AuthorInput) input: AuthorInput
  ): Promise<Author> {
    return await this.authorRepository.create(input);
  }
}

// Since AuthorService requires an author repository, it needs to be instantiated and set.
setInstance(AuthorService, new AuthorService(AuthorRepository.create());

All Field Types

@ObjectType()
class SomeType {
  @Field(Types.ID) id?: string;

  @Field(Types.Int) someInt?: number;

  @Field(Types.Float) someFloat?: number;

  @Field(Types.String) someString?: string;

  @Field(Types.Boolean) someBoolean?: boolean;

  @Field(SomeType) nestedType?: SomeType;

  @Field(SomeOtherType) someOtherType?: SomeOtherType;

  @Field(Types.NonNullable(Types.String))
  nonNullable: string;

  @Field(Types.NonNullable(Types.String))
  nonNullable: string;

  @Field(Types.List(Types.String))
  list?: string[];

  @Field(Types.NonNullable(Types.List(Types.String)))
  list: string[];

  // You can even provide a custom GraphQLScalarType
  @Field(GraphQLDateTime) dateTime: Date;
}

Adding resolvers to properties

Classes act as a simple class passed directly to GraphQL (see http://graphql.org/graphql-js/object-types/. So you can make a @Field() a function:

@ObjectType()
class User {
  @Field(Types.NonNullable(Types.String))
  firstName: string;

  @Field(Types.NonNullable(Types.String))
  lastName: string;

  @Field(Types.List(Types.ID))
  friends?: string[];

  @Field(Types.String)
  fullName(): string {
    return `${this.firstName} ${this.lastName}`;
  }
}

But if you have more complex logic, you can put it elsewhere, such as a service:

class UserService {
  constructor(private userRepository: UserRepository) {}

  @FieldResolver(User, Types.List(User))
  async friends(user: User): Promise<User> {
    return await this.userRepository.findFriendsForUser(user);
  }
}

Param Resolvers (@Arg() and @Context())

class SomeService {
  @Query(Types.Int)
  async rollDice(
    @Arg('times', Types.Int)
    times: number,
    @Context('roller') roller: Function
  ): Promise<User> {
    return roller(times);
  }
}

Check out examples for more examples!