Giter Club home page Giter Club logo

graphql-party's Introduction

NO LONGER IN DEVELOPMENT: type-graphql (boring name) beat me to the punch.

๐ŸŽ‰๐ŸŽŠ graphql-party

A @decorator based GraphQL.js schema builder.

Travis Travis

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!

graphql-party's People

Contributors

j avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

graphql-party's Issues

non

Je ne peux pas ce soir

1.0.0-alpha checklist โœ…

Here are some things to work on before an initial beta release. Most features have been already coded in a project I made for fun, so it's a matter of cleaning them up and writing tests. Boy, it's been fun!

  • Add @ObjectType decorator.
  • Add @Field decorator.
  • Add @FieldResolver decorator so resolver functions can live in other places besides @ObjectType()
  • Add @InputType decorator.
  • Add @Query decorator for creating Query object types.
    • Implement
    • Schema creation tests
    • Test resolver functions
  • Add @Mutation decorator for creating Mutation object types.
    • Implement
    • Schema creation tests
    • Test resolver functions
  • Add @Context parameter decorator for field resolvers.
    • Implement
    • Create examples
    • Complete integration tests
  • Add @Arg parameter decorator for field resolvers.
    • Implement
    • Create examples
    • Complete integration tests
  • Add @Obj parameter decorator for field resolvers. (will do only if requested, you can until access if you don't use param decorators)
  • Add @Info parameter decorator for field resolvers. (will do only if requested, you can until access if you don't use param decorators)
  • Add tests for examples
  • Publish!

Non

Je ne peux pas ce soir

Non

Je ne peux pas ce soir

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.