-
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abhik Mitra
authored and
Abhik Mitra
committed
Sep 2, 2018
1 parent
5e2bcf5
commit ac65c7a
Showing
17 changed files
with
312 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
declare module "graphql-query-complexity" { | ||
import {QueryComplexityOptions} from 'graphql-query-complexity/dist/QueryComplexity'; | ||
import QueryComplexity from 'graphql-query-complexity/dist/QueryComplexity'; | ||
import { ValidationContext } from 'graphql'; | ||
type returnType = (context: ValidationContext) => QueryComplexity ; | ||
export default function createQueryComplexityValidator(options: QueryComplexityOptions): returnType; | ||
} | ||
declare module "graphql-query-complexity/dist/QueryComplexity" { | ||
import { ValidationContext, FragmentDefinitionNode, OperationDefinitionNode, FieldNode, InlineFragmentNode } from 'graphql'; | ||
import { GraphQLUnionType, GraphQLObjectType, GraphQLInterfaceType, GraphQLError } from 'graphql'; | ||
export interface QueryComplexityOptions { | ||
maximumComplexity: number; | ||
variables?: Object; | ||
onComplete?: (complexity: number) => void; | ||
createError?: (max: number, actual: number) => GraphQLError; | ||
} | ||
export default class QueryComplexity { | ||
context: ValidationContext; | ||
complexity: number; | ||
options: QueryComplexityOptions; | ||
fragments: { | ||
[name: string]: FragmentDefinitionNode; | ||
}; | ||
OperationDefinition: Object; | ||
constructor(context: ValidationContext, options: QueryComplexityOptions); | ||
onOperationDefinitionEnter(operation: OperationDefinitionNode): void; | ||
onOperationDefinitionLeave(): GraphQLError | undefined; | ||
nodeComplexity(node: FieldNode | FragmentDefinitionNode | InlineFragmentNode | OperationDefinitionNode, typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType, complexity?: number): number; | ||
createError(): GraphQLError; | ||
getDefaultComplexity(args: Object, childScore: number): 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,15 @@ | ||
|
||
query GetRecipesWithComplexityError { | ||
recipes { | ||
title | ||
averageRating, | ||
ratingsCount | ||
} | ||
} | ||
|
||
mutation GetRecipesWithoutComplexityError { | ||
recipes { | ||
title | ||
averageRating | ||
} | ||
} |
Empty file.
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,46 @@ | ||
import "reflect-metadata"; | ||
import { GraphQLServer, Options } from "graphql-yoga"; | ||
import { buildSchema } from "../../src"; | ||
import queryComplexity from "graphql-query-complexity"; | ||
import { RecipeResolver } from "./recipe-resolver"; | ||
|
||
async function bootstrap() { | ||
// build TypeGraphQL executable schema | ||
const schema = await buildSchema({ | ||
resolvers: [RecipeResolver], | ||
}); | ||
|
||
// Create GraphQL server | ||
const server = new GraphQLServer({ schema }); | ||
|
||
// Configure server options | ||
const serverOptions: Options = { | ||
port: 4000, | ||
endpoint: "/graphql", | ||
playground: "/playground", | ||
validationRules: req => [ | ||
/** | ||
* This provides GraphQL query analysis to reject complex queries to your GraphQL server. | ||
* This can be used to protect your GraphQL servers | ||
* against resource exhaustion and DoS attacks. | ||
* More doucmentation can be found (here)[https://github.com/ivome/graphql-query-complexity] | ||
*/ | ||
queryComplexity({ | ||
maximumComplexity: 8, | ||
variables: req.query.variables, | ||
onComplete: (complexity: number) => { | ||
console.log("Query Complexity:", complexity); | ||
}, | ||
}), | ||
], | ||
}; | ||
|
||
// Start the server | ||
server.start(serverOptions, ({ port, playground }) => { | ||
console.log( | ||
`Server is running, GraphQL Playground available at http://localhost:${port}${playground}`, | ||
); | ||
}); | ||
} | ||
|
||
bootstrap(); |
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,11 @@ | ||
import { Recipe } from "./recipe-type"; | ||
import { InputType, Field } from "../../src"; | ||
|
||
@InputType() | ||
export class RecipeInput implements Partial<Recipe> { | ||
@Field() | ||
title: string; | ||
|
||
@Field({ nullable: true }) | ||
description?: string; | ||
} |
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,51 @@ | ||
import { | ||
Resolver, | ||
Query, | ||
FieldResolver, | ||
Arg, | ||
Root, | ||
Mutation, | ||
Float, | ||
Int, | ||
ResolverInterface, | ||
} from "../../src"; | ||
import { plainToClass } from "class-transformer"; | ||
|
||
import { Recipe } from "./recipe-type"; | ||
import { RecipeInput } from "./recipe-input"; | ||
import { createRecipeSamples } from "./recipe-samples"; | ||
|
||
@Resolver(of => Recipe) | ||
export class RecipeResolver implements ResolverInterface<Recipe> { | ||
private readonly items: Recipe[] = createRecipeSamples(); | ||
|
||
@Query(returns => Recipe, { nullable: true }) | ||
async recipe(@Arg("title") title: string): Promise<Recipe | undefined> { | ||
return await this.items.find(recipe => recipe.title === title); | ||
} | ||
|
||
@Query(returns => [Recipe], { description: "Get all the recipes from around the world " }) | ||
async recipes(): Promise<Recipe[]> { | ||
return await this.items; | ||
} | ||
|
||
@Mutation(returns => Recipe, { complexity: 10 }) | ||
async addRecipe(@Arg("recipe") recipeInput: RecipeInput): Promise<Recipe> { | ||
const recipe = plainToClass(Recipe, { | ||
description: recipeInput.description, | ||
title: recipeInput.title, | ||
ratings: [], | ||
creationDate: new Date(), | ||
}); | ||
await this.items.push(recipe); | ||
return recipe; | ||
} | ||
|
||
@FieldResolver() | ||
ratingsCount( | ||
@Root() recipe: Recipe, | ||
@Arg("minRate", type => Int, { nullable: true }) minRate: number = 0.0, | ||
): number { | ||
return recipe.ratings.filter(rating => rating >= minRate).length; | ||
} | ||
} |
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,26 @@ | ||
import { plainToClass } from "class-transformer"; | ||
|
||
import { Recipe } from "./recipe-type"; | ||
|
||
export function createRecipeSamples() { | ||
return plainToClass(Recipe, [ | ||
{ | ||
description: "Desc 1", | ||
title: "Recipe 1", | ||
ratings: [0, 3, 1], | ||
creationDate: new Date("2018-04-11"), | ||
}, | ||
{ | ||
description: "Desc 2", | ||
title: "Recipe 2", | ||
ratings: [4, 2, 3, 1], | ||
creationDate: new Date("2018-04-15"), | ||
}, | ||
{ | ||
description: "Desc 3", | ||
title: "Recipe 3", | ||
ratings: [5, 4], | ||
creationDate: new Date(), | ||
}, | ||
]); | ||
} |
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,49 @@ | ||
import { Field, ObjectType, Int, Float } from "../../src"; | ||
|
||
@ObjectType({ description: "Object representing cooking recipe" }) | ||
export class Recipe { | ||
/* | ||
By default, every field gets a complexity of 1. | ||
Which can be customized by passing the complexity parameter | ||
*/ | ||
@Field({ complexity: 6 }) | ||
title: string; | ||
|
||
@Field(type => String, { nullable: true, deprecationReason: "Use `description` field instead" }) | ||
get specification(): string | undefined { | ||
return this.description; | ||
} | ||
|
||
@Field({ complexity: 5, nullable: true, description: "The recipe" }) | ||
description?: string; | ||
|
||
@Field(type => [Int]) | ||
ratings: number[]; | ||
|
||
@Field() | ||
creationDate: Date; | ||
|
||
@Field(type => Int) | ||
ratingsCount: number; | ||
|
||
@Field(type => Float, { | ||
nullable: true, | ||
/* | ||
By default, every field gets a complexity of 1. | ||
You can also pass a calculation function in the complexity option | ||
to determine a custom complexity. | ||
This function will provide the complexity of | ||
the child nodes as well as the field input arguments. | ||
That way you can make a more realistic estimation of individual field complexity values: | ||
*/ | ||
complexity: (args, childComplexity) => childComplexity + 1, | ||
}) | ||
get averageRating(): number | null { | ||
const ratingsCount = this.ratings.length; | ||
if (ratingsCount === 0) { | ||
return null; | ||
} | ||
const ratingsSum = this.ratings.reduce((a, b) => a + b, 0); | ||
return ratingsSum / ratingsCount; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
import { GraphQLFieldConfig, GraphQLError } from "graphql"; | ||
|
||
export type ComplexityResolver = (args: any, complexity: number) => number; | ||
|
||
type ComplexityGraphQLFieldConfig<TSource, TContext> = GraphQLFieldConfig<TSource, TContext> & { | ||
complexity?: ComplexityResolver | number; | ||
}; | ||
|
||
export interface ComplexityGraphQLFieldConfigMap<TSource, TContext> { | ||
[key: string]: ComplexityGraphQLFieldConfig<TSource, TContext>; | ||
} |
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
Oops, something went wrong.