Skip to content

Commit

Permalink
Backport GraphQL-Tag fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 31, 2024
1 parent 0e92930 commit 704c5d9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
84 changes: 83 additions & 1 deletion src/__tests__/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"value": {
"block": true,
"kind": "StringValue",
"value": "block string uses """",
"value": "block string uses """
",
},
},
],
Expand Down Expand Up @@ -768,5 +769,86 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
],
"kind": "Document",
"loc": {
"end": 1257,
"source": {
"body": "# Copyright (c) 2015-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
whoever123is: node(id: [123, 456]) {
id
... on User @onInlineFragment {
field2 {
id
alias: field1(first: 10, after: $foo) @include(if: $foo) {
id
...frag @onFragmentSpread
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}
mutation likeStory @onMutation {
like(story: 123) @onField {
story {
id @onField
}
}
}
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput)
@onSubscription {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}
fragment frag on Friend @onFragmentDefinition {
foo(
size: $site
bar: 12
obj: {
key: "value"
block: """
block string uses \\"""
"""
}
)
}
query teeny {
unnamed(truthy: true, falsey: false, nullish: null)
query
}
query tiny {
__typename
}
",
"locationOffset": {
"column": 1,
"line": 1,
},
"name": "graphql.web",
},
"start": 0,
},
}
`;
24 changes: 21 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ function operationDefinition(
}
}

function document(): ast.DocumentNode {
function document(input: string, noLoc: boolean): ast.DocumentNode {
let match: string | undefined;
let definition: ast.OperationDefinitionNode | undefined;
ignored();
Expand All @@ -498,6 +498,24 @@ function document(): ast.DocumentNode {
throw error('Document');
}
} while (idx < input.length);

if (!noLoc) {
return {
kind: 'Document' as Kind.DOCUMENT,
definitions,
loc: {
start: 0,
end: input.length,
// @ts-ignore
source: {
body: input,
name: 'graphql.web',
locationOffset: { line: 1, column: 1 },
},
},
};
}

return {
kind: 'Document' as Kind.DOCUMENT,
definitions,
Expand All @@ -510,11 +528,11 @@ type ParseOptions = {

export function parse(
string: string | Source,
_options?: ParseOptions | undefined
options?: ParseOptions | undefined
): ast.DocumentNode {
input = typeof string.body === 'string' ? string.body : string;
idx = 0;
return document();
return document(input, options && options.noLocation);
}

export function parseValue(
Expand Down

0 comments on commit 704c5d9

Please sign in to comment.