-
Notifications
You must be signed in to change notification settings - Fork 270
Improve validation of URLPathTemplate and JSONSelection strings passed to @source{Type,Field} directives
#2926
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
Draft
benjamn
wants to merge
6
commits into
benjamn/fix-@sourceField-validation-on-extend-type
Choose a base branch
from
benjamn/validate-URLPathTemplate-and-JSONSelection
base: benjamn/fix-@sourceField-validation-on-extend-type
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b596b71
URLPathTemplate parser
benjamn c04261a
Add EBNF parser for JSONSelection string syntax.
benjamn b6e3ba0
getSelectionOutputShape
benjamn 076e3e6
Report parseJSONSelection errors during validation.
benjamn cd86214
Create EBNF parsers lazily (not in module scope).
benjamn 0fc0d11
Teach cspell the acronym EBNF.
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ Didnt | |
| diretive | ||
| doesn | ||
| Dont | ||
| ebnf | ||
| effecient | ||
| elimiate | ||
| elts | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,309 @@ | ||
| import { sourceIdentity } from '../index'; | ||
| import { | ||
| sourceIdentity, | ||
| parseJSONSelection, | ||
| getSelectionOutputShape, | ||
| parseURLPathTemplate, | ||
| getURLPathTemplateVars, | ||
| } from '../index'; | ||
|
|
||
| describe('SourceSpecDefinition', () => { | ||
| it('should export expected identity URL', () => { | ||
| expect(sourceIdentity).toBe('https://specs.apollo.dev/source'); | ||
| }); | ||
| }); | ||
|
|
||
| function parseSelectionExpectingNoErrors(selection: string) { | ||
| const ast = parseJSONSelection(selection)!; | ||
| expect(ast.errors).toEqual([]); | ||
| return ast; | ||
| } | ||
|
|
||
| describe('parseJSONSelection', () => { | ||
| it('parses simple selections', () => { | ||
| expect(parseSelectionExpectingNoErrors('a')!.type).toBe('Selection'); | ||
| expect(parseSelectionExpectingNoErrors('a b')!.type).toBe('Selection'); | ||
| expect(parseSelectionExpectingNoErrors('a b { c }')!.type).toBe('Selection'); | ||
| expect(parseSelectionExpectingNoErrors('.a')!.type).toBe('Selection'); | ||
| expect(parseSelectionExpectingNoErrors('.a.b.c')!.type).toBe('Selection'); | ||
| }); | ||
|
|
||
| const complexSelection = ` | ||
| # Basic field selection. | ||
| foo | ||
|
|
||
| # Similar to a GraphQL alias with a subselection. | ||
| barAlias: bar { x y z } | ||
|
|
||
| # Similar to a GraphQL alias without a subselection, but allowing for JSON | ||
| # properties that are not valid GraphQL Name identifiers. | ||
| quotedAlias: "string literal" { nested stuff } | ||
|
|
||
| # Apply a subselection to the result of extracting .foo.bar, and alias it. | ||
| pathAlias: .foo.bar { a b c } | ||
|
|
||
| # Nest various fields under a new key (group). | ||
| group: { foo baz: bar { x y z } } | ||
|
|
||
| # Get the first event from events and apply a selection and an alias to it. | ||
| firstEvent: .events.0 { id description } | ||
|
|
||
| # Apply the { nested stuff } selection to any remaining properties and alias | ||
| # the result as starAlias. Note that any * selection must appear last in the | ||
| # sequence of named selections, and will be typed as JSON regardless of what | ||
| # is subselected, because the field names are unknown. | ||
| starAlias: * { nested stuff } | ||
| `; | ||
| // TODO Improve error message when other named selections accidentally follow | ||
| // a * selection. | ||
|
|
||
| it('parses a multiline selection with comments', () => { | ||
| expect(parseSelectionExpectingNoErrors(complexSelection)!.type).toBe('Selection'); | ||
| }); | ||
|
|
||
| describe('getSelectionOutputShape', () => { | ||
| it('returns the correct output shape for a simple selection', () => { | ||
| const ast = parseSelectionExpectingNoErrors('a'); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for a'); | ||
| } | ||
| expect(getSelectionOutputShape(ast)).toEqual({ | ||
| a: 'JSON', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns the correct output shape for a complex selection', () => { | ||
| const ast = parseSelectionExpectingNoErrors(complexSelection); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + complexSelection); | ||
| } | ||
| expect(getSelectionOutputShape(ast)).toEqual({ | ||
| foo: 'JSON', | ||
| barAlias: { | ||
| x: 'JSON', | ||
| y: 'JSON', | ||
| z: 'JSON', | ||
| }, | ||
| quotedAlias: { | ||
| nested: 'JSON', | ||
| stuff: 'JSON', | ||
| }, | ||
| pathAlias: { | ||
| a: 'JSON', | ||
| b: 'JSON', | ||
| c: 'JSON', | ||
| }, | ||
| group: { | ||
| foo: 'JSON', | ||
| baz: { | ||
| x: 'JSON', | ||
| y: 'JSON', | ||
| z: 'JSON', | ||
| }, | ||
| }, | ||
| starAlias: 'JSON', | ||
| firstEvent: { | ||
| id: 'JSON', | ||
| description: 'JSON', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns the correct output shape for a selection with nested fields', () => { | ||
| const ast = parseSelectionExpectingNoErrors(` | ||
| a | ||
| b { c d } | ||
| e { f { g h } } | ||
| i { j { k l } } | ||
| m { n o { p q } } | ||
| # stray comment | ||
| r { s t { u v } } | ||
| w { x { y z } } | ||
| `); | ||
|
|
||
| if (!ast) { | ||
| throw new Error('Generic parse failure for alphabet soup'); | ||
| } | ||
|
|
||
| expect(getSelectionOutputShape(ast)).toEqual({ | ||
| a: 'JSON', | ||
| b: { | ||
| c: 'JSON', | ||
| d: 'JSON', | ||
| }, | ||
| e: { | ||
| f: { | ||
| g: 'JSON', | ||
| h: 'JSON', | ||
| }, | ||
| }, | ||
| i: { | ||
| j: { | ||
| k: 'JSON', | ||
| l: 'JSON', | ||
| }, | ||
| }, | ||
| m: { | ||
| n: 'JSON', | ||
| o: { | ||
| p: 'JSON', | ||
| q: 'JSON', | ||
| }, | ||
| }, | ||
| r: { | ||
| s: 'JSON', | ||
| t: { | ||
| u: 'JSON', | ||
| v: 'JSON', | ||
| }, | ||
| }, | ||
| w: { | ||
| x: { | ||
| y: 'JSON', | ||
| z: 'JSON', | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parseURLPathTemplate', () => { | ||
| it('allows an empty path', () => { | ||
| const template = '/'; | ||
| const ast = parseURLPathTemplate(template); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + template); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| expect(getURLPathTemplateVars(ast)).toEqual({}); | ||
| }); | ||
|
|
||
| it('allows query params only', () => { | ||
| const template = '/?param={param}&other={other}'; | ||
| const ast = parseURLPathTemplate(template); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + template); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(Object.keys(vars).sort()).toEqual([ | ||
| 'other', | ||
| 'param', | ||
| ]); | ||
| }); | ||
|
|
||
| it('allows empty query parameters after a /?', () => { | ||
| const template = '/?'; | ||
| const ast = parseURLPathTemplate(template); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + template); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| expect(getURLPathTemplateVars(ast)).toEqual({}); | ||
| }); | ||
|
|
||
| it('allows valueless keys in query parameters', () => { | ||
| const template = '/?a&b=&c&d=&e'; | ||
| const ast = parseURLPathTemplate(template); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + template); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(Object.keys(vars).sort()).toEqual([]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| '/users/{userId}/posts/{postId}', | ||
| '/users/{userId}/posts/{postId}/', | ||
| '/users/{userId}/posts/{postId}/junk', | ||
| ] as const)('parses path-only templates with variables: %s', pathTemplate => { | ||
| const ast = parseURLPathTemplate(pathTemplate); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + pathTemplate); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(Object.keys(vars).sort()).toEqual([ | ||
| 'postId', | ||
| 'userId', | ||
| ]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| '/users/{user.id}/posts/{post.id}', | ||
| '/users/{user.id}/posts/{post.id}/', | ||
| '/users/{user.id}/posts/{post.id}/junk', | ||
| ] as const)('parses path template with nested vars: %s', pathTemplate => { | ||
| const ast = parseURLPathTemplate(pathTemplate); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + pathTemplate); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(Object.keys(vars).sort()).toEqual([ | ||
| 'post.id', | ||
| 'user.id', | ||
| ]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| '/users/{user.id}?param={param}', | ||
| '/users/{user.id}/?param={param}', | ||
| '/users/{user.id}/junk?param={param}', | ||
| '/users/{user.id}/{param}?', | ||
| ] as const)('parses templates with query parameters: %s', pathTemplate => { | ||
| const ast = parseURLPathTemplate(pathTemplate); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + pathTemplate); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(Object.keys(vars).sort()).toEqual([ | ||
| 'param', | ||
| 'user.id', | ||
| ]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| '/location/{latitude},{longitude}?filter={filter}', | ||
| '/location/{latitude},{longitude}/?filter={filter}', | ||
| '/location/{latitude},{longitude}/junk?filter={filter}', | ||
| '/location/lat:{latitude},lon:{longitude}?filter={filter}', | ||
| '/location/lat:{latitude},lon:{longitude}/?filter={filter!}', | ||
| '/location/lat:{latitude},lon:{longitude}/junk?filter={filter!}', | ||
| '/?lat={latitude}&lon={longitude}&filter={filter}', | ||
| '/?location={latitude},{longitude}&filter={filter}', | ||
| '/?filter={filter}&location={latitude!}-{longitude!}', | ||
| ] as const)('should parse a template with multi-var segments: %s', pathTemplate => { | ||
| const ast = parseURLPathTemplate(pathTemplate); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + pathTemplate); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(Object.keys(vars).sort()).toEqual([ | ||
| 'filter', | ||
| 'latitude', | ||
| 'longitude', | ||
| ]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| '/users?ids={uid,...}&filter={filter}', | ||
| '/users_batch/{uid,...}?filter={filter}', | ||
| ] as const)('can parse batch endpoints: %s', pathTemplate => { | ||
| const ast = parseURLPathTemplate(pathTemplate); | ||
| if (!ast) { | ||
| throw new Error('Generic parse failure for ' + pathTemplate); | ||
| } | ||
| expect(ast.errors).toEqual([]); | ||
| const vars = getURLPathTemplateVars(ast); | ||
| expect(vars).toEqual({ | ||
| uid: { | ||
| batchSep: ',', | ||
| }, | ||
| filter: {}, | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The output shape of a
JSONSelectionstring is a tree of nested JSON dictionaries mirroring the structure of a GraphQL query, including scalar properties as leaf nodes. The ultimate goal is to validate this structure against object fields to ensure all fields in the schema are handled by someJSONSelectionmapping.These
'JSON'leaf values could have been any placeholder value, but I wanted to emphasize we can't assume much about the types of leaf fields withinJSONSelectionoutput shapes, except that they must be some JSON value (including possiblynullfor nullable fields).