From 5de9c7a645000b49cd1badfd64ab3b89410f108f Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Sat, 19 Sep 2020 11:21:05 +0930 Subject: [PATCH] fix: servers endpoint now returns correct data --- app/cache/user.ts | 2 +- app/graphql/schema/resolvers.ts | 29 +++++++++++++++++++---------- package.json | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/cache/user.ts b/app/cache/user.ts index a19eadea3..9fdb835d3 100644 --- a/app/cache/user.ts +++ b/app/cache/user.ts @@ -25,5 +25,5 @@ export interface CachedServer { }; export interface CachedServers { - servers: [CachedServer] + servers: CachedServer[] }; \ No newline at end of file diff --git a/app/graphql/schema/resolvers.ts b/app/graphql/schema/resolvers.ts index 6e0eaf5c0..9a36580d2 100644 --- a/app/graphql/schema/resolvers.ts +++ b/app/graphql/schema/resolvers.ts @@ -95,20 +95,32 @@ type Server = makeNullUndefinedAndOptional; const getServers = async (): Promise => { const cachedServers = userCache.get('mine')?.servers; + if (cachedServers) { + return cachedServers; + } + + // For now use the my_servers key + // Later we should return the correct one for the current user with the correct scope, etc. + const apikey = apiManager.getValidKeys().find(key => key.name === 'my_servers')?.key.toString(); // No cached servers found if (!cachedServers) { // Fetch servers from mothership const servers = await fetch(MOTHERSHIP_GRAPHQL_LINK, { - method: 'GET', + method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', }, body: JSON.stringify({ - query: "{ servers { owner { username url avatar } guid apikey name status wanip lanip localurl remoteurl } }" + query: 'query($apikey: String!) { servers @auth(apiKey: $apikey) { owner { username url avatar } guid apikey name status wanip lanip localurl remoteurl } }', + variables: { + apikey + } }) - }).then(r => r.json() as Promise); + }) + .then(r => r.json()) + .then(({ data }) => data.servers as Promise); log.debug('Using upstream for /servers endpoint'); @@ -117,10 +129,10 @@ const getServers = async (): Promise => { return []; } - // @todo: Cache servers - // userCache.set('mine', { - // servers - // }) + // Cache servers + userCache.set('mine', { + servers + }); // Return servers from mothership return servers; @@ -128,9 +140,6 @@ const getServers = async (): Promise => { log.debug('Falling back to local state for /servers endpoint'); const guid = varState?.data?.regGuid; - // For now use the my_servers key - // Later we should return the correct one for the current user with the correct scope, etc. - const apikey = apiManager.getValidKeys().find(key => key.name === 'my_servers')?.key.toString(); const name = varState?.data?.name; const wanip = null; const lanip = networkState.data[0].ipaddr[0]; diff --git a/package.json b/package.json index 2cea675c7..dafd15c93 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "apollo-server", "apollo-server-express", "camelcase", + "cross-fetch", "dot-prop", "express", "graphql",