All Projects → scon-io → Graphql Typescript

scon-io / Graphql Typescript

Licence: mit
Define and build GraphQL Schemas using typed classes

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Graphql Typescript

Type Graphql
Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
Stars: ✭ 6,864 (+10144.78%)
Mutual labels:  graphql, graphql-schema, decorators
Typegql
Create GraphQL schema with TypeScript classes.
Stars: ✭ 415 (+519.4%)
Mutual labels:  graphql, graphql-schema, decorators
Graphql Cost Analysis
A Graphql query cost analyzer.
Stars: ✭ 527 (+686.57%)
Mutual labels:  graphql, graphql-schema
React Decoration
A collection of decorators for React Components
Stars: ✭ 641 (+856.72%)
Mutual labels:  decorators, annotations
Graphql
Pure PHP realization of GraphQL protocol
Stars: ✭ 717 (+970.15%)
Mutual labels:  graphql, graphql-schema
Graphql Auto Generating Cms
Use your existing graphQL schema to generate CMS in a couple steps. DEMO: http://cms-demo.web4fly.com/
Stars: ✭ 373 (+456.72%)
Mutual labels:  graphql, graphql-schema
Graphql Tools
🔧 Build, mock, and stitch a GraphQL schema using the schema language
Stars: ✭ 4,556 (+6700%)
Mutual labels:  graphql, graphql-schema
Graphqldesigner.com
A developer web-app tool to rapidly prototype a full stack implementation of GraphQL with React.
Stars: ✭ 587 (+776.12%)
Mutual labels:  graphql, graphql-schema
Strawberry
A new GraphQL library for Python 🍓
Stars: ✭ 891 (+1229.85%)
Mutual labels:  graphql, graphql-schema
Graphql Config
One configuration for all your GraphQL tools (supported by most tools, editors & IDEs)
Stars: ✭ 883 (+1217.91%)
Mutual labels:  graphql, graphql-schema
Fullstack Graphql
🌈 Simple Fullstack GraphQL Application. API built with Express + GraphQL + Sequelize (supports MySQL, Postgres, Sqlite and MSSQL). WebApp built with React + Redux to access the API. Written in ES6 using Babel + Webpack.
Stars: ✭ 955 (+1325.37%)
Mutual labels:  graphql, graphql-schema
Graphqlite
Use PHP Annotations to declare your GraphQL API
Stars: ✭ 370 (+452.24%)
Mutual labels:  graphql, annotations
React Apollo Decorators
Better decorators for Apollo and React
Stars: ✭ 39 (-41.79%)
Mutual labels:  graphql, decorators
Get Graphql Schema
Fetch and print the GraphQL schema from a GraphQL HTTP endpoint. (Can be used for Relay Modern.)
Stars: ✭ 443 (+561.19%)
Mutual labels:  graphql, graphql-schema
Vim Graphql
A Vim plugin that provides GraphQL file detection, syntax highlighting, and indentation.
Stars: ✭ 357 (+432.84%)
Mutual labels:  graphql, graphql-schema
Warthog
GraphQL API Framework with strong conventions and auto-generated schema
Stars: ✭ 316 (+371.64%)
Mutual labels:  graphql, decorators
graphql-metadata
Annotate your graphql schema with lightweight directives
Stars: ✭ 28 (-58.21%)
Mutual labels:  annotations, graphql-schema
soap-typescript
SOAP decorators for creating wsdl's and annotating services to provide metadata for node-soap
Stars: ✭ 20 (-70.15%)
Mutual labels:  decorators, annotations
Graphql Code Generator
A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
Stars: ✭ 7,993 (+11829.85%)
Mutual labels:  graphql, graphql-schema
Sql To Graphql Schema Generator
⚛️ Generate GraphQL Scheme Online From SQL Query - https://sql-to-graphql.now.sh/
Stars: ✭ 32 (-52.24%)
Mutual labels:  graphql, graphql-schema

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].