Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
docs(examples): fix types issues in code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Aug 17, 2022
1 parent 480561d commit 2658acf
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 44 deletions.
4 changes: 3 additions & 1 deletion examples/mikro-orm/entities/rate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Property, ManyToOne, PrimaryKey } from "@mikro-orm/core";
import { Entity, Property, ManyToOne, PrimaryKey, OptionalProps } from "@mikro-orm/core";
import { ObjectType, Field, Int } from "../../../src";

import { User } from "./user";
Expand All @@ -24,4 +24,6 @@ export class Rate {

@ManyToOne(type => Recipe)
recipe: Recipe;

[OptionalProps]?: "date";
}
14 changes: 7 additions & 7 deletions examples/mikro-orm/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export async function seedDatabase(em: EntityManager) {
author: defaultUser,
});
recipe1.ratings.add(
em.create(Rate, { value: 2, user: defaultUser }),
em.create(Rate, { value: 4, user: defaultUser }),
em.create(Rate, { value: 5, user: defaultUser }),
em.create(Rate, { value: 3, user: defaultUser }),
em.create(Rate, { value: 4, user: defaultUser }),
em.create(Rate, { value: 2, user: defaultUser, recipe: recipe1 }),
em.create(Rate, { value: 4, user: defaultUser, recipe: recipe1 }),
em.create(Rate, { value: 5, user: defaultUser, recipe: recipe1 }),
em.create(Rate, { value: 3, user: defaultUser, recipe: recipe1 }),
em.create(Rate, { value: 4, user: defaultUser, recipe: recipe1 }),
);
em.persist(recipe1);

Expand All @@ -31,8 +31,8 @@ export async function seedDatabase(em: EntityManager) {
author: defaultUser,
});
recipe2.ratings.add(
em.create(Rate, { value: 2, user: defaultUser }),
em.create(Rate, { value: 4, user: defaultUser }),
em.create(Rate, { value: 2, user: defaultUser, recipe: recipe2 }),
em.create(Rate, { value: 4, user: defaultUser, recipe: recipe2 }),
);
em.persist(recipe2);

Expand Down
4 changes: 2 additions & 2 deletions examples/redis-subscriptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "reflect-metadata";
import { ApolloServer } from "apollo-server";
import Redis from "ioredis";
import Redis, { RedisOptions } from "ioredis";
import { RedisPubSub } from "graphql-redis-subscriptions";
import { buildSchema } from "../../src";

Expand All @@ -11,7 +11,7 @@ const REDIS_PORT = 6379;

async function bootstrap() {
// configure Redis connection options
const options: Redis.RedisOptions = {
const options: RedisOptions = {
host: REDIS_HOST,
port: REDIS_PORT,
retryStrategy: times => Math.max(times * 100, 3000),
Expand Down
4 changes: 2 additions & 2 deletions examples/typegoose/entities/recipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prop as Property, getModelForClass } from "@typegoose/typegoose";
import { ObjectId } from "mongodb";
import { Types } from "mongoose";
import { Field, ObjectType } from "../../../src";

import { Rate } from "./rate";
Expand All @@ -9,7 +9,7 @@ import { Ref } from "../types";
@ObjectType()
export class Recipe {
@Field()
readonly _id: ObjectId;
readonly _id: Types.ObjectId;

@Field()
@Property({ required: true })
Expand Down
4 changes: 2 additions & 2 deletions examples/typegoose/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { prop as Property, getModelForClass } from "@typegoose/typegoose";
import { ObjectId } from "mongodb";
import { Types } from "mongoose";
import { Field, ObjectType } from "../../../src";

@ObjectType()
export class User {
@Field()
readonly _id: ObjectId;
readonly _id: Types.ObjectId;

@Field()
@Property({ required: true })
Expand Down
4 changes: 2 additions & 2 deletions examples/typegoose/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Recipe, RecipeModel } from "./entities/recipe";
import { RecipeModel } from "./entities/recipe";
import { User, UserModel } from "./entities/user";

export async function seedDatabase() {
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function seedDatabase() {
{ value: 4, user: defaultUser },
],
},
] as Recipe[]);
]);

return { defaultUser };
}
5 changes: 2 additions & 3 deletions examples/typegoose/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "reflect-metadata";
import { ApolloServer } from "apollo-server";
import { connect } from "mongoose";
import { ObjectId } from "mongodb";
import { connect, Types } from "mongoose";
import * as path from "path";
import { buildSchema } from "../../src";

Expand Down Expand Up @@ -35,7 +34,7 @@ async function bootstrap() {
// use document converting middleware
globalMiddlewares: [TypegooseMiddleware],
// use ObjectId scalar mapping
scalarsMap: [{ type: ObjectId, scalar: ObjectIdScalar }],
scalarsMap: [{ type: Types.ObjectId, scalar: ObjectIdScalar }],
validate: false,
});

Expand Down
12 changes: 6 additions & 6 deletions examples/typegoose/object-id.scalar.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { GraphQLScalarType, Kind } from "graphql";
import { ObjectId } from "mongodb";
import { Types } from "mongoose";

