Giter VIP home page Giter VIP logo

graphql-typescript's Introduction

graphql-typescript npm npm CircleCI token Coveralls github dependencies Status

Define and build GraphQL Schemas using typed classes

import { Type, Field, Nullable, Mutation, String, Boolean, Int, makeSchema } from 'graphql-typescript'

@Type class Query {
  @Field(() => Box) box: Box
}

class UnboxArguments {
  @Field(() => [String]) tools: string[]
}

@Type class Box {
  @Field(() => Size)
  size: Size

  @Nullable
  @Field(() => String)
  content: string

  @Mutation(() => Boolean)
  unbox(box: BoxModel, args: UnboxArguments, context: any) { ... }
}

@Type class Size {
  @Field(() => Int) height: number
  @Field(() => Int) width: number
  @Field(() => Int) length: number
}


const schema = makeSchema(Query, {
  types: [Size, Box]
})
type Query {
  box: Box!
}

type Mutation {
  unbox(tools: [String]!): Boolean!
}

type Box {
  size: Size!
  content: String
}

type Size {
  height: Int!
  width: Int!
  length: Int!
}

Prerequisites

Set decorator flags in your tsconfig.json

"experimentalDecorators": true,
"emitDecoratorMetadata": true

Installing

npm install -S graphql
npm install -S graphql-typescript

Type Definition

@Type

Adding @Type to a class definition defines GraphQL object type.

@Type class Character {
  @Field(() => String) name: string
  @Field(() => [Episode]) appearsIn: Episode[]
}
type Character {
  name: String!
  appearsIn: [Episode]!
}

@Field

Adding @Field to properties or methods of a @Type decorated class defines what fields it has.

@Type
class Hello {
  @Field(() => String)
  a: string

  @Field(() => [String])
  b: string[]

  @Field(() => String)
  c(_:any, _args: any, context: any) { ... }
}
type Hello {
  a: String!
  b: [String]!
  c: String!
}

@Mutation

Adding @Mutation to methods of a @Type decorated class defines a mutation. No matter which class it is in, it will come under mutation type.

class Argument {
  @Field(() => [Int]) world: number[]
}

@Type
class Hello {
  @Mutation(() => String)
  a(_: any, _args: any, context: any) { ... }

  @Mutation(() => [String])
  b(_: any, _args: any, context: any) { ... }

  @Mutation(() => String)
  c(_: any, args: Argument, context: any) { ... }
}
type Mutation {
  ...
  a: String!
  b: [String]!
  c(world: [Int]!): String!
}

@Nullable

All fields and mutations are Non-null type by default. Adding `@Nullable to fields or mutations properties make it nullable.

@Type
class Hello {
  @Nullable
  @Field(() => String)
  hello: string
}
type Hello {
  hello: String
}

@Input

Adding @Input to a class definition defines a input type An input class can only have @Field properties.

@Input class AddCharacterInput {
  @Field(() => String) name: string
  @Field(() => Int) age: number
}
input AddCharacterInput {
  name: String!
  age: Int!
}

Scalar types

To use GraphQL default scalar types, import it from 'graphql-typescript'

import { String, Boolean, Int, Float, ID } from 'graphql-typescript'

Arguments

All fields of GraphQL objects type can have arguments. Methods with @Field or @Mutation get request query arguments from second parameter. It needs to define a argument class. Because a purpose of this class is only typing arguments, there is no class decorator and it can have only @Field properties.

class UnboxArguments {
  @Field(() => [String]) tools: string[]
}

@Type class Box {
  @Mutation(() => Boolean) unbox(box: BoxModel, args: UnboxArguments) { ... }
}
type Mutation{
  unbox(tools: [String]!): Boolean
}

To use input type in argument, do like below.

@Input class UnboxInput {
  @Field(() => [String]) tools: string[]
}
class UnboxArguments {
  @Field(() => UnboxInput) inputs: UnboxInput
}

@Type class Box {
  @Mutation(() => Boolean) unbox(box: BoxModel, args: UnboxArguments) { ... }
}
input UnboxInput {
  tools: [String]!
}

type Mutation{
  unbox(inputs: UnboxInput!): Boolean
}

Generating GraphQL Schema

import { makeSchema } from 'graphql-typescript'
import { Query } from './Query'
import { Box } from './Box'
import { Character } from './Character'

const schema = makeSchema(Query, {
  models: [ Box, Character ]
})

makeSchema

makeSchema(rootType, {
  types: [ ... ]
})
  • rootType: A root type of schema
  • types: Rest of types except a root type

graphql-typescript's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

capaj thaadur

graphql-typescript's Issues

non-optional array elements

First of all, I love this library.

I have a Field that is an array type, like this:

@Field([String] strings () { ... }

but the resulting type is:

type Query {
  strings: [String]!
}

whereas I want it to be:

type Query {
  strings: [String!]!
}

is there any way to do this? Or to just make array elements always non-null?

could it be possible to specify arguments only via an inline type?

like so I could write it:

@Type class Query {
  @Field test(_ : any, args : {
    tools: [String]
  }) : Boolean {
    console.log(args);
    return true;
  }
}

instead of

class TestArguments {
  @Field([String])tools : [String]
}

@Type class Query {
  @Field test(_ : any, args : TestArguments) : Boolean {
    return true;
  }
}

? Should be doable-we can just generate a name of that type from the @field

TypeError: Cannot read property 'name' of undefined

I wanted to try this out and with the code:

import {
  Type,
  Field,
  Mutation,
  String,
  Boolean,
  Int,
  makeSchema
} from 'graphql-typescript'

@Type class Query {
  constructor() {
    this.box = new Size()
  }
  @Field box : Size
}

@Type class Size {
  @Field height : Int = 0
  @Field width : Int = 0
  @Field length : Int = 0
}

const schema = makeSchema(Query, {types: [Size]})

console.log(schema)

I am getting this error:

TypeError: Cannot read property 'name' of undefined
    at Object.createProperty (/home/capaj/git_projects/graphql/gql-ts-sample-project/node_modules/graphql-typescript/src/services.ts:11:26)
    at Field (/home/capaj/git_projects/graphql/gql-ts-sample-project/node_modules/graphql-typescript/src/decorators/Field.ts:45:19)
    at DecorateProperty (/home/capaj/git_projects/graphql/gql-ts-sample-project/node_modules/reflect-metadata/Reflect.ts:1214:35)
    at Object.decorate (/home/capaj/git_projects/graphql/gql-ts-sample-project/node_modules/reflect-metadata/Reflect.ts:737:24)
    at __decorate (/home/capaj/git_projects/graphql/gql-ts-sample-project/server.ts:4:92)
    at /home/capaj/git_projects/graphql/gql-ts-sample-project/server.ts:15:10
    at Object.<anonymous> (/home/capaj/git_projects/graphql/gql-ts-sample-project/server.ts:11:7)
    at Module._compile (module.js:635:30)
    at Module.m._compile (/home/capaj/.nvm/versions/node/v8.9.3/lib/node_modules/ts-node/src/index.ts:403:23)
    at Module._extensions..js (module.js:646:10)

Cannot add a nullable mutation

@Nullable
@Mutation(User)
addUser(_: any, args: AddUserArguments) { ... }
TypeError: Cannot read property 'name' of undefined
    at Object.getFieldLiteral (/../node_modules/graphql-typescript/src/services.ts:132:9)

enums?

Any plans to support enums?

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.