Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
@anchan828/graphql-decorator
Advanced tools
Readme
Helps to build GraphQL schema with TypeScript.
It provide the following features:
@ObjectType
, @Schema
, @NonNull
, and more...) corresponding to GraphQL type system.This tool requires Node.js v4.4.0 or later.
npm i @anchan828/graphql-decorator
This tool uses ES.next Decorators and Reflect, so create tsconfig.json :
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": false,
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules"
]
}
And write .ts code such as:
/* main.ts */
import { Schema, Query, ObjectType, Field, schemaFactory } from "graphql-decorator";
const graphql = require("graphql").graphql;
// @ObjectType creates GraphQLObjectType from a class
@ObjectType()
class QueryType {
@Field() greeting(): string {
return "Hello, world!";
}
}
// @Schema creates GraphQLSchema from a class.
// The class should have a field annotated by @Query decorator.
@Schema()
class SchemaType {
@Query() query: QueryType;
}
async function main() {
// create schema from annotated class
const schema = schemaFactory(SchemaType);
const result = await graphql(schema, `query { greeting } `);
console.log(result.data.greeting);
}
main();
Finally, execute the above schema:
tsc main.ts && node main.js
# -> Hello, world!
You can declare a GraphQL schema class with @Schema
, @Query
and @Mutation
decorators.
For example:
import { Schema, Query, Mutation } from "graphql-decorator";
@Schema()
class MySchema {
@Query() query: RootQuery;
@Mutation() mutation: Mutations;
}
A schema class should a field annotated by @Query
, which represents that the type of this filed will be a root query of GraphQL. And the type of this field should be annotated by @ObjectType
.
The field annotated by @Mutation
also represents mutation of your GraphQL schema.
You can annotate your class with @ObjectType()
For example:
@ObjectType()
class SomeObject {
@Field() title: string;
@Field() greeting(): string {
return "Hello";
}
}
The above example has 2 fields, the one is title
and the another is greeting
.
You can set the @Field
decorator to your class's properties and methods. The fields annotated by @Field
will be exposed as fields of this object in GraphQL schema. And when you set @Field
to methods, the methods will work as the resolver function in schema.
By the default, @Field
detects GraphQLScalarType corresponding to the field type.
You can explicitly configure the type of the fields using type
option.
@ObjectType() class User {
@Field() name: string;
}
@ObjectType() class SomeObject {
@Field({type: User}) user: User;
}
You can use @NonNull
and @List
decorator. For example:
@ObjectType()
class User {
@NonNull() @Field({type: graphql.GraphQLID})
id: string;
}
@ObjectType()
class Query {
@List() @Field({type: User}) getAllUsers(): Promise<User[]> {
/* implementation for fetch all users */
}
}
You can use @Arg
for declare arguments of resolver function. For example:
@ObjectType()
class MutationType {
@Field({type: User}) deleteUser(
@Arg({name: "id"}) id: string
) {
/* implementation for delete user */
}
}
And you can declare GraphQL InputObjectType with @InputObjectType
decorator.
@InputObjectType()
class UserForUpdate {
@Field() name: string;
@Field() emal: string;
}
@ObjectType()
class MutationType {
@Field({type: User}) updateUser(
@Arg({name: "id"}) id: string,
@Arg({name: "input"}) input: UserForUpdate
) {
/* implementation for delete user */
}
}
T.B.D.
Please checkout exmaples folder in this repository.
This software is released under the MIT License, see LICENSE.txt.
FAQs
Creates GraphQL Schema from decorated TypeScript Class. [Note] This package forked from https://github.com/Quramy/graphql-decorator
We found that @anchan828/graphql-decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).