Skip to content

fix(parser): fix programattic api and allow to pass inline string #2902

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

Merged
merged 4 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/hot-worms-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@graphql-eslint/eslint-plugin': minor
---

Improve `parseForESLint` API and allow to pass context schema as inline string.

You can now use it this way:

```ts
parseForESLint(code, { schemaSdl: 'type Query { foo: String }', filePath: 'test.graphql' });
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"packageManager": "[email protected]",
"engines": {
"node": ">=16",
"pnpm": ">=9.0.6"
"pnpm": ">=10.6"
},
"scripts": {
"build": "turbo run build --filter=!website && attw --pack packages/plugin/dist",
Expand Down
352 changes: 352 additions & 0 deletions packages/plugin/__tests__/__snapshots__/parser.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,355 @@ exports[`Parser > parseForESLint() should return ast and tokens 1`] = `
type: Program,
}
`;

exports[`Parser > should allow to pass inline schema string as input 1`] = `
{
body: [
{
definitions: [
{
description: undefined,
directives: [],
fields: [
{
arguments: [],
description: undefined,
directives: [],
gqlType: {
kind: NamedType,
leadingComments: [],
loc: {
end: {
column: 19,
line: 3,
},
source:
type Query {
foo: String
}
,
start: {
column: 13,
line: 3,
},
},
name: {
kind: Name,
leadingComments: [],
loc: {
end: {
column: 19,
line: 3,
},
source:
type Query {
foo: String
}
,
start: {
column: 13,
line: 3,
},
},
range: [
33,
39,
],
rawNode: [Function],
type: Name,
typeInfo: [Function],
value: String,
},
range: [
33,
39,
],
rawNode: [Function],
type: NamedType,
typeInfo: [Function],
},
kind: FieldDefinition,
leadingComments: [],
loc: {
end: {
column: 13,
line: 3,
},
source:
type Query {
foo: String
}
,
start: {
column: 8,
line: 3,
},
},
name: {
kind: Name,
leadingComments: [],
loc: {
end: {
column: 11,
line: 3,
},
source:
type Query {
foo: String
}
,
start: {
column: 8,
line: 3,
},
},
range: [
28,
31,
],
rawNode: [Function],
type: Name,
typeInfo: [Function],
value: foo,
},
range: [
28,
39,
],
rawNode: [Function],
type: FieldDefinition,
typeInfo: [Function],
},
],
interfaces: [],
kind: ObjectTypeDefinition,
leadingComments: [],
loc: {
end: {
column: 46,
line: 4,
},
source:
type Query {
foo: String
}
,
start: {
column: 6,
line: 2,
},
},
name: {
kind: Name,
leadingComments: [],
loc: {
end: {
column: 16,
line: 2,
},
source:
type Query {
foo: String
}
,
start: {
column: 11,
line: 2,
},
},
range: [
12,
17,
],
rawNode: [Function],
type: Name,
typeInfo: [Function],
value: Query,
},
range: [
7,
47,
],
rawNode: [Function],
type: ObjectTypeDefinition,
typeInfo: [Function],
},
],
kind: Document,
leadingComments: [],
loc: {
end: {
column: 4,
line: 5,
},
source:
type Query {
foo: String
}
,
start: {
column: 0,
line: 1,
},
},
range: [
0,
52,
],
rawNode: [Function],
type: Document,
typeInfo: [Function],
},
],
comments: [],
loc: {
end: {
column: 4,
line: 5,
},
source:
type Query {
foo: String
}
,
start: {
column: 0,
line: 1,
},
},
range: [
0,
52,
],
sourceType: script,
tokens: [
{
loc: {
end: {
column: 10,
line: 2,
},
start: {
column: 6,
line: 2,
},
},
range: [
7,
11,
],
type: Name,
value: type,
},
{
loc: {
end: {
column: 16,
line: 2,
},
start: {
column: 11,
line: 2,
},
},
range: [
12,
17,
],
type: Name,
value: Query,
},
{
loc: {
end: {
column: 18,
line: 2,
},
start: {
column: 17,
line: 2,
},
},
range: [
18,
19,
],
type: {,
value: undefined,
},
{
loc: {
end: {
column: 11,
line: 3,
},
start: {
column: 8,
line: 3,
},
},
range: [
28,
31,
],
type: Name,
value: foo,
},
{
loc: {
end: {
column: 12,
line: 3,
},
start: {
column: 11,
line: 3,
},
},
range: [
31,
32,
],
type: :,
value: undefined,
},
{
loc: {
end: {
column: 19,
line: 3,
},
start: {
column: 13,
line: 3,
},
},
range: [
33,
39,
],
type: Name,
value: String,
},
{
loc: {
end: {
column: 7,
line: 4,
},
start: {
column: 6,
line: 4,
},
},
range: [
46,
47,
],
type: },
value: undefined,
},
],
type: Program,
}
`;
12 changes: 12 additions & 0 deletions packages/plugin/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { parseForESLint } from '../src/parser.js';

describe('Parser', () => {
it('should allow to pass inline schema string as input', () => {
const code = /* GraphQL */ `
type Query {
foo: String
}
`;

const result = parseForESLint(code, { schemaSdl: code, filePath: 'test.graphql' });
expect(result.ast).toMatchSnapshot();
expect(result.ast.tokens).toBeTruthy();
});

it('parseForESLint() should return ast and tokens', () => {
const code = /* GraphQL */ `
"""
Expand Down
Loading
Loading