-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): cleaning up a few functions and handling more graphql …
…scenarios
- Loading branch information
lpsmit
committed
Aug 15, 2024
1 parent
31bfe2a
commit c3be8c7
Showing
7 changed files
with
83 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { jsonToGraphQLQuery } from 'json-to-graphql-query'; | ||
import { trimOptions } from '../src/trimOptions'; | ||
import { trimQueryBody } from '../src/trimQueryBody'; | ||
|
||
jest.mock('json-to-graphql-query', () => ({ | ||
jsonToGraphQLQuery: jest.fn(), | ||
})); | ||
jest.mock('../src/trimQueryBody', () => ({ | ||
trimQueryBody: jest.fn(), | ||
})); | ||
|
||
describe('trimOptions', () => { | ||
it('should return options if isGraphQL is false', () => { | ||
const options = { isGraphQL: false }; | ||
expect(trimOptions(options)).toEqual(options); | ||
}); | ||
it('should return options if isGraphQL is true and existingQuery is not provided', () => { | ||
const options = { isGraphQL: true, body: { query: 'query' } }; | ||
jsonToGraphQLQuery.mockImplementation((value) => value); | ||
const expectedBody = JSON.stringify({ | ||
query: 'query', | ||
}); | ||
expect(trimOptions(options)).toEqual({ ...options, body: expectedBody }); | ||
}); | ||
it('should return options with trimmed query if isGraphQL is true and existingQuery is provided', () => { | ||
const options = { isGraphQL: true, existingQuery: 'existingQuery', body: { query: 'query' } }; | ||
const trimmedQuery = 'trimmedQuery'; | ||
trimQueryBody.mockReturnValue(trimmedQuery); | ||
jsonToGraphQLQuery.mockImplementation((value) => value); | ||
const expectedBody = JSON.stringify({ | ||
query: 'trimmedQuery', | ||
}); | ||
expect(trimOptions(options)).toEqual({ ...options, body: expectedBody }); | ||
}); | ||
it('should handle if options is not provided', () => { | ||
expect(trimOptions()).toEqual(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { jsonToGraphQLQuery } from 'json-to-graphql-query'; | ||
import { trimQueryBody } from './trimQueryBody'; | ||
|
||
export const trimOptions = (options) => { | ||
const { existingQuery, isGraphQL } = options || {}; | ||
const { | ||
body: requestBody, | ||
...restOfOptions | ||
} = options || {}; | ||
const { | ||
query, | ||
...restOfBody | ||
} = requestBody || {}; | ||
|
||
if (isGraphQL && query && existingQuery) { | ||
return { | ||
...restOfOptions, | ||
body: JSON.stringify({ | ||
query: jsonToGraphQLQuery(trimQueryBody(query, existingQuery), { pretty: true }), | ||
...restOfBody, | ||
}), | ||
}; | ||
} | ||
if (isGraphQL && query) { | ||
return { | ||
...restOfOptions, | ||
body: JSON.stringify({ | ||
query: jsonToGraphQLQuery(query, { pretty: true }), | ||
...restOfBody, | ||
}), | ||
}; | ||
} | ||
return options; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters