Skip to content

Commit

Permalink
fix: korjaa graphql-kutsut yllapidon osoitteeseen (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
haapamakim authored Nov 2, 2021
1 parent 229227c commit a9cf34a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 190 deletions.
1 change: 1 addition & 0 deletions backend/src/service/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const identifyUser = async (event: AppSyncResolverEvent<any>) => {
return;
}
}
vaylaUser = undefined;
log.info("Anonymous user");
};

Expand Down
179 changes: 3 additions & 176 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions src/services/api/commonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,34 @@ const defaultOptions: DefaultOptions = {
};

export class API extends AbstractApi {
private client: ApolloClient<any>;
private publicClient: ApolloClient<any>;
private authenticatedClient: ApolloClient<any>;

constructor(link: ApolloLink) {
constructor(publicLink: ApolloLink, authenticatedLink: ApolloLink) {
super();
this.client = new ApolloClient({
link,
this.publicClient = new ApolloClient({
link: publicLink,
cache: new InMemoryCache(),
defaultOptions,
});
this.authenticatedClient = new ApolloClient({
link: authenticatedLink,
cache: new InMemoryCache(),
defaultOptions,
});
}

async callGraphQL(operation: OperationConfig, variables: any) {
async callGraphQL(client: ApolloClient<any>, operation: OperationConfig, variables: any) {
switch (operation.operationType) {
case OperationType.Query:
const queryResponse: ApolloQueryResult<any> = await this.client.query({
const queryResponse: ApolloQueryResult<any> = await client.query({
query: gql(operation.graphql),
variables,
fetchPolicy: "network-only",
});
return queryResponse.data?.[operation.name];
case OperationType.Mutation:
const fetchResponse: FetchResult<any> = await this.client.mutate({
const fetchResponse: FetchResult<any> = await client.mutate({
mutation: gql(operation.graphql),
variables,
});
Expand All @@ -48,10 +54,10 @@ export class API extends AbstractApi {
}

async callAPI(operation: OperationConfig, variables?: any): Promise<any> {
return await this.callGraphQL(operation, variables);
return await this.callGraphQL(this.publicClient, operation, variables);
}

async callYllapitoAPI(operation: OperationConfig, variables?: any): Promise<any> {
return await this.callGraphQL(operation, variables);
return await this.callGraphQL(this.authenticatedClient, operation, variables);
}
}
2 changes: 1 addition & 1 deletion src/services/api/developerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ const link = ApolloLink.from([
}),
]);

export const api = new API(link);
export const api = new API(link, link);
19 changes: 15 additions & 4 deletions src/services/api/permanentApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ import { createAuthLink } from "aws-appsync-auth-link";
import { createHttpLink } from "apollo-link-http";
import { API } from "@services/api/commonApi";

const link = ApolloLink.from([
const publicLink = ApolloLink.from([
createAuthLink({
url: awsExports.aws_appsync_graphqlEndpoint,
url: "/graphql",
region: awsExports.aws_appsync_region,
auth: { type: "API_KEY", apiKey: awsExports.aws_appsync_apiKey || "" },
}),
createHttpLink({
uri: awsExports.aws_appsync_graphqlEndpoint,
uri: "/graphql",
}),
]);

export const api = new API(link);
const authenticatedLink = ApolloLink.from([
createAuthLink({
url: "/yllapito/graphql",
region: awsExports.aws_appsync_region,
auth: { type: "API_KEY", apiKey: awsExports.aws_appsync_apiKey || "" },
}),
createHttpLink({
uri: "/yllapito/graphql",
}),
]);

export const api = new API(publicLink, authenticatedLink);

0 comments on commit a9cf34a

Please sign in to comment.