-
Notifications
You must be signed in to change notification settings - Fork 16
Ensure that arrays are properly supported #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,7 +56,57 @@ abstract class Rewriter { | |
| return variables; | ||
| } | ||
|
|
||
| public rewriteResponse(response: any, key: string | number): any { | ||
| /* | ||
| * Receives the parent object of the matched field with the key of the matched field. | ||
| * For arrays, the index of the element is also present. | ||
| */ | ||
| public rewriteResponse(response: any, key: string, index?: number): any { | ||
| return response; | ||
| } | ||
|
|
||
| /* | ||
| * Helper that extracts the element from the response if possible otherwise returns null. | ||
| */ | ||
| protected extractReponseElement(response: any, key: string, index?: number): any { | ||
| // Verify the response format | ||
| let element = null; | ||
| if (response === null || typeof response !== 'object') return element; | ||
|
|
||
| // Extract the key | ||
| element = response[key] || null; | ||
|
|
||
| // Extract the position | ||
| if (Array.isArray(element)) { | ||
| element = element[index!] || null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aaah https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator, learned something new! Thanks! |
||
| } | ||
|
|
||
| return element; | ||
| } | ||
|
|
||
| /* | ||
| * Helper that rewrite the element from the response if possible and returns the response. | ||
| */ | ||
| protected rewriteResponseElement( | ||
| response: any, | ||
| newElement: any, | ||
| key: string, | ||
| index?: number | ||
| ): any { | ||
| // Verify the response format | ||
| if (response === null || typeof response !== 'object') return response; | ||
|
|
||
| // Extract the key | ||
| let element = response[key]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getting a linting error here, this should be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right can you fix it since the PR is merge? |
||
|
|
||
| // Extract the position | ||
| // NOTE: We might eventually want to create an array if one is not present at the key | ||
| // and we receive an index in input | ||
| if (Array.isArray(element)) { | ||
| element[index!] = newElement; | ||
| } else { | ||
| response[key] = newElement; | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import ScalarFieldToObjectFieldRewriter from '../../src/rewriters/ScalarFieldToO | |
| import { gqlFmt } from '../testUtils'; | ||
|
|
||
| describe('Rewrite scalar field to be a nested object with a single scalar field', () => { | ||
| it('rewrites a scalar field to be an objet field with 1 scalar subfield', () => { | ||
| it('rewrites a scalar field to be an object field with 1 scalar subfield', () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😅 thanks for fixing! |
||
| const handler = new RewriteHandler([ | ||
| new ScalarFieldToObjectFieldRewriter({ | ||
| fieldName: 'title', | ||
|
|
@@ -226,4 +226,48 @@ describe('Rewrite scalar field to be a nested object with a single scalar field' | |
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('rewrites a scalar field array to be an array of object fields with 1 scalar subfield', () => { | ||
| const handler = new RewriteHandler([ | ||
| new ScalarFieldToObjectFieldRewriter({ | ||
| fieldName: 'titles', | ||
| objectFieldName: 'text' | ||
| }) | ||
| ]); | ||
|
|
||
| const query = gqlFmt` | ||
| query getThing { | ||
| thing { | ||
| titles | ||
| } | ||
| } | ||
| `; | ||
| const expectedRewritenQuery = gqlFmt` | ||
| query getThing { | ||
| thing { | ||
| titles { | ||
| text | ||
| } | ||
| } | ||
| } | ||
| `; | ||
| expect(handler.rewriteRequest(query)).toEqual({ | ||
| query: expectedRewritenQuery | ||
| }); | ||
| expect( | ||
| handler.rewriteResponse({ | ||
| thing: { | ||
| titles: [ | ||
| { | ||
| text: 'THING' | ||
| } | ||
| ] | ||
| } | ||
| }) | ||
| ).toEqual({ | ||
| thing: { | ||
| titles: ['THING'] | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import Rewriter, { RewriterOpts } from '../../src/rewriters/Rewriter'; | ||
|
|
||
| describe('rewriter', () => { | ||
| class TestRewriter extends Rewriter { | ||
| constructor(options: RewriterOpts) { | ||
| super(options); | ||
| } | ||
|
|
||
| public extractReponseElement(response: any, key: string, index?: number): any { | ||
| return super.extractReponseElement(response, key, index); | ||
| } | ||
|
|
||
| public rewriteResponseElement( | ||
| response: any, | ||
| newElement: any, | ||
| key: string, | ||
| index?: number | ||
| ): any { | ||
| return super.rewriteResponseElement(response, newElement, key, index); | ||
| } | ||
| } | ||
|
|
||
| describe('extractResponseElement', () => { | ||
| const rewriter = new TestRewriter({ fieldName: 'test' }); | ||
|
|
||
| it('can extract element in object', () => { | ||
| const key = 'key'; | ||
| const element = { a: 1 }; | ||
| const response = { [key]: element }; | ||
|
|
||
| expect(rewriter.extractReponseElement(response, key)).toEqual(element); | ||
| }); | ||
|
|
||
| it('can extract element in array', () => { | ||
| const key = 'key'; | ||
| const element = { a: 1 }; | ||
| const response = { [key]: [element] }; | ||
|
|
||
| expect(rewriter.extractReponseElement(response, key, 0)).toEqual(element); | ||
| }); | ||
|
|
||
| it('does not fail on null, empty or malformed response', () => { | ||
| const key = 'key'; | ||
|
|
||
| expect(rewriter.extractReponseElement(null, key)).toEqual(null); | ||
| expect(rewriter.extractReponseElement('string', key)).toEqual(null); | ||
| expect(rewriter.extractReponseElement({ a: 1 }, key)).toEqual(null); | ||
| }); | ||
| }); | ||
|
|
||
| describe('rewriteResponseElement', () => { | ||
| const rewriter = new TestRewriter({ fieldName: 'test' }); | ||
|
|
||
| it('can replace element in object', () => { | ||
| const key = 'key'; | ||
| const newElement = { a: 1 }; | ||
| const response = { [key]: 1 }; | ||
|
|
||
| expect(rewriter.rewriteResponseElement(response, newElement, key)).toEqual({ | ||
| [key]: newElement | ||
| }); | ||
| }); | ||
|
|
||
| it('can replace element in array', () => { | ||
| const key = 'key'; | ||
| const newElement = { a: 1 }; | ||
| const response = { [key]: [1] }; | ||
|
|
||
| expect(rewriter.rewriteResponseElement(response, newElement, key, 0)).toEqual({ | ||
| [key]: [newElement] | ||
| }); | ||
| }); | ||
|
|
||
| it('does not fail on null, empty or malformed response', () => { | ||
| const key = 'key'; | ||
| const newElement = { a: 1 }; | ||
|
|
||
| expect(rewriter.rewriteResponseElement(null, newElement, key)).toEqual(null); | ||
| expect(rewriter.rewriteResponseElement('string', newElement, key)).toEqual('string'); | ||
| expect(rewriter.rewriteResponseElement({ a: 1 }, newElement, key)).toEqual({ | ||
| a: 1, | ||
| [key]: newElement | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, I see what you're doing here. We don't have enough info to rewrite child array just on the parent object and the key. I think this is the best fix we can do while keeping the style as close to the current style as possible. 👍