-
-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): add examples for generic types
- Loading branch information
1 parent
7a1c603
commit bb897b6
Showing
10 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
} |