generated from wayfair-incubator/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from wayfair-incubator/gwardwell_improve_empty…
…_froid_key_errors Improves error messages when an empty key is generated
- Loading branch information
Showing
5 changed files
with
66 additions
and
9 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
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 |
---|---|---|
|
@@ -71,7 +71,7 @@ export class Key { | |
parseableField = Key.getKeyDirectiveFields(parseableField); | ||
} | ||
( | ||
Key.parseKeyFields(parseableField) | ||
Key.parseKeyFields(this.typename, parseableField) | ||
.definitions[0] as OperationDefinitionNode | ||
)?.selectionSet?.selections?.forEach((selection) => | ||
this.addSelection(selection) | ||
|
@@ -174,6 +174,7 @@ export class Key { | |
*/ | ||
public toString(): string { | ||
return Key.getSortedSelectionSetFields( | ||
this.typename, | ||
this._fields.map((field) => field.toString()).join(' ') | ||
); | ||
} | ||
|
@@ -210,10 +211,22 @@ export class Key { | |
* Parses a key fields string into AST. | ||
* | ||
* @param {string} keyFields - The key fields string | ||
Check warning on line 213 in src/schema/Key.ts GitHub Actions / lint
Check warning on line 213 in src/schema/Key.ts GitHub Actions / publish
|
||
* @param {string} typename - The typename of the node the directive belongs to | ||
* @returns {DocumentNode} The key fields represented in AST | ||
*/ | ||
private static parseKeyFields(keyFields: string): DocumentNode { | ||
return parse(`{${keyFields}}`, {noLocation: true}); | ||
private static parseKeyFields( | ||
typename: string, | ||
keyFields: string | ||
): DocumentNode { | ||
try { | ||
return parse(`{${keyFields}}`, {noLocation: true}); | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to parse key fields "${keyFields}" for type "${typename}" due to error: ${ | ||
(error as Error).message | ||
}` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -234,12 +247,18 @@ export class Key { | |
* Sorts the selection set fields. | ||
* | ||
* @param {string} fields - The selection set fields. | ||
Check warning on line 249 in src/schema/Key.ts GitHub Actions / lint
Check warning on line 249 in src/schema/Key.ts GitHub Actions / publish
|
||
* @param {string} typename - The typename of the node the directive belongs to | ||
* @returns {string} The sorted selection set fields. | ||
*/ | ||
public static getSortedSelectionSetFields(fields: string): string { | ||
public static getSortedSelectionSetFields( | ||
typename: string, | ||
fields: string | ||
): string { | ||
const selections = Key.sortSelectionSetByNameAscending( | ||
(Key.parseKeyFields(fields).definitions[0] as OperationDefinitionNode) | ||
.selectionSet | ||
( | ||
Key.parseKeyFields(typename, fields) | ||
.definitions[0] as OperationDefinitionNode | ||
).selectionSet | ||
); | ||
return Key.formatSelectionSetFields(print(selections)); | ||
} | ||
|
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