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 } `