diff --git a/app/fetch_event.ts b/app/fetch_event.ts index cd50265c88adbb5f895ee96933a93dc89caaa6cb..15d0d8e2a0739f9c3a8032e2374aa5beb1aa1391 100644 --- a/app/fetch_event.ts +++ b/app/fetch_event.ts @@ -6,14 +6,14 @@ import NotFound = Deno.errors.NotFound const APP_API_BASE_URL = 'https://gemeinschaftswerk-nachhaltigkeit.de/app/api/v1' const APP_FILES_BASE_URL = 'https://gemeinschaftswerk-nachhaltigkeit.de/app/api/v1/files' -export type HoliEventResponse = { - data: HoliEvent -} - export type FetchEventInput = { id: string } +export type HoliEventResponse = { + data: HoliEvent +} + export const toHoliEvent = (activity: Activity): HoliEvent | null => { if (activity.period.permanent) return null @@ -45,7 +45,6 @@ export const fetchEvent = async (input: FetchEventInput) => { const startDate = Date.now() logger.info(`fetching event from ${url}`) - const response = await fetch(url) if (response.status === 404) { @@ -53,10 +52,13 @@ export const fetchEvent = async (input: FetchEventInput) => { } try { - const json = (await response.json()).activity as Activity - + const json = await response.json() const holiEvent = toHoliEvent(json) + if (!holiEvent) { + throw new NotFound('Not found') + } + return { data: holiEvent, } @@ -68,6 +70,6 @@ export const fetchEvent = async (input: FetchEventInput) => { throw e } finally { const duration = Date.now() - startDate - logger.debug(`fetching projects took ${duration} ms`) + logger.debug(`fetching event took ${duration} ms`) } } diff --git a/app/fetch_event_test.ts b/app/fetch_event_test.ts index 03c8d8f480e1ef6e9a40228c400413c2f2d07096..1f0b17d3f8c223fab09180616a08262e6c4fc747 100644 --- a/app/fetch_event_test.ts +++ b/app/fetch_event_test.ts @@ -1,8 +1,8 @@ import { RealTimedEvent } from './providers/jasd/tests/fixtures.ts' import { fetchEvent } from './fetch_event.ts' -import { returnsNext, stub } from 'https://deno.land/std@0.165.0/testing/mock.ts' -import { describe, it } from 'https://deno.land/std@0.165.0/testing/bdd.ts' -import { assertEquals, assertRejects } from 'https://deno.land/std@0.165.0/testing/asserts.ts' +import { returnsNext, stub } from '@std/testing/mock' +import { describe, it } from '@std/testing/bdd' +import { assertEquals, assertRejects } from '@std/assert' import NotFound = Deno.errors.NotFound describe('fetchEvent', () => { @@ -22,7 +22,7 @@ describe('fetchEvent', () => { const fakeFetch = stub( globalThis, 'fetch', - returnsNext([Promise.resolve(new Response(JSON.stringify(RealTimedEvent)))]), + returnsNext([Promise.resolve(new Response(JSON.stringify(RealTimedEvent.activity)))]), ) const expectedEvent = { @@ -55,6 +55,4 @@ describe('fetchEvent', () => { fakeFetch.restore() }) - - //return 500 errors for internal problems }) diff --git a/app/server.ts b/app/server.ts index ca8cc88846b4ba39e1f8b0afc1e953d30c459e14..51dfff1aec5bdb614e8d404deb0f2b014631a149 100644 --- a/app/server.ts +++ b/app/server.ts @@ -1,6 +1,7 @@ import { createSchema, createYoga } from './deps.ts' import { logger } from './logging.ts' import { fetchEvents, FetchEventsInput, HoliEventsResponse } from './fetch_events.ts' +import { fetchEvent, FetchEventInput, HoliEventResponse } from './fetch_event.ts' const eventsTypeDefs = ` type Location { @@ -33,9 +34,14 @@ const eventsTypeDefs = ` data: [Event] } + type EventResponse { + data: Event + } + type Query { # uses offset-based pagination as described in https://www.apollographql.com/docs/react/pagination/offset-based events(offset: Int = 0, limit: Int = 5, location: String): EventsResponse! + event(id: String!): EventResponse! } ` @@ -50,7 +56,6 @@ export type EventRequest = { } const validateEventsRequestInput = (request: EventsRequest): FetchEventsInput => { - console.log(request) return { limit: request.limit ?? 5, offset: request.offset ?? 0, @@ -58,7 +63,7 @@ const validateEventsRequestInput = (request: EventsRequest): FetchEventsInput => } } -const _validateEventRequestInput = (request: EventRequest): FetchEventsInput => { +const validateEventRequestInput = (request: EventRequest): FetchEventInput => { console.log(request) return { id: request.id, @@ -83,7 +88,6 @@ const createResolvers = (_config: ServerConfig) => ({ ) } }, - /* event: ( // deno-lint-ignore no-explicit-any _parent: any, @@ -91,16 +95,15 @@ const createResolvers = (_config: ServerConfig) => ({ // next line is required for the resolver to work // deno-lint-ignore no-unused-vars context: GraphQLContext, - ): Promise<HoliEventsResponse> => { + ): Promise<HoliEventResponse> => { if (_config.fake) { - return Promise.resolve({ totalResults: 0, data: [] }) + return Promise.resolve({ data: {} }) } else { return fetchEvent( validateEventRequestInput(parameters), ) } }, - */ }, }) diff --git a/deno.json b/deno.json index b8a724ebeb8d9aa0356468097a6c229b77765dd6..972875f88f0444468fa144fdd1caa2fe4aab3321 100644 --- a/deno.json +++ b/deno.json @@ -15,7 +15,8 @@ "singleQuote": true, "semiColons": false, "include": [ - "app/" + "app/", + "smoketest/" ], "exclude": [ "*.md" diff --git a/smoketest/main.js b/smoketest/main.js index d311c4412806fd23198ecc163d0acf16dbf7cfa9..41a4b5bc18df6822ffa686b27d0778e8f2ffe4ab 100644 --- a/smoketest/main.js +++ b/smoketest/main.js @@ -17,13 +17,21 @@ function forQuery(query, checkFunction) { } export default () => { - forQuery(`{ events(limit:1) { data { id } } }`, (response) => { + forQuery('query IntrospectionQuery { __schema { types { name } }}', (response) => { check(response, { - 'is status 200': (r) => r.status === 200, + 'can be introspected': (r) => r.status === 200, }) + }) + + forQuery(`{ events(limit: 1) { data { id } } }`, (response) => { + check(JSON.parse(response.body), { + 'returns events': (r) => Array.isArray(r.data.events.data) && r.data.events.data.length > 0, + }) + }) + forQuery(`{ event(id: "1234") { data { id } } }`, (response) => { check(JSON.parse(response.body), { - 'contains events': (r) => Array.isArray(r.data.events.data) && r.data.events.data.length > 0, + 'returns event': (r) => r.data.event.data.id === '1234', }) }) }