mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
fix(appsync): honor configured base URL in API URIs (#2457)
* fix(appsync): honor configured base URL in API URIs * fix(appsync): repair persisted API URIs
This commit is contained in:
1 parent
c969ffc31d
commit
86e1c016cd
2 files changed
+122
-7
No files matched your search
@@ -44,6 +44,7 @@ public class AppSyncService {
|
||||
private final Instance<RequestContext> requestContextInstance;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Clock clock;
|
||||
private final String baseUrl;
|
||||
|
||||
@Inject
|
||||
public AppSyncService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver,
|
||||
@@ -70,6 +71,7 @@ public class AppSyncService {
|
||||
this.requestContextInstance = requestContextInstance;
|
||||
this.objectMapper = objectMapper;
|
||||
this.clock = clock;
|
||||
this.baseUrl = trimTrailingSlash(config.effectiveBaseUrl());
|
||||
}
|
||||
|
||||
// ──────────────────────────── GraphQL API ────────────────────────────
|
||||
@@ -124,11 +126,7 @@ public class AppSyncService {
|
||||
|
||||
api.setArn(buildApiArn(apiId, region));
|
||||
|
||||
Map<String, String> uris = new HashMap<>();
|
||||
String baseUri = "http://localhost:4566";
|
||||
uris.put("GRAPHQL", baseUri + "/v1/apis/" + apiId + "/graphql");
|
||||
uris.put("REALTIME", "ws://localhost:4566/v1/apis/" + apiId + "/graphql/realtime");
|
||||
api.setUris(uris);
|
||||
api.setUris(graphqlApiUris(apiId));
|
||||
|
||||
Map<String, Object> tags = castMap(request.get("tags"));
|
||||
if (tags != null) {
|
||||
@@ -144,11 +142,14 @@ public class AppSyncService {
|
||||
|
||||
public GraphqlApi getGraphqlApi(String apiId) {
|
||||
return apiStore.get(apiId)
|
||||
.map(this::refreshGraphqlApiUris)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException", "GraphQL API not found: " + apiId, 404));
|
||||
}
|
||||
|
||||
public Page<GraphqlApi> listGraphqlApis(Integer maxResults, String nextToken) {
|
||||
return paginate(apiStore.scan(k -> true), nextToken, maxResults);
|
||||
List<GraphqlApi> apis = apiStore.scan(k -> true);
|
||||
apis.forEach(this::refreshGraphqlApiUris);
|
||||
return paginate(apis, nextToken, maxResults);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1128,6 +1129,31 @@ public class AppSyncService {
|
||||
return result != null ? result : defaultValue;
|
||||
}
|
||||
|
||||
private static String trimTrailingSlash(String value) {
|
||||
return value.endsWith("/") ? value.substring(0, value.length() - 1) : value;
|
||||
}
|
||||
|
||||
private static String toWebSocketBaseUrl(String value) {
|
||||
return value.replaceFirst("^http", "ws");
|
||||
}
|
||||
|
||||
private Map<String, String> graphqlApiUris(String apiId) {
|
||||
String graphqlPath = "/v1/apis/" + apiId + "/graphql";
|
||||
Map<String, String> uris = new HashMap<>();
|
||||
uris.put("GRAPHQL", baseUrl + graphqlPath);
|
||||
uris.put("REALTIME", toWebSocketBaseUrl(baseUrl) + graphqlPath + "/realtime");
|
||||
return uris;
|
||||
}
|
||||
|
||||
private GraphqlApi refreshGraphqlApiUris(GraphqlApi api) {
|
||||
Map<String, String> expectedUris = graphqlApiUris(api.getApiId());
|
||||
if (!expectedUris.equals(api.getUris())) {
|
||||
api.setUris(expectedUris);
|
||||
apiStore.put(api.getApiId(), api);
|
||||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
private Integer castInt(Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof Integer i) return i;
|
||||
|
||||
@@ -31,9 +31,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AppSyncServiceTest {
|
||||
|
||||
@@ -200,6 +204,75 @@ class AppSyncServiceTest {
|
||||
assertEquals(1, api.getAdditionalAuthenticationProviders().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void graphqlApiUrisUseConfiguredBaseUrlAndPersist() {
|
||||
AppSyncService configuredService = newService(
|
||||
Clock.fixed(NOW, ZoneOffset.UTC), "http://floci.example:4577/");
|
||||
|
||||
GraphqlApi api = configuredService.createGraphqlApi(
|
||||
Map.of("name", "configured-url", "authenticationType", "API_KEY"), "us-east-1");
|
||||
Map<String, String> expectedUris = Map.of(
|
||||
"GRAPHQL", "http://floci.example:4577/v1/apis/" + api.getApiId() + "/graphql",
|
||||
"REALTIME", "ws://floci.example:4577/v1/apis/" + api.getApiId() + "/graphql/realtime");
|
||||
|
||||
assertEquals(expectedUris, api.getUris());
|
||||
assertEquals(expectedUris, configuredService.getGraphqlApi(api.getApiId()).getUris());
|
||||
assertEquals(expectedUris, configuredService.listGraphqlApis(null, null).items().getFirst().getUris());
|
||||
}
|
||||
|
||||
@Test
|
||||
void graphqlApiRealtimeUriUsesSecureWebSocketForHttpsBaseUrl() {
|
||||
AppSyncService configuredService = newService(
|
||||
Clock.fixed(NOW, ZoneOffset.UTC), "https://floci.example:8443");
|
||||
|
||||
GraphqlApi api = configuredService.createGraphqlApi(
|
||||
Map.of("name", "secure-url", "authenticationType", "API_KEY"), "us-east-1");
|
||||
|
||||
assertEquals(
|
||||
"https://floci.example:8443/v1/apis/" + api.getApiId() + "/graphql",
|
||||
api.getUris().get("GRAPHQL"));
|
||||
assertEquals(
|
||||
"wss://floci.example:8443/v1/apis/" + api.getApiId() + "/graphql/realtime",
|
||||
api.getUris().get("REALTIME"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistedGraphqlApiUrisAreRepairedOnGetListAndUpdate() {
|
||||
AccountAwareStorageBackend<GraphqlApi> apiStore = spy(
|
||||
AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
AppSyncService legacyService = newService(
|
||||
Clock.fixed(NOW, ZoneOffset.UTC), "http://localhost:4566", apiStore);
|
||||
GraphqlApi api = legacyService.createGraphqlApi(
|
||||
Map.of("name", "persisted-api", "authenticationType", "API_KEY"), "us-east-1");
|
||||
String apiId = api.getApiId();
|
||||
Map<String, String> staleUris = api.getUris();
|
||||
|
||||
AppSyncService configuredService = newService(
|
||||
Clock.fixed(NOW, ZoneOffset.UTC), "https://floci.example:8443/", apiStore);
|
||||
Map<String, String> expectedUris = Map.of(
|
||||
"GRAPHQL", "https://floci.example:8443/v1/apis/" + apiId + "/graphql",
|
||||
"REALTIME", "wss://floci.example:8443/v1/apis/" + apiId + "/graphql/realtime");
|
||||
|
||||
clearInvocations(apiStore);
|
||||
assertEquals(expectedUris, configuredService.getGraphqlApi(apiId).getUris());
|
||||
verify(apiStore).put(apiId, api);
|
||||
|
||||
api.setUris(staleUris);
|
||||
apiStore.put(apiId, api);
|
||||
clearInvocations(apiStore);
|
||||
assertEquals(expectedUris, configuredService.listGraphqlApis(null, null).items().getFirst().getUris());
|
||||
verify(apiStore).put(apiId, api);
|
||||
|
||||
api.setUris(staleUris);
|
||||
apiStore.put(apiId, api);
|
||||
clearInvocations(apiStore);
|
||||
GraphqlApi updated = configuredService.updateGraphqlApi(
|
||||
apiId, Map.of("name", "updated-api"), "us-east-1");
|
||||
assertEquals("updated-api", updated.getName());
|
||||
assertEquals(expectedUris, updated.getUris());
|
||||
verify(apiStore, times(2)).put(apiId, api);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createResolverDoesNotReregisterSchema() {
|
||||
GraphqlApi api = service.createGraphqlApi(Map.of("name", "r", "authenticationType", "API_KEY"), "us-east-1");
|
||||
@@ -216,18 +289,34 @@ class AppSyncServiceTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private AppSyncService newService(Clock clock) {
|
||||
return newService(clock, "http://localhost:4566");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private AppSyncService newService(Clock clock, String baseUrl) {
|
||||
return newService(clock, baseUrl, AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private AppSyncService newService(Clock clock, String baseUrl,
|
||||
AccountAwareStorageBackend<GraphqlApi> apiStore) {
|
||||
StorageFactory storageFactory = new StorageFactory(null, null) {
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
if ("appsync-apis.json".equals(fileName)) {
|
||||
return (AccountAwareStorageBackend<V>) apiStore;
|
||||
}
|
||||
return AccountAwareStorageBackend.inMemory("000000000000");
|
||||
}
|
||||
};
|
||||
Instance<RequestContext> requestContext = mock(Instance.class);
|
||||
schemaRegistry = mock(SchemaRegistry.class);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.effectiveBaseUrl()).thenReturn(baseUrl);
|
||||
return new AppSyncService(
|
||||
storageFactory,
|
||||
mock(EmulatorConfig.class),
|
||||
config,
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
schemaRegistry,
|
||||
mock(SchemaCreationWorker.class),
|
||||
|
||||
Reference in new issue
Block a user