Skip to content

Commit

Permalink
avoid usage of let and shorten unit test to just focus on User Agent …
Browse files Browse the repository at this point in the history
…Override
  • Loading branch information
yuhengshs committed Sep 27, 2024
1 parent 9c4b98c commit a2734a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 66 deletions.
59 changes: 17 additions & 42 deletions packages/api-graphql/__tests__/GraphQLAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1627,53 +1627,28 @@ describe('API test', () => {
},
},
});
const threadToGet = {
id: 'some-id',
topic: 'something reasonably interesting',
};
const graphqlVariables = { id: 'some-id' };
const graphqlResponse = {
data: {
getThread: {
__typename: 'Thread',
...serverManagedFields,
...threadToGet,
},

const mockPost = jest.fn().mockResolvedValue({
body: {
json: () => ({ data: { test: 'result' } }),
},
};
const spy = jest
.spyOn((raw.GraphQLAPI as any)._api, 'post')
.mockReturnValue({
body: {
json: () => graphqlResponse,
},
});
const graphqlOptions: raw.GraphQLOptionsV6<
GetThreadQuery,
typeof typedQueries.getThread
> = {
query: typedQueries.getThread,
variables: graphqlVariables,
});
(raw.GraphQLAPI as any)._api.post = mockPost;

const graphqlOptions = {
query: 'query TestQuery { test }',
variables: { id: 'some-id' },
authMode: 'apiKey' as GraphQLAuthMode,
[INTERNAL_USER_AGENT_OVERRIDE]: {
category: 'CustomCategory',
action: 'CustomAction',
},
};
// Add the INTERNAL_USER_AGENT_OVERRIDE to the options object
(graphqlOptions as any)[INTERNAL_USER_AGENT_OVERRIDE] = {
category: 'CustomCategory',
action: 'CustomAction',
};

const result: GraphQLResult<GetThreadQuery> =
await client.graphql(graphqlOptions);

const thread: GetThreadQuery['getThread'] = result.data?.getThread;
const errors = result.errors;

expectGet(spy, 'getThread', graphqlVariables);
expect(errors).toBe(undefined);
expect(thread).toEqual(graphqlResponse.data.getThread);
await client.graphql(graphqlOptions);

// Check if the INTERNAL_USER_AGENT_OVERRIDE was properly handled
expect(spy).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
options: expect.objectContaining({
Expand All @@ -1686,7 +1661,7 @@ describe('API test', () => {
}),
);
// Ensure the INTERNAL_USER_AGENT_OVERRIDE was not passed along in the options
expect(spy).not.toHaveBeenCalledWith(
expect(mockPost).not.toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
options: expect.objectContaining({
Expand Down
2 changes: 1 addition & 1 deletion packages/api-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"dependencies": {
"@aws-amplify/api-rest": "4.0.49",
"@aws-amplify/core": "6.4.2",
"@aws-amplify/data-schema": "^1.6.3",
"@aws-amplify/data-schema": "^1.5.0",
"@aws-sdk/types": "3.387.0",
"graphql": "15.8.0",
"rxjs": "^7.8.1",
Expand Down
31 changes: 12 additions & 19 deletions packages/api-graphql/src/GraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,26 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
options: GraphQLOptions,
additionalHeaders?: CustomHeaders,
): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
let cleanOptions = options;
let userAgentDetails: CustomUserAgentDetails;
const userAgentDetails: CustomUserAgentDetails = {
category: Category.API,
action: ApiAction.GraphQl,
};

if (isGraphQLOptionsWithOverride(options)) {
const {
[INTERNAL_USER_AGENT_OVERRIDE]: internalUserAgentOverride,
...rest
...cleanOptions
} = options;
userAgentDetails = {
category: Category.API,
action: ApiAction.GraphQl,

return super.graphql(amplify, cleanOptions, additionalHeaders, {
...userAgentDetails,
...internalUserAgentOverride,
};
cleanOptions = rest;
} else {
userAgentDetails = {
category: Category.API,
action: ApiAction.GraphQl,
};
});
}

return super.graphql(
amplify,
cleanOptions,
additionalHeaders,
userAgentDetails,
);
return super.graphql(amplify, options, additionalHeaders, {
...userAgentDetails,
});
}

/**
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock

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

0 comments on commit a2734a5

Please sign in to comment.