npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eslint-ast/eslint-plugin-graphql

v2.0.0

Published

Provides native eslint support for graphql

Downloads

904

Readme

@eslint-ast/eslint-plugin-graphql

Provides native eslint support for graphql

This project converts graphql AST into an AST eslint supports, and then provides native eslint functionality for both authoring new graphql validation rules, and running graphql's provided validation rules.

Usage

yarn add @eslint-ast/eslint-plugin-graphql
# Versions of eslint prior to 7.0.0 are not supported
yarn add eslint@^7.0.0
// .eslintrc.js
module.exports = {
  root: true,
  overrides: [
    {
      files: '**/*.graphql',
      parserOptions: {
        schema: `${__dirname}/path/to/schema.graphql`,
      },
      extends: ['plugin:eslint-ast/graphql/recommended']
    },
  ],
};

Full support for eslint-disable comments

# eslint-disable <rule-name>, <rule-2-name>, ... <rule-n-name>
# eslint-enable <rule-name>, <rule-2-name>, ... <rule-n-name>
# eslint-disable-line
# eslint-disable-line <rule-name>, <rule-2-name>, ... <rule-n-name>
# eslint-disable-next-line
# eslint-disable-next-line <rule-name>, <rule-2-name>, ... <rule-n-name>

For our built-in rules, they will have the name structure:

# eslint-disable @eslint-ast/graphql/single-top-level-query

For rules, we run from graphql's own library they will have the following naming convention

# eslint-disable @eslint-ast/graphql/ExecutableDefinitionsRule

Parser Util Functions

The parser provides useful util functions you can use while writing your rules. You can access these in your create function via context.parserServices.*.

Util Functions

| Function Name| Description | | :----------- | :----------- | | getSchema() | Get the schema source that was configured with the parser in .eslintrc.js | | getDocument() | Returns eslint node of the entire graphql document ast | | getGraphQL() | Get the graphql instance from require('graphql') used in the parser. See #27 for more information on why this is useful | | createTypeInfo() | Returns a instance of TypeInfo from graphql library | | parse(:string) | Parse the given graphql source into an eslint ast. | | pathOf(:object) | Return a string which is the path to a nested projection. Such as query/grandParent/parent/child | | correspondingNode(:object) | Given a graphql node, will return eslint node. Given an eslint node, will return graphql node. | | toEslintAST(:object) | Takes a graphql ast and converts it into a eslint ast | | getFragmentDefinitionsFromSource(:string) | Returns array of FragmentDefinition from a given query source. |

Usage Example

module.exports = {
  // ... rule metadata properties

  create(context) {
    const typeInfo = context.parserServices.createTypeInfo();

    return {
      '*'(node) {
        typeInfo.enter(context.parserServices.correspondingNode(node));
      },
      '*:exit'(node) {
        typeInfo.leave(context.parserServices.correspondingNode(node));
      },

      SelectionSet(node) {
        let parentType = typeInfo.getParentType();

      },
    };
  },
}

vim-coc users

  1. open :CocLocalConfig
  2. add 'graphql' to 'eslint.filetypes'

If you prefer, you can add this to your global coc config via :CocConfig.

vscode users

  1. Install Extensions
  • GraphQL
  • ESLint
  1. Enable linting on GraphQL in your settings.json and add graphql to the list of filetypes in eslint.validate
{
    "eslint.validate": [
      // ... other files
      "graphql"
    ],
}

List Of Validations:

This project includes some of its own rules:

  • single-top-level-query

This project supports and executes all of graphql's own validations as of version 15.3.0; they are:

  • ExecutableDefinitionsRule
  • UniqueOperationNamesRule
  • LoneAnonymousOperationRule
  • SingleFieldSubscriptionsRule
  • KnownTypeNamesRule
  • FragmentsOnCompositeTypesRule
  • VariablesAreInputTypesRule
  • ScalarLeafsRule
  • FieldsOnCorrectTypeRule
  • UniqueFragmentNamesRule
  • KnownFragmentNamesRule
  • NoUnusedFragmentsRule
  • PossibleFragmentSpreadsRule
  • NoFragmentCyclesRule
  • UniqueVariableNamesRule
  • NoUndefinedVariablesRule
  • NoUnusedVariablesRule
  • KnownDirectivesRule
  • UniqueDirectivesPerLocationRule
  • KnownArgumentNamesRule
  • UniqueArgumentNamesRule
  • ValuesOfCorrectTypeRule
  • ProvidedRequiredArgumentsRule
  • VariablesInAllowedPositionRule
  • OverlappingFieldsCanBeMergedRule
  • UniqueInputFieldNamesRule

Troubleshooting

Parsing Error

If you see errors like

/Users/me/src/project/query.graphql
  1:6  error  Parsing error: ';' expected

You are likely parsing .graphql files as if they were JavaScript and need to set the parser option in your .eslintrc. See the Usage section for details.

Strange Crashes

If you see strange crashes similar to:

TypeError: Cannot read property 'internal' of null
Occurred while linting /Users/me/src/project/query.graphql:0

You may be on an older version of eslint. [email protected] or higher is required.