From 9aa94a33ce08e6ff10320e0337ec4eae77217141 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Thu, 10 Sep 2020 17:07:05 +0200 Subject: [PATCH] src: added skeleton --- gulpfile.js | 5 +++++ src/datasources/db/cargobikeAPI.ts | 20 ++++++++++++++++++++ src/datasources/userserver/userserviceAPI.ts | 17 +++++++++++++++++ src/index.ts | 9 ++++++--- src/resolvers/cargobike.ts | 10 ---------- src/resolvers/cargobikeResolver.ts | 6 ++++++ src/schema/type-defs.ts | 2 +- 7 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 src/datasources/db/cargobikeAPI.ts create mode 100644 src/datasources/userserver/userserviceAPI.ts delete mode 100644 src/resolvers/cargobike.ts create mode 100644 src/resolvers/cargobikeResolver.ts diff --git a/gulpfile.js b/gulpfile.js index 96a3e73..13bde17 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -64,4 +64,9 @@ task('watch', () => { task('watchnolint', () => { watch('**/*.ts', compileTypescript); watch(['src/**/*', '!src/**/*.ts'], moveRemaining()); + nodemon({ + script: 'dist/index.js', + watch: ['dist/**/*.js'], + ext: 'js' + }); }); \ No newline at end of file diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts new file mode 100644 index 0000000..e9f3a07 --- /dev/null +++ b/src/datasources/db/cargobikeAPI.ts @@ -0,0 +1,20 @@ +import { DataSource } from 'apollo-datasource'; + +/** + * extended datasource to feed resolvers with data about cargoBikes + */ +export class CargoBikeAPI extends DataSource { + constructor() { + super(); + // this.store = store; + } + /** + * Finds cargo bike by id + */ + async findCargoBikeById({ id }:{id: any}){ + return { + id, + name: "hello world" + }; + } +} \ No newline at end of file diff --git a/src/datasources/userserver/userserviceAPI.ts b/src/datasources/userserver/userserviceAPI.ts new file mode 100644 index 0000000..9b4b95a --- /dev/null +++ b/src/datasources/userserver/userserviceAPI.ts @@ -0,0 +1,17 @@ +import { DataSource} from 'apollo-datasource'; + +/** + * fetches datafrom user server, especially validates user tokens + */ +export class UserServerAPI extends DataSource{ + constructor() { + super(); + } + + /** + * validates user token + */ + async validateToken(token:string){ + return true; + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d608660..99c9c05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,15 @@ // tslint:disable: no-console import { ApolloServer } from 'apollo-server'; -import bikeresolver from './resolvers/cargobike'; - +import bikeresolver from './resolvers/cargobikeResolver'; +import { CargoBikeAPI } from './datasources/db/cargobikeAPI'; import typeDefs from './schema/type-defs'; const server = new ApolloServer({ resolvers:[bikeresolver], - typeDefs + typeDefs, + dataSources: () => ({ + cargoBikeAPI: new CargoBikeAPI(), + }) }); server.listen() diff --git a/src/resolvers/cargobike.ts b/src/resolvers/cargobike.ts deleted file mode 100644 index 2829022..0000000 --- a/src/resolvers/cargobike.ts +++ /dev/null @@ -1,10 +0,0 @@ -export default { - Query: { - cargobike: () => { - return { - id:1, - name: "hello!" - }; - } - } -}; \ No newline at end of file diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts new file mode 100644 index 0000000..b6dab85 --- /dev/null +++ b/src/resolvers/cargobikeResolver.ts @@ -0,0 +1,6 @@ +export default { + Query: { + cargobike: (_: any,{ id }:{id: any},{dataSources}:{dataSources: any}) => + dataSources.cargoBikeAPI.findCargoBikeById( { id } ) + }, +}; diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 277a779..6c1779c 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -234,7 +234,7 @@ type Organisation{ vereinsregisternr: Int } type Query { - cargobike: CargoBike + cargobike(id:ID!): CargoBike } `; \ No newline at end of file