-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix linting in query and variable editor (#2427)
* fix linting in query editor * add e2e test suite for query and variable linting * fix error message assertion based on GraphQL version
- Loading branch information
1 parent
90ec49a
commit ebc864f
Showing
7 changed files
with
193 additions
and
1 deletion.
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 @@ | ||
--- | ||
'@graphiql/react': patch | ||
--- | ||
|
||
Mark `graphql` as external dependency to avoid importing multiple instances |
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 @@ | ||
--- | ||
'@graphiql/react': patch | ||
--- | ||
|
||
Fix linting by also updating the options object in the internal codemirror state |
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
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,160 @@ | ||
import { version as graphqlVersion } from 'graphql/version'; | ||
|
||
describe('Linting', () => { | ||
it('Marks GraphQL syntax errors as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
{ | ||
doesNotExist | ||
test { | ||
id | ||
} | ||
+++ | ||
} | ||
`, | ||
}).assertLinterMarkWithMessage( | ||
'+++', | ||
'error', | ||
graphqlVersion.startsWith('15.') | ||
? 'Syntax Error: Cannot parse the unexpected character "+".' | ||
: 'Syntax Error: Unexpected character: "+".', | ||
); | ||
}); | ||
|
||
it('Does not mark valid fields', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
{ | ||
myAlias: id | ||
test { | ||
id | ||
} | ||
} | ||
`, | ||
}) | ||
.contains('myAlias') | ||
.should('not.have.class', 'CodeMirror-lint-mark') | ||
.and('not.have.class', 'CodeMirror-lint-mark-error'); | ||
}); | ||
|
||
it('Marks invalid fields as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
{ | ||
doesNotExist | ||
test { | ||
id | ||
} | ||
} | ||
`, | ||
}).assertLinterMarkWithMessage( | ||
'doesNotExist', | ||
'error', | ||
'Cannot query field "doesNotExist" on type "Test".', | ||
); | ||
}); | ||
|
||
it('Marks deprecated fields as warning', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
{ | ||
id | ||
deprecatedField { | ||
id | ||
} | ||
} | ||
`, | ||
}).assertLinterMarkWithMessage( | ||
'deprecatedField', | ||
'warning', | ||
'The field Test.deprecatedField is deprecated.', | ||
); | ||
}); | ||
|
||
it('Marks syntax errors in variables JSON as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
query WithVariables($stringArg: String) { | ||
hasArgs(string: $stringArg) | ||
} | ||
`, | ||
variablesString: JSON.stringify({ stringArg: '42' }, null, 2).slice( | ||
0, | ||
-1, | ||
), | ||
}).assertLinterMarkWithMessage( | ||
'"42"', | ||
'error', | ||
'Expected } but found [end of file].', | ||
); | ||
}); | ||
|
||
it('Marks unused variables as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
query WithVariables($stringArg: String) { | ||
hasArgs(string: $stringArg) | ||
} | ||
`, | ||
variables: { | ||
stringArg: '42', | ||
unusedVariable: 'whoops', | ||
}, | ||
}).assertLinterMarkWithMessage( | ||
'unusedVariable', | ||
'error', | ||
'Variable "$unusedVariable" does not appear in any GraphQL query.', | ||
); | ||
}); | ||
|
||
it('Marks invalid variable type as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
query WithVariables($stringArg: String) { | ||
hasArgs(string: $stringArg) | ||
} | ||
`, | ||
variables: { | ||
stringArg: 42, | ||
}, | ||
}).assertLinterMarkWithMessage( | ||
'42', | ||
'error', | ||
'Expected value of type "String".', | ||
); | ||
}); | ||
|
||
it('Marks variables with null values for a non-nullable type as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
query WithVariables($stringArg: String!) { | ||
hasArgs(string: $stringArg) | ||
} | ||
`, | ||
variables: { | ||
stringArg: null, | ||
}, | ||
}).assertLinterMarkWithMessage( | ||
'null', | ||
'error', | ||
'Type "String!" is non-nullable and cannot be null.', | ||
); | ||
}); | ||
|
||
it('Marks variables with non-object values for a input object type as error', () => { | ||
cy.visitWithOp({ | ||
query: /* GraphQL */ ` | ||
query WithVariables($objectArg: TestInput) { | ||
hasArgs(object: $objectArg) | ||
} | ||
`, | ||
variables: { | ||
objectArg: '42', | ||
}, | ||
}).assertLinterMarkWithMessage( | ||
'"42"', | ||
'error', | ||
'Type "TestInput" must be an Object.', | ||
); | ||
}); | ||
}); |
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