From 85e1d95631f51cf783ce5c873fc817af5d4f78ef Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Fri, 11 Sep 2020 19:15:01 +0200 Subject: [PATCH] type-des: first test mutation --- src/datasources/db/cargobikeAPI.ts | 24 +++++++++++++++++++++++- src/resolvers/cargobikeResolver.ts | 4 ++++ src/schema/type-defs.ts | 3 ++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 824195e..a88844e 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -1,9 +1,15 @@ import { DataSource } from 'apollo-datasource' - +import { getConnection, Connection } from 'typeorm' +import { CargoBike } from '../../model/CargoBike' /** * extended datasource to feed resolvers with data about cargoBikes */ export class CargoBikeAPI extends DataSource { + connection : Connection + constructor(){ + super() + this.connection = getConnection() + } /** * Finds cargo bike by id */ @@ -13,4 +19,20 @@ export class CargoBikeAPI extends DataSource { name: token } } + + async updateBike ({ id, token, name }:{id:any, token: string, name: string }) { + const bike = new CargoBike() + bike.id = id + bike.description = token + bike.name = name + this.connection.manager.save(bike) + return { + success: true, + message: token, + bike: { + id, + name + } + } + } } diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 56da61a..bca1077 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -2,5 +2,9 @@ export default { Query: { cargobike: (_: any, { id, token }:{id: any, token: string}, { dataSources }:{dataSources: any}) => dataSources.cargoBikeAPI.findCargoBikeById({ id, token }) + }, + Mutation: { + addBike: (_:any, { id, token, name }:{id: any, token: string, name:string}, { dataSources }:{dataSources: any }) => + dataSources.cargoBikeAPI.updateBike({ id, token, name }) } } diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index aaf7b2a..1c3739c 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -265,9 +265,10 @@ type UpdateBikeResponse { message: String bike: CargoBike } + "for testing" type Mutation { - addBike(id: ID!):UpdateBikeResponse + addBike(id: ID!, token: String!, name: String):UpdateBikeResponse } `