Skip to content

Commit 40abf8e

Browse files
committed
refactor: depracate decorators with GraphQL prefix
1 parent 2f8750e commit 40abf8e

71 files changed

Lines changed: 276 additions & 270 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- add base support for GraphQL enums using TypeScript enums
66
- add support for defining GraphQL unions
77
- add support for importing resolvers from file path glob
8+
- depracate decorators with `GraphQL` prefix - use `@ArgsType`, `@InputType`, `@InterfaceType`, `@ObjectType` and `@Resolver` instead
89
### Fixes
910
- fix not working array type notation in circular dependencies (correct thunk generation)
1011

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ We have API for cooking recipes and we love using GraphQL for it.
2222
At first we will create the `Recipe` type, which is the foundations of our API:
2323

2424
```ts
25-
@GraphQLObjectType()
25+
@ObjectType()
2626
class Recipe {
2727
@Field(type => ID)
2828
readonly id: string;
@@ -42,7 +42,7 @@ class Recipe {
4242
```
4343
Take a look at the decorators:
4444

45-
- `@GraphQLObjectType()` marks the class as the object shape known from GraphQL SDL as `type`
45+
- `@ObjectType()` marks the class as the object shape known from GraphQL SDL as `type`
4646
- `@Field()` marks the property as the object's field - it is also used to collect type metadata from TypeScript reflection system
4747
- the parameter function in decorator `@Field(type => ID)` is used to declare the GraphQL scalar type like the builit-in `ID`
4848
- due to reflection limitation, optional (nullable) fields has to be annotated with `{ nullable: true }` decorator param
@@ -62,7 +62,7 @@ type Recipe {
6262
Next, we need to define what is the `Rate` type:
6363

6464
```ts
65-
@GraphQLObjectType()
65+
@ObjectType()
6666
class Rate {
6767
@Field(type => Int)
6868
value: number;
@@ -82,16 +82,16 @@ So, as we have the base of our recipe related types, let's create a resolver!
8282

8383
We will start by creating a class with apropiate decorator:
8484
```ts
85-
@GraphQLResolver(objectType => Recipe)
85+
@Resolver(objectType => Recipe)
8686
export class RecipeResolver {
8787
// we will implement this later
8888
}
8989
```
90-
`@GraphQLResolver` marks our class as a resolver of type `Recipe` (type info is needed for attaching field resolver to correct type).
90+
`@Resolver` marks our class as a resolver of type `Recipe` (type info is needed for attaching field resolver to correct type).
9191

9292
Now let's create our first query:
9393
```ts
94-
@GraphQLResolver(objectType => Recipe)
94+
@Resolver(objectType => Recipe)
9595
export class RecipeResolver {
9696
constructor(
9797
// declare to inject instance of our repository
@@ -110,7 +110,7 @@ export class RecipeResolver {
110110
111111
So, how the `FindRecipeArgs` looks like?
112112
```ts
113-
@GraphQLArgsType()
113+
@ArgsType()
114114
class FindRecipeArgs {
115115
@Field(type => ID)
116116
recipeId: string;
@@ -167,7 +167,7 @@ class RecipeResolver {
167167
168168
Here's how `RateInput` type looks:
169169
```ts
170-
@GraphQLInputType()
170+
@InputType()
171171
class RateInput {
172172
@Field(type => ID)
173173
recipeId: string;
@@ -176,7 +176,7 @@ class RateInput {
176176
value: number;
177177
}
178178
```
179-
`@GraphQLInputType()` marks the class as the `input` in SDL, in oposite to `type` or `scalar`
179+
`@InputType()` marks the class as the `input` in SDL, in oposite to `type` or `scalar`
180180
181181
The corresponding GraphQL schema:
182182
```graphql
@@ -209,7 +209,7 @@ class RecipeResolver {
209209
210210
The whole `RecipeResolver` we discussed above with sample implementation of methods looks like this:
211211
```ts
212-
@GraphQLResolver(objectType => Recipe)
212+
@Resolver(objectType => Recipe)
213213
export class RecipeResolver {
214214
constructor(
215215
// inject the repository (or other services)
@@ -269,7 +269,7 @@ So the GQL type classes would be also reused by ORM or validation lib:
269269
import { Entity, ObjectIdColumn, Column, OneToMany, CreateDateColumn } from "typeorm";
270270

271271
@Entity()
272-
@GraphQLObjectType()
272+
@ObjectType()
273273
export class Recipe {
274274
@ObjectIdColumn()
275275
@Field(type => ID)
@@ -300,7 +300,7 @@ export class Recipe {
300300
```ts
301301
import { IsMongoId, Min, Max } from "class-validator";
302302

303-
@GraphQLInputType()
303+
@InputType()
304304
class RateInput {
305305
@IsMongoId()
306306
@Field(type => ID)

docs/authorization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ And that's why authorization is a first-class feature in `TypeGraphQL`!
99
At first, you need to use `@Authorized` decorator as a guard on a field or a query/mutation.
1010
Example object type's fields guards:
1111
```ts
12-
@GraphQLObjectType()
12+
@ObjectType()
1313
class MyObject {
1414
@Field()
1515
publicField: string;
@@ -34,7 +34,7 @@ This way authed users (regardless of theirs roles) could read only `publicField`
3434

3535
Sample query and mutations guards:
3636
```ts
37-
@GraphQLResolver()
37+
@Resolver()
3838
class MyResolver {
3939
@Query()
4040
publicQuery(): MyObject {

docs/dependency-injection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Then, your resolvers will be able to declare their dependecies and TypeGraphQL w
2727
import { Service } from "typedi";
2828

2929
@Service()
30-
@GraphQLResolver(objectType => Recipe)
30+
@Resolver(objectType => Recipe)
3131
export class RecipeResolver {
3232
constructor(
3333
// constructor injection of a service

docs/enums.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ registerEnum(Direction, {
3636

3737
The last step is very important: TypeScript has limited reflection ability, so we have to explicitly provide the enum type both for object/input type fields as well as return type of queries/mutations or arg type:
3838
```ts
39-
@GraphQLInputType()
39+
@InputType()
4040
class JourneyInput {
4141
@Field(type => Direction) // it's very important
4242
direction: Direction;

docs/interfaces-and-inheritance.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ TypeScript has first class support for interfaces. Unfortunatelly, they exist on
99

1010
Luckily, we can use abstract class for this purpose - it behave almost like an interface (can't be "newed", can be implemented by class), it just won't stop developers from implementing a method or initializing a field. But until we do the same things like with an interface, we can safely use it.
1111

12-
So, how to create GraphQL interface definition? We create an abstract class and decorate it with `@GraphQLInterfaceType()`. The rest is exactly the same as with object types - we use `@Field` to declare the shape of the type:
12+
So, how to create GraphQL interface definition? We create an abstract class and decorate it with `@InterfaceType()`. The rest is exactly the same as with object types - we use `@Field` to declare the shape of the type:
1313

1414
```ts
15-
@GraphQLInterfaceType()
15+
@InterfaceType()
1616
abstract class IPerson {
1717
@Field(type => ID)
1818
id: string;
@@ -28,7 +28,7 @@ abstract class IPerson {
2828
Then we can use this "interface" in object type class definition:
2929

3030
```ts
31-
@GraphQLObjectType({ implements: IPerson })
31+
@ObjectType({ implements: IPerson })
3232
class Person implements IPerson {
3333
id: string;
3434
name: string;
@@ -47,7 +47,7 @@ One of the most known principles of software development is DRY - don't repeat y
4747

4848
While creating GraphQL API, it's a common pattern to have pagination args in resolvers, like `skip` and `take`. So instead of repeating yourself, you can declare it once:
4949
```ts
50-
@GraphQLArgsType()
50+
@ArgsType()
5151
class PaginationArgs {
5252
@Field(type => Int, { nullable: true })
5353
skip: number = 0;
@@ -59,7 +59,7 @@ class PaginationArgs {
5959

6060
and then reuse it everywhere:
6161
```ts
62-
@GraphQLArgsType()
62+
@ArgsType()
6363
class GetTodosArgs extends PaginationArgs {
6464
@Field({ nullable: false })
6565
onlyCompleted: boolean = false;
@@ -69,7 +69,7 @@ class GetTodosArgs extends PaginationArgs {
6969
This technique also works with input type classes, as well as with object type classes:
7070
```ts
7171
// `Person` is the object type class we've created earlier in this docs
72-
@GraphQLObjectType()
72+
@ObjectType()
7373
class Student extends Person {
7474
@Field()
7575
universityName: string;

docs/scalars.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This shorthand allows you to save keystrokes when declaring field type:
1111
// import the aliases
1212
import { ID, Float, Int } from "type-graphql";
1313

14-
@GraphQLObjectType()
14+
@ObjectType()
1515
class SampleObject {
1616
@Field(type => ID)
1717
readonly id: string;
@@ -27,7 +27,7 @@ In the last case you can ommit the `type => Float` since JavaScript `Number` wil
2727

2828
Other scalars - `GraphQLString` and `GraphQLBoolean` doesn't need aliases - when it's possible, they will be reflected automatically:
2929
```ts
30-
@GraphQLObjectType()
30+
@ObjectType()
3131
class User {
3232
@Field()
3333
name: string;
@@ -39,7 +39,7 @@ class User {
3939

4040
However in some cases you will have to explicitly declare the string/bool scalar type. Use JS constructor functions (`String`, `Boolean`) then:
4141
```ts
42-
@GraphQLObjectType()
42+
@ObjectType()
4343
class SampleObject {
4444
@Field(type => String, { nullable: true })
4545
get optionalInfo(): string | undefined { // TS reflected type is `Object` :(
@@ -69,7 +69,7 @@ const schema = await buildSchema({
6969

7070
There's no need to explicitly declare the field type then:
7171
```ts
72-
@GraphQLObjectType()
72+
@ObjectType()
7373
class User {
7474
@Field()
7575
registrationDate: Date;
@@ -108,7 +108,7 @@ Then you can just use it in your field decorators:
108108
// import the earlier created const
109109
import { ObjectIdScalar } from "../my-scalars/ObjectId";
110110

111-
@GraphQLObjectType()
111+
@ObjectType()
112112
class User {
113113
@Field(type => ObjectIdScalar) // and explicitly use it
114114
readonly id: ObjectId;
@@ -123,7 +123,7 @@ class User {
123123

124124
Optionally, you can delcare the association between reflected property type and your scalars to automatically map them (no need to explicit type annotation!):
125125
```ts
126-
@GraphQLObjectType()
126+
@ObjectType()
127127
class User {
128128
@Field() // magic goes here - no type annotation for custom scalar
129129
readonly id: ObjectId;

docs/unions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ You can read more about GraphQL union type in [official docs](http://graphql.org
77
Let's start by creating the object types from example above:
88

99
```ts
10-
@GraphQLObjectType()
10+
@ObjectType()
1111
class Movie {
1212
@Field()
1313
name: string;
@@ -17,7 +17,7 @@ class Movie {
1717
}
1818
```
1919
```ts
20-
@GraphQLObjectType()
20+
@ObjectType()
2121
class Actor {
2222
@Field()
2323
name: string;
@@ -41,7 +41,7 @@ All that left to do is to use the union type in the query.
4141
Notice, that due to TypeScript's reflection limitation, you have to explicitly use `SearchResultUnion` value in `@Query` decorator return type annotation.
4242
For TS compile-time type safety you can also use `typeof SearchResultUnion` which is equal to type `Movie | Actor`.
4343
```ts
44-
@GraphQLResolver()
44+
@Resolver()
4545
class SearchResolver {
4646
@Query(returnType => [SearchResultUnion])
4747
async search(

docs/validation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ And that's why TypeGraphQL has bulit-in support for validation of arguments and
88
## How to use
99
At first, you have to decorate the input/arguments class with appropiate decorators from `class-validator`. So we take this:
1010
```ts
11-
@GraphQLInputType()
11+
@InputType()
1212
export class RecipeInput {
1313
@Field()
1414
title: string;
@@ -21,7 +21,7 @@ and produce this:
2121
```ts
2222
import { MaxLength, Length } from "class-validator";
2323

24-
@GraphQLInputType()
24+
@InputType()
2525
export class RecipeInput {
2626
@Field()
2727
@MaxLength(30)
@@ -36,7 +36,7 @@ And that's it! :wink:
3636

3737
TypeGraphQL will automatically validate your inputs and arguments based on the definitions:
3838
```ts
39-
@GraphQLResolver(objectType => Recipe)
39+
@Resolver(objectType => Recipe)
4040
export class RecipeResolver {
4141
@Mutation(() => Recipe)
4242
async addRecipe(@Arg("input") recipeInput: RecipeInput): Promise<Recipe> {

examples/authorization/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import * as express from "express";
33
import * as graphqlHTTP from "express-graphql";
44
import { buildSchema } from "../../src";
55

6-
import { Resolver } from "./resolver";
6+
import { ExampleResolver } from "./resolver";
77
import { Context } from "./context.interface";
88
import { authChecker } from "./auth-checker";
99

1010
void async function bootstrap() {
1111
// build TypeGraphQL executable schema
1212
const schema = await buildSchema({
13-
resolvers: [Resolver],
13+
resolvers: [ExampleResolver],
1414
authChecker, // register auth checking function
1515
});
1616

0 commit comments

Comments
 (0)