-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add
loc
getter on fragment documents for graphql-tag
inter-c…
…ompatibility (#396)
- Loading branch information
Showing
4 changed files
with
167 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'gql.tada': patch | ||
--- | ||
|
||
Add `loc` getter to parsed `DocumentNode` fragment outputs to ensure that using fragments created by `gql.tada`'s `graphql()` function with `graphql-tag` doesn't crash. `graphql-tag` does not treat the `DocumentNode.loc` property as optional on interpolations, which leads to intercompatibility issues. |
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,105 @@ | ||
import type { Location } from '@0no-co/graphql.web'; | ||
import { describe, it, expect } from 'vitest'; | ||
import { concatLocSources } from '../utils'; | ||
|
||
const makeLocation = (input: string): Location => ({ | ||
start: 0, | ||
end: input.length, | ||
source: { | ||
body: input, | ||
name: 'GraphQLTada', | ||
locationOffset: { line: 1, column: 1 }, | ||
}, | ||
}); | ||
|
||
describe('concatLocSources', () => { | ||
it('outputs the fragments concatenated to one another', () => { | ||
const actual = concatLocSources([{ loc: makeLocation('a') }, { loc: makeLocation('b') }]); | ||
expect(actual).toBe('ab'); | ||
}); | ||
|
||
it('works when called recursively', () => { | ||
// NOTE: Should work repeatedly | ||
for (let i = 0; i < 2; i++) { | ||
const actual = concatLocSources([ | ||
{ | ||
get loc() { | ||
return makeLocation( | ||
concatLocSources([{ loc: makeLocation('a') }, { loc: makeLocation('b') }]) | ||
); | ||
}, | ||
}, | ||
{ | ||
get loc() { | ||
return makeLocation( | ||
concatLocSources([{ loc: makeLocation('c') }, { loc: makeLocation('d') }]) | ||
); | ||
}, | ||
}, | ||
]); | ||
expect(actual).toBe('abcd'); | ||
} | ||
}); | ||
|
||
it('deduplicates recursively', () => { | ||
// NOTE: Should work repeatedly | ||
for (let i = 0; i < 2; i++) { | ||
const a = { loc: makeLocation('a') }; | ||
const b = { loc: makeLocation('b') }; | ||
const c = { loc: makeLocation('c') }; | ||
const d = { loc: makeLocation('d') }; | ||
|
||
let actual = concatLocSources([ | ||
{ | ||
get loc() { | ||
return makeLocation(concatLocSources([a, b, c, d])); | ||
}, | ||
}, | ||
{ | ||
get loc() { | ||
return makeLocation( | ||
concatLocSources([ | ||
a, | ||
b, | ||
c, | ||
{ | ||
get loc() { | ||
return makeLocation(concatLocSources([a, b, c, d])); | ||
}, | ||
}, | ||
]) | ||
); | ||
}, | ||
}, | ||
]); | ||
|
||
expect(actual).toBe('abcd'); | ||
|
||
actual = concatLocSources([ | ||
{ | ||
get loc() { | ||
return makeLocation( | ||
concatLocSources([ | ||
a, | ||
b, | ||
c, | ||
{ | ||
get loc() { | ||
return makeLocation(concatLocSources([a, b, c, d])); | ||
}, | ||
}, | ||
]) | ||
); | ||
}, | ||
}, | ||
{ | ||
get loc() { | ||
return makeLocation(concatLocSources([a, b, c, d])); | ||
}, | ||
}, | ||
]); | ||
|
||
expect(actual).toBe('abcd'); | ||
} | ||
}); | ||
}); |
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