export const ObjectIdScalar = new GraphQLScalarType({
name: "ObjectId",
description: "Mongo object id scalar type",
serialize(value: unknown): string {
if (!(value instanceof ObjectId)) {
if (!(value instanceof Types.ObjectId)) {
throw new Error("ObjectIdScalar can only serialize ObjectId values");
}
return value.toHexString();
},
parseValue(value: unknown): ObjectId {
parseValue(value: unknown): Types.ObjectId {
if (typeof value !== "string") {
throw new Error("ObjectIdScalar can only parse string values");
}
return new ObjectId(value);
return new Types.ObjectId(value);
},
parseLiteral(ast): ObjectId {
parseLiteral(ast): Types.ObjectId {
if (ast.kind !== Kind.STRING) {
throw new Error("ObjectIdScalar can only parse string values");
}
return new ObjectId(ast.value);
return new Types.ObjectId(ast.value);
},
});
6 changes: 3 additions & 3 deletions examples/typegoose/resolvers/recipe-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectId } from "mongodb";
import { Types } from "mongoose";
import { Resolver, Query, FieldResolver, Arg, Root, Mutation, Ctx } from "../../../src";

import { Recipe, RecipeModel } from "../entities/recipe";
Expand All @@ -12,7 +12,7 @@ import { ObjectIdScalar } from "../object-id.scalar";
@Resolver(of => Recipe)
export class RecipeResolver {
@Query(returns => Recipe, { nullable: true })
recipe(@Arg("recipeId", type => ObjectIdScalar) recipeId: ObjectId) {
recipe(@Arg("recipeId", type => ObjectIdScalar) recipeId: Types.ObjectId) {
return RecipeModel.findById(recipeId);
}

Expand All @@ -29,7 +29,7 @@ export class RecipeResolver {
const recipe = new RecipeModel({
...recipeInput,
author: user._id,
} as Recipe);
});

await recipe.save();
return recipe;
Expand Down
4 changes: 2 additions & 2 deletions examples/typegoose/resolvers/types/rate-input.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ObjectId } from "mongodb";
import { Types } from "mongoose";
import { InputType, Field, Int } from "../../../../src";

@InputType()
export class RateInput {
@Field()
recipeId: ObjectId;
recipeId: Types.ObjectId;

@Field(type => Int)
value: number;
Expand Down
4 changes: 2 additions & 2 deletions examples/typegoose/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ObjectId } from "mongodb";
import { Types } from "mongoose";

export type Ref<T> = T | ObjectId;
export type Ref<T> = T | Types.ObjectId;
2 changes: 1 addition & 1 deletion examples/typeorm-basic-usage/resolvers/rate-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export class RateResolver {

@FieldResolver()
async user(@Root() rate: Rate): Promise<User> {
return (await this.userRepository.findOne(rate.userId, { cache: 1000 }))!;
return (await this.userRepository.findOne({ where: { id: rate.userId }, cache: 1000 }))!;
}
}
7 changes: 4 additions & 3 deletions examples/typeorm-basic-usage/resolvers/recipe-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class RecipeResolver {

@Query(returns => Recipe, { nullable: true })
recipe(@Arg("recipeId", type => Int) recipeId: number) {
return this.recipeRepository.findOne(recipeId);
return this.recipeRepository.findOneBy({ id: recipeId });
}

@Query(returns => [Recipe])
Expand All @@ -42,7 +42,8 @@ export class RecipeResolver {
@Mutation(returns => Recipe)
async rate(@Arg("rate") rateInput: RateInput, @Ctx() { user }: Context): Promise<Recipe> {
// find the recipe
const recipe = await this.recipeRepository.findOne(rateInput.recipeId, {
const recipe = await this.recipeRepository.findOne({
where: { id: rateInput.recipeId },
relations: ["ratings"],
});
if (!recipe) {
Expand Down Expand Up @@ -72,6 +73,6 @@ export class RecipeResolver {

@FieldResolver()
async author(@Root() recipe: Recipe): Promise<User> {
return (await this.userRepository.findOne(recipe.authorId, { cache: 1000 }))!;
return (await this.userRepository.findOne({ where: { id: recipe.authorId }, cache: 1000 }))!;
}
}
4 changes: 2 additions & 2 deletions examples/typeorm-basic-usage/resolvers/types/rate-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { InputType, Field, Int, ID } from "../../../../src";

@InputType()
export class RateInput {
@Field(type => ID)
recipeId: string;
@Field(type => Int)
recipeId: number;

@Field(type => Int)
value: number;
Expand Down
5 changes: 3 additions & 2 deletions examples/typeorm-lazy-relations/resolvers/recipe-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RecipeResolver {

@Query(returns => Recipe, { nullable: true })
recipe(@Arg("recipeId", type => Int) recipeId: number) {
return this.recipeRepository.findOne(recipeId);
return this.recipeRepository.findOneBy({ id: recipeId });
}

@Query(returns => [Recipe])
Expand All @@ -37,7 +37,8 @@ export class RecipeResolver {
@Mutation(returns => Recipe)
async rate(@Ctx() { user }: Context, @Arg("rate") rateInput: RateInput): Promise<Recipe> {
// find the recipe
const recipe = await this.recipeRepository.findOne(rateInput.recipeId, {
const recipe = await this.recipeRepository.findOne({
where: { id: rateInput.recipeId },
relations: ["ratings"], // preload the relation as we will modify it
});
if (!recipe) {
Expand Down
6 changes: 3 additions & 3 deletions examples/typeorm-lazy-relations/resolvers/types/rate-input.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { InputType, Field, Int, ID } from "../../../../src";
import { InputType, Field, Int } from "../../../../src";

@InputType()
export class RateInput {
@Field(type => ID)
recipeId: string;
@Field(type => Int)
recipeId: number;

@Field(type => Int)
value: number;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"check:format": "prettier --check \"{src,tests,examples}/**/*.{ts,js}\" \"docs/**/*.md\"",
"check:type": "tsc --noEmit && tsc --noEmit -p ./examples/tsconfig.json",
"check": "npm run check:format && npm run check:type",
"//lint": "tslint --project tsconfig.json",
"lint": "echo TODO: add ESLint",
"verify": "npm run check && npm run lint",
"package": "gulp package",
"docs": "npm run --prefix website start",
Expand Down

0 comments on commit 2658acf

Please sign in to comment.