在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Quramy/graphql-decorator开源软件地址(OpenSource Url):https://github.com/Quramy/graphql-decorator开源编程语言(OpenSource Language):TypeScript 99.3%开源软件介绍(OpenSource Introduction):This is not maintained. See issue/#3. graphql-decoratorHelps to build GraphQL schema with TypeScript. It provide the following features:
Getting startedThis tool requires Node.js v4.4.0 or later. npm i graphql-decorator typescript 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! GuideSchemaYou can declare a GraphQL schema class with 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 The field annotated by Object TypeYou can annotate your class with For example: @ObjectType()
class SomeObject {
@Field() title: string;
@Field() greeting(): string {
return "Hello";
}
} The above example has 2 fields, the one is You can set the Type of fieldBy the default, You can explicitly configure the type of the fields using @ObjectType() class User {
@Field() name: string;
}
@ObjectType() class SomeObject {
@Field({type: User}) user: User;
} NonNull, ListYou can use @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 */
}
} Resolver's argumentsYou can use @ObjectType()
class MutationType {
@Field({type: User}) deleteUser(
@Arg({name: "id"}) id: string
) {
/* implementation for delete user */
}
} And you can declare GraphQL InputObjectType with @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 */
}
} API UsageT.B.D. ExamplesPlease checkout exmaples folder in this repository. LicenseThis software is released under the MIT License, see LICENSE.txt. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论