Giter VIP home page Giter VIP logo

dimamachina / graphql-eslint Goto Github PK

View Code? Open in Web Editor NEW
781.0 7.0 103.0 9.17 MB

ESLint parser, plugin and set rules for GraphQL (for schema and operations). Easily customizable with custom rules. Integrates with IDEs and modern GraphQL tools.

Home Page: https://the-guild.dev/graphql/eslint

License: MIT License

JavaScript 1.31% TypeScript 93.41% Shell 0.13% MDX 5.15%
eslint eslint-plugin graphql linter eslint-parser eslint-rules graphql-rules eslint-graphql

graphql-eslint's Introduction

GraphQL-ESLint

npm version

Documentation

https://the-guild.dev/graphql/eslint

Contributions

Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!

And if this is your first time contributing to this project, please do read our Contributor Workflow Guide before you get started off.

Code of Conduct

Help us keep GraphQL ESLint open and inclusive. Please read and follow our Code of Conduct as adopted from Contributor Covenant.

License

Released under the MIT license.

graphql-eslint's People

Contributors

ardatan avatar berger92 avatar comatory avatar dependabot[bot] avatar dimamachina avatar dnalborczyk avatar dotansimha avatar eddeee888 avatar emrysmyrddin avatar enisdenjo avatar floedelmann avatar gilgardosh avatar github-actions[bot] avatar godsenal avatar henryqdineen avatar jounqin avatar justintross avatar kamilkisiela avatar mderriey avatar nishtahir avatar renovate[bot] avatar rtrembecky avatar saihaj avatar theguild-bot avatar tobiasdiez avatar tshedor avatar tuvalsimha avatar tvvignesh avatar urigo avatar yassineldeeb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

graphql-eslint's Issues

Instead of using custom parser - use original parser from `graphql-js`

Right now I wrote a custom parser that creates ESTree compatible tree.
The main difference here the kind => type and description => comments.
I think it might be better to do:

  1. parse with graphql-js (or graphql-tools)
  2. visit all nodes and convert to ESTree syntax, also include type-info from TypeInfo when schema is available
  3. return ESTree nodes with complete type-info and also the original node.

Something like that:

type ESTreeNode = Omit<DocumentNode, "kind", "description"> & { type: string, description: description, rawNode: DocumentNode, typeInfo: TypeInfo  }

New rule: force union result type for mutations

This way, we can force things like:

type Mutation {
   doSomething: Result
}

union Result = OkResult | ErrorResult

type OkResult {
  updated: Something
  effectedSomethingElse: SomethingElse
}

type ErrorResult {
  message: String!
  reason: String
}

Refactoring suggestions

Idea: find similar things and suggest refactorings. Is it possible?

E.g. find similar types and suggest to extract them into interface, find similar sets of fields in operations and suggest to extract them into fragments, find similar input values and suggest to extract them into variables.

Or vice versa: find fragments and variables that used only in one place and suggest to inline them.

let `require-id-when-available` validate when fragment is present

consider

fragment a on A {
  id
  aField
}
# import "./a.fragment.graphql"
query AQuery {
  as { ...a }
}

Field "id" must be selected when it's available on a type. Please make sure to include it in your selection set!eslint@graphql-eslint/require-id-when-available

require-id-when-available should allow fields which contain (only?) fragment spreads. Bonus points if the fragment themselves validate as having id

parseForESLint performance

Running graphql-eslint on our projects is slower than expected. I did some profiling and noticed that loadSchemaSync() is responsible for several seconds on execution time. I added some naive memoization to that code (just based on config.schema) and the performance improved.

Do you think this memoization is something we could support? Would it cause any issues? Should there be an option to enable the memoization?

I can make a PR if needed but I wanted to make sure it wouldn't break any assumption. If memoization can be based on config.schema it should be straightforward but I notice that the config is being spread into loadSchemaSync() options and changes every time. Is there any overlap between ParserOptions and LoadSchemaOptions? It wasn't obvious to me.

https://github.com/dotansimha/graphql-eslint/blob/15176710527dd85621ebebde3cd0d0d30b7ca584/packages/plugin/src/parser.ts#L93-L94

Thanks!

Parsing error: Loader Url has no sync mode

Hi. I was trying out this eslint plugin today and I am facing this error in all my GQL files both when linting via the CLI as well as the VSCode extension.

1

This error is thrown for every GQL file I use.

I tried to isolate the problem by removing all other eslint rules and just having this plugin around and still had this issue.

My graphql-config is configured correctly cause both the codegen and the VSCode GraphQL extension which uses it are working correctly. I am using multiple projects with both remote and local schema which works well with codegen and extension.

I also noticed other people reporting similar error but with apollo eslint rather:

apollographql/eslint-plugin-graphql#287
apollographql/eslint-plugin-graphql#270 (comment)

Any clues? Thanks.

My .eslintrc file:

{
  "overrides": [
    {
      "files": ["*.gql"],
      "parser": "@graphql-eslint/eslint-plugin",
      "plugins": ["@graphql-eslint"],
      "rules": {
        "avoid-operation-name-prefix": [
          "error",
          { "keywords": "get" }
        ],
        "input-name": [
          "error",
          { "checkInputType": true }
          ],
          "description-style": [
          "error",
          { "style": "block" }
          ],
          "naming-convention": [
          "error",
          {
            "FieldDefinition": "camelCase",
            "ObjectTypeDefinition": "PascalCase"
          }
          ],
          "no-anonymous-operations": ["error"],
          "require-deprecation-reason": "error",
          "no-case-insensitive-enum-values-duplicates": ["error"],
          "no-operation-name-suffix": ["error"],
          "require-id-when-available": ["error",
          {
            "fieldName": "id"
          }
          ],
          "validate-against-schema": ["error"],
          "require-description": [
            "error",
            { "on": ["ObjectTypeDefinition", "FieldDefinition"] }
          ]
      }
    }
  ]
}

My package.json

"name":"...",
"version":"...",
"scripts": {
    "lint": "eslint --ext=.gql ./packages",
}
"devDependencies": {
    "@graphql-codegen/add": "^2.0.1",
    "@graphql-codegen/cli": "^1.17.10",
    "@graphql-codegen/typed-document-node": "^1.17.9",
    "@graphql-codegen/typescript": "^1.17.10",
    "@graphql-codegen/typescript-operations": "^1.17.8",
    "@graphql-codegen/typescript-resolvers": "^1.17.10",
    "@graphql-eslint/eslint-plugin": "^0.4.1",
    "@graphql-typed-document-node/core": "^3.1.0",
    "@types/body-parser": "^1.19.0",
    "@types/express": "^4.17.8",
    "@types/node": "^14.11.8",
    "@types/qs": "6.9.5",
    "@typescript-eslint/eslint-plugin": "^4.4.1",
    "@typescript-eslint/parser": "^4.4.1",
    "concurrently": "^5.3.0",
    "eslint": "^7.11.0",
    "globby": "^11.0.1",
    "morgan": "^1.10.0",
    "nodemon": "^2.0.5",
    "plop": "^2.7.4",
    "rimraf": "^3.0.2",
    "typedoc": "^0.19.2",
    "typescript": "4.0.3",
    "utility-types": "^3.10.0"
  },
  "dependencies": {
    "graphql": "^15.4.0",
    "graphql-modules": "^1.0.0-alpha.2"
  }

CC: @dotansimha

Getting ESLint errors about missing files

I'm trying to add this plugin to my project, I followed the standard configuration in the documentation:

image

But am getting the following error in VSCode's ESLint output:

[Info  - 9:40:12 AM] ESLint server is starting
[Info  - 9:40:12 AM] ESLint server running in node v12.14.1
[Info  - 9:40:12 AM] ESLint server is running.
[Info  - 9:42:43 AM] ESLint library loaded from: /Users/peterp/x/redwoodjs/example-invoice/node_modules/eslint/lib/api.js
[Info  - 9:42:56 AM] ENOTDIR: not a directory, stat '/Users/peterp/x/redwoodjs/example-invoice/web/src/index.js/0_/Users/peterp/x/redwoodjs/example-invoice/web/src/index.js' Occurred while linting /Users/peterp/x/redwoodjs/example-invoice/web/src/index.js/0_/Users/peterp/x/redwoodjs/example-invoice/web/src/index.js:1
[Info  - 9:42:56 AM] ENOTDIR: not a directory, stat '/Users/peterp/x/redwoodjs/example-invoice/web/src/pages/InvoicePage/subcomponents/InvoiceCell/InvoiceCell.js/0_document.graphql' Occurred while linting /Users/peterp/x/redwoodjs/example-invoice/web/src/pages/InvoicePage/subcomponents/InvoiceCell/InvoiceCell.js/0_document.graphql:0

I've pushed up a branch here: https://github.com/redwoodjs/example-invoice/pull/39/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2

Support Graphql-Tag in code files

This one is a bit tricky, since it requires to run multiple parsers on files, need to check that ESLint supports that.
Also, we need to make sure to mark ranges and offsets correctly within the file.

GitHub/CI bot

What about GitHub/CI action/app like GraphQL Inspector?

As an idea, it could be called GraphQL Doctor.

GraphQL Inspector is about detecting changes and preventing breaking changes in GraphQL schema.

GraphQL Doctor is about validating and force best design practices in GraphQL schema.

Docs & README

  • How to use
  • How to use with other parsers together
  • How to write custom rules
  • How to visualize in VSCode

UPPER_CASE and snake_case naming conventions prevent numbers from being used

Because of the regexes used, numbers can't be used with UPPER_CASE and snake_case formats :

https://github.com/dotansimha/graphql-eslint/blob/ee9500dadffe0bd5e5957f0b852072a439132416/packages/plugin/src/rules/naming-convention.ts#L4-L9

For instance, my_s3xy_string would fail snake_case naming convention, as would MY_S3XY_STRING for the UPPER_CASE one. We don't use snake_case, but we have a bunch of uppercase enums with numbers in them, so spotting this problem was pretty obvious in our codebase.

Thanks a lot for the effort put into this library, looking forward to seeing it grow among the community ๐Ÿ’ช

Allow to access all loaded operation files

Some rules might needs access to other loaded operations - maybe we can have a util on the context level for getting a fragment by name or something like that.

This might be done using ESLint as core, since we need to make sure to load the same files ESLint loaded (as defined in the configuration - exclude, extension and so on)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.