Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add endpoint. #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cats/cats.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type Query {
cats: [Cat]
cat(id: ID!): Cat
catByIds(ids: [ID!]): [Cat]
}

type Mutation {
Expand Down
10 changes: 9 additions & 1 deletion src/cats/cats.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParseIntPipe, UseGuards } from '@nestjs/common';
import { ParseArrayPipe, ParseIntPipe, UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { Cat } from '../graphql.schema';
Expand Down Expand Up @@ -26,6 +26,14 @@ export class CatsResolver {
return this.catsService.findOneById(id);
}

@Query('catByIds')
async findByIds(
@Args('ids', new ParseArrayPipe({ items: Number, separator: ',' }))
ids: number[],
): Promise<Cat[]> {
return this.catsService.findByIds(ids);
}

@Mutation('createCat')
async create(@Args('createCatInput') args: CreateCatDto): Promise<Cat> {
const createdCat = await this.catsService.create(args);
Expand Down
4 changes: 4 additions & 0 deletions src/cats/cats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export class CatsService {
findOneById(id: number): Cat {
return this.cats.find(cat => cat.id === id);
}

findByIds(ids: number[]): Cat[] {
return ids.map(id => this.cats.find(cat => cat.id === id))
}
}
36 changes: 21 additions & 15 deletions src/graphql.schema.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
/** ------------------------------------------------------

/*
* -------------------------------------------------------
* THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
* -------------------------------------------------------
*/

/* tslint:disable */
/* eslint-disable */
export class CreateCatInput {
name?: string;
age?: number;
name?: Nullable<string>;
age?: Nullable<number>;
}

export abstract class IQuery {
abstract cats(): Cat[] | Promise<Cat[]>;
abstract cats(): Nullable<Nullable<Cat>[]> | Promise<Nullable<Nullable<Cat>[]>>;

abstract cat(id: string): Cat | Promise<Cat>;
abstract cat(id: string): Nullable<Cat> | Promise<Nullable<Cat>>;

abstract catByIds(ids?: Nullable<string[]>): Nullable<Nullable<Cat>[]> | Promise<Nullable<Nullable<Cat>[]>>;
}

export abstract class IMutation {
abstract createCat(createCatInput?: CreateCatInput): Cat | Promise<Cat>;
abstract createCat(createCatInput?: Nullable<CreateCatInput>): Nullable<Cat> | Promise<Nullable<Cat>>;
}

export abstract class ISubscription {
abstract catCreated(): Cat | Promise<Cat>;
abstract catCreated(): Nullable<Cat> | Promise<Nullable<Cat>>;
}

export class Owner {
id: number;
name: string;
age?: number;
cats?: Cat[];
id: number;
name: string;
age?: Nullable<number>;
cats?: Nullable<Cat[]>;
}

export class Cat {
id?: number;
name?: string;
age?: number;
owner?: Owner;
id?: Nullable<number>;
name?: Nullable<string>;
age?: Nullable<number>;
owner?: Nullable<Owner>;
}

type Nullable<T> = T | null;