Skip to content

Commit

Permalink
docs(examples): add examples for generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Feb 18, 2019
1 parent 7a1c603 commit bb897b6
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 2 deletions.
1 change: 1 addition & 0 deletions dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require("ts-node/register/transpile-only");
// require("./examples/authorization/index.ts");
// require("./examples/automatic-validation/index.ts");
// require("./examples/enums-and-unions/index.ts");
// require("./examples/generic-types/index.ts");
// require("./examples/interfaces-inheritance/index.ts");
// require("./examples/middlewares/index.ts");
// require("./examples/redis-subscriptions/index.ts");
Expand Down
3 changes: 2 additions & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ All examples has a `examples.gql` file with sample queries/mutations/subscriptio

## Advanced
- [Enums and unions](https://github.com/19majkel94/type-graphql/tree/master/examples/enums-and-unions)
- [Interfaces and types inheritance](https://github.com/19majkel94/type-graphql/tree/master/examples/interfaces-inheritance)
- [Subscriptions (simple)](https://github.com/19majkel94/type-graphql/tree/master/examples/simple-subscriptions)
- [Subscriptions (using Redis)](https://github.com/19majkel94/type-graphql/tree/master/examples/redis-subscriptions)
- [Interfaces and types inheritance](https://github.com/19majkel94/type-graphql/tree/master/examples/interfaces-inheritance)
- [Resolvers inheritance](https://github.com/19majkel94/type-graphql/tree/master/examples/resolvers-inheritance)
- [Generic types](https://github.com/19majkel94/type-graphql/tree/master/examples/generic-types)

## Features usage
- [Dependency injection (IoC container)](https://github.com/19majkel94/type-graphql/tree/master/examples/using-container)
Expand Down
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ All examples has a `examples.gql` file with sample queries/mutations/subscriptio

## Advanced
- [Enums and unions](./enums-and-unions)
- [Interfaces and types inheritance](./interfaces-inheritance)
- [Subscriptions (simple)](./simple-subscriptions)
- [Subscriptions (using Redis)](./redis-subscriptions)
- [Interfaces and types inheritance](./interfaces-inheritance)
- [Resolvers inheritance](./resolvers-inheritance)
- [Generic types](./generic-types)

## Features usage
- [Dependency injection (IoC container)](./using-container)
Expand Down
16 changes: 16 additions & 0 deletions examples/generic-types/examples.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query GetRecipes {
recipes(first: 3) {
items {
title
ratings
}
total
hasMore
}
}

mutation AddRecipe {
addSampleRecipe {
title
}
}
23 changes: 23 additions & 0 deletions examples/generic-types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import "reflect-metadata";
import { ApolloServer } from "apollo-server";
import * as path from "path";
import { buildSchema } from "../../src";

import RecipeResolver from "./recipe.resolver";

async function bootstrap() {
const schema = await buildSchema({
resolvers: [RecipeResolver],
emitSchemaFile: path.resolve(__dirname, "schema.gql"),
});

const server = new ApolloServer({
schema,
playground: true,
});

const { url } = await server.listen(4000);
console.log(`Server is running, GraphQL Playground available at ${url}`);
}

bootstrap().catch(console.error);
17 changes: 17 additions & 0 deletions examples/generic-types/paginated-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ClassType, Field, ObjectType, Int } from "../../src";

export default function PaginatedResponse<TItem>(TItemClass: ClassType<TItem>) {
// `isAbstract` decorator option is mandatory to prevent registering in schema
@ObjectType({ isAbstract: true })
abstract class PaginatedResponseClass {
@Field(type => [TItemClass])
items: TItem[];

@Field(type => Int)
total: number;

@Field()
hasMore: boolean;
}
return PaginatedResponseClass;
}
45 changes: 45 additions & 0 deletions examples/generic-types/recipe.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ObjectType, Query, Mutation, Arg, Int, Resolver } from "../../src";

import PaginatedResponse from "./paginated-response.type";
import Recipe from "./recipe.type";
import createSampleRecipes from "./recipe.samples";

// we need to create a temporary class for the abstract, generic class "instance"
@ObjectType()
class RecipesResponse extends PaginatedResponse(Recipe) {
// simple helper for creating new instances easily
constructor(recipesResponse: RecipesResponse) {
super();
Object.assign(this, recipesResponse);
}

// you can add more fields here if you need
}

@Resolver()
export default class RecipeResolver {
private readonly recipes = createSampleRecipes();

@Query({ name: "recipes" })
getRecipes(
@Arg("first", type => Int, { nullable: true, defaultValue: 10 }) first: number,
): RecipesResponse {
const total = this.recipes.length;
return new RecipesResponse({
items: this.recipes.slice(0, first),
hasMore: total > first,
total,
});
}

@Mutation()
addSampleRecipe(): Recipe {
const recipe: Recipe = {
title: "Sample recipe",
description: "Sample description",
ratings: [1, 2, 3, 4],
};
this.recipes.push(recipe);
return recipe;
}
}
21 changes: 21 additions & 0 deletions examples/generic-types/recipe.samples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Recipe from "./recipe.type";

export default function createSampleRecipes(): Recipe[] {
return [
{
description: "Desc 1",
title: "Recipe 1",
ratings: [0, 3, 1],
},
{
description: "Desc 2",
title: "Recipe 2",
ratings: [4, 2, 3, 1],
},
{
description: "Desc 3",
title: "Recipe 3",
ratings: [5, 4],
},
];
}
13 changes: 13 additions & 0 deletions examples/generic-types/recipe.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Field, ObjectType, Int } from "../../src";

@ObjectType()
export default class Recipe {
@Field()
title: string;

@Field()
description?: string;

@Field(type => [Int])
ratings: number[];
}
24 changes: 24 additions & 0 deletions examples/generic-types/schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------

type Mutation {
addSampleRecipe: Recipe!
}

type Query {
recipes(first: Int = 10): RecipesResponse!
}

type Recipe {
title: String!
description: String!
ratings: [Int!]!
}

type RecipesResponse {
items: [Recipe!]!
total: Int!
hasMore: Boolean!
}

0 comments on commit bb897b6

Please sign in to comment.