All Projects → nicolasgere → graphql-ts

nicolasgere / graphql-ts

Licence: MIT license
Graphql implementation in Typescript using decorator

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to graphql-ts

Kaop Ts
Simple Yet Powerful Library of ES2016 Decorators with Strongly typed method Interceptors like BeforeMethod, AfterMethod, OnException, etc
Stars: ✭ 235 (+273.02%)
Mutual labels:  metadata, decorators
Reflection
Lightweight (3K) ES Module implementation of reflect-metadata
Stars: ✭ 136 (+115.87%)
Mutual labels:  metadata, decorators
IFIscripts
Detailed documentation is available here: http://ifiscripts.readthedocs.io/en/latest/index.html
Stars: ✭ 46 (-26.98%)
Mutual labels:  metadata
vue3-oop
使用类和依赖注入写vue组件
Stars: ✭ 90 (+42.86%)
Mutual labels:  decorators
dirdf
R package: dirdf - Extracts Metadata from Directory and File Names
Stars: ✭ 57 (-9.52%)
Mutual labels:  metadata
metadata-tools
Contains tools for metadata, such as Roslyn's metadata visualizer.
Stars: ✭ 37 (-41.27%)
Mutual labels:  metadata
stupid-python-tricks
Stupid Python tricks.
Stars: ✭ 112 (+77.78%)
Mutual labels:  decorators
metadata-one-liners
retrive metadata endpoint data with these one liners.
Stars: ✭ 38 (-39.68%)
Mutual labels:  metadata
oge
Page metadata as a service
Stars: ✭ 22 (-65.08%)
Mutual labels:  metadata
pyexiv2
Read/Write metadata(including EXIF, IPTC, XMP), comment and ICC Profile embedded in digital images.
Stars: ✭ 120 (+90.48%)
Mutual labels:  metadata
nts
NTS Radio downloader and metadata parser
Stars: ✭ 58 (-7.94%)
Mutual labels:  metadata
MetaCPP
C++ Reflection & Serialization using Clang's LibTooling
Stars: ✭ 44 (-30.16%)
Mutual labels:  metadata
js-coalaip
Javascript implementation for COALA IP
Stars: ✭ 18 (-71.43%)
Mutual labels:  metadata
icc
JavaScript module to parse International Color Consortium (ICC) profiles
Stars: ✭ 37 (-41.27%)
Mutual labels:  metadata
conp-dataset
📂 A DataLad dataset for CONP
Stars: ✭ 17 (-73.02%)
Mutual labels:  metadata
ome-types
native Python dataclasses for the OME data model
Stars: ✭ 28 (-55.56%)
Mutual labels:  metadata
metad
Metad is a metadata server, support self semantic.
Stars: ✭ 77 (+22.22%)
Mutual labels:  metadata
roda-in
Tool to create Submission Information Packages (SIP)
Stars: ✭ 18 (-71.43%)
Mutual labels:  metadata
metadata-xml-tool
CLI tool for processing Salesforce Metadata XML files
Stars: ✭ 14 (-77.78%)
Mutual labels:  metadata
Crema
Meta data server & client tools for game development
Stars: ✭ 61 (-3.17%)
Mutual labels:  metadata

This project is not maintained anymore

Due to the lack of time. Sorry for that

If you are looking for alternatives, check out the awesome-graphql list, where you can find similar libraries like typegql, TypeGraphQL or graphql-schema-decorator .

GraphQL.ts

Build Status npm version

The Typescrit implementation for GraphQL, a query language for APIs created by Facebook. See specification here http://graphql.org/

Getting Started

That package is currently in development and not ready for PRODUCTION. Graphql.ts use decorator and metadata for generate a graphql.js model. The why of this package is to provide a suger syntax for Typescript and use the power of the typings. Feel free to contribute, any issues, pull request or stars are welcome.

Using GraphQL.ts

Install GraphQL.ts from npm

npm install --save graphql-ts

We use reflect-metadata for have type at the runtime, so we need to pass some parameters to the compilator

"compilerOptions": {
       "module": "commonjs",
       "target": "es6",
       "emitDecoratorMetadata": true,
       "experimentalDecorators": true,
   }

GraphQL.ts provides the capabilities to build the schema. This schema will be interprated by GraphQL.js

First, build a GraphQL type schema which maps to your code base.

import {field, objectType} from 'graphql-ts'

@objectType
class root{
  @field
  hello():string{
    return "world"
  }
}

//That is the entry point of the schema
graphqlTs.init(new root());

This code will generate at the runtime the equivalent model

import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString
} from 'graphql';

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world';
        }
      }
    }
  })
});

Then, serve the result of a query against that schema.

import {graphqlTs, field, objectType} from 'graphql-ts'


@objectType
class root{
  @field
  hello():string{
   return "world"
  }
}
graphqlTs.init(new root());

var queryString = '{ hello }';
graphqlTs.query(queryString).then(result => {

  // Prints
  // {
  //   data: { hello: "world" }
  // }
  console.log(result);

});

Decorator

Graphql-ts work with decorator for annotate the code and then generate the model

  • @objectType create an object type with the class name as name
@objectType
class user{
  @field
  name:string
}
  • @inputType create an input object type with the class name as name
@inputType
class userInput{
    @field
    name:string
}
  • @scalarType create a scalar type, for more information about the scalar in graphql check here
@scalarType
export class DateQl {
    @field
    serialize(value: any) {
        //you're code here
    };
    @field
    parseValue(value: any) {
        //you're code here
    }
    @field
    parseLiteral(valueNode: any): any {
        //you're code here
    }
  • @field add the field in the model, if it's a function, it will be use as resolve. In the resolve, 'this' will be the equivalent of _ in graphql, and the context will be in this.contextQl
@objectType
class user{
  @field
  name:string

  @field
  name:string

  @field
  fullName():string{
    console.log(this.contextQl) //value of the context, by default req
    return this.firstName + ' ' + this.lastName
  }
}
  • @description(name:string) add a description to the field or the object
@objectType
class user{
  @field  @description('The name of the user')
  name:string
}
  • @list same as field but return a list, for more information about the list in graphql check here
  • @returnType(Type) Cause of lack in typescript about emit decorator for complexe object, when we returne an object, Array for exemple, we are not able to have the T type, so that's why we need to specify that T using the @returnType
@objectType
class user{
  @field  @description('The name of the user')
  name:string

  @list @returnType(Number)
  notes:number[]

  @list @returnType(user)
  friends():user[]{
    return dataUsers({friends:this.name});
   }
}
  • @required(['paramName']) set a params as required
@objectType @description('voila enfin le root')
export class root {
    @field @returnType(user) @required(['firstName'])
    user(firstName:string):user{
        return dataUsers({name:firstName}).firstOrDefault();
    }
}
  • @nullable(boolean) set a field or input nullable or not, by default is true
@inputType
export class userInput{
  @field //nullable is true by default
  firstName:string

  @field @nullable(false)
  lastName:string

  @list @returnType(String) @nullable(false)
  friends:string[]
}
  • @mutation create a mutation, see here for more information
   @mutation
    addUser(userInput:userInput):user{
      let newUser = new user();
      dataUsers().push(<any>newUser);
      return  newUser;
    }

###More complex exemple

For more complexe case, check the exemple folder.

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].