https://github.com/nicolasgere/graphql-ts
Graphql implementation in Typescript using decorator
https://github.com/nicolasgere/graphql-ts
decorators graphql metadata typescript
Last synced: 6 months ago
JSON representation
Graphql implementation in Typescript using decorator
- Host: GitHub
- URL: https://github.com/nicolasgere/graphql-ts
- Owner: nicolasgere
- License: mit
- Created: 2017-01-26T05:55:27.000Z (over 9 years ago)
- Default Branch: dev
- Last Pushed: 2022-12-07T00:49:42.000Z (over 3 years ago)
- Last Synced: 2025-09-28T08:22:49.147Z (9 months ago)
- Topics: decorators, graphql, metadata, typescript
- Language: TypeScript
- Homepage:
- Size: 67.4 KB
- Stars: 64
- Watchers: 4
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/chentsulin/awesome-graphql#lib-ts), where you can find similar libraries like [typegql](https://github.com/prismake/typegql), [TypeGraphQL](https://19majkel94.github.io/type-graphql) or [graphql-schema-decorator](https://github.com/indigotech/graphql-schema-decorator)
.
# GraphQL.ts
[](https://travis-ci.org/nicolasgere/graphql-ts?branch=dev)
[](https://badge.fury.io/js/graphql-ts)
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
```sh
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
```js
"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.
```ts
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
```js
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.
```ts
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
```ts
@objectType
class user{
@field
name:string
}
```
+ @inputType create an input object type with the class name as name
```ts
@inputType
class userInput{
@field
name:string
}
```
+ @scalarType create a scalar type, for more information about the scalar in graphql check [here](http://graphql.org/graphql-js/type/#graphqlscalartype)
```ts
@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
```ts
@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
```ts
@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](http://graphql.org/graphql-js/type/#graphqllist)
+ @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
```ts
@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
```ts
@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
```ts
@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](http://graphql.org/graphql-js/mutations-and-input-types/) for more information
```ts
@mutation
addUser(userInput:userInput):user{
let newUser = new user();
dataUsers().push(newUser);
return newUser;
}
```
###More complex exemple
For more complexe case, check the [exemple](exemple/) folder.
```