Skip to content

Commit

Permalink
Add debug (#989)
Browse files Browse the repository at this point in the history
* add debug for schema and operations loading

* add fast-glob

* add changeset

* fix canary release
  • Loading branch information
dimaMachina committed Mar 13, 2022
1 parent b926fb6 commit 3fe3761
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-zebras-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

print debug information with `DEBUG` env variable, example `DEBUG=graphql-eslint:* eslint .`
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: Have a question?
url: https://github.com/dotansimha/graphql-eslint/discussions/new
url: https://github.com/B2o5T/graphql-eslint/discussions/new
about: Not sure about something? need help from the community? have a question to our team? please ask and answer questions here.
4 changes: 2 additions & 2 deletions .github/workflows/canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Release Canary
id: canary
uses: 'kamilkisiela/release-canary@master'
if: github.repository == 'dotansimha/graphql-eslint'
if: github.repository == 'B2o5T/graphql-eslint'
with:
npm-token: ${{ secrets.NODE_AUTH_TOKEN }}
npm-script: 'yarn release:canary'
Expand All @@ -66,4 +66,4 @@ jobs:
The latest changes of this PR are not available as alpha, since there are no linked `changesets` for this PR.
bot-token: ${{ secrets.GH_API_TOKEN }}
bot: 'theguild-bot'
github-token: ${{ secrets.GITHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"@graphql-tools/graphql-tag-pluck": "7.1.5",
"@graphql-tools/utils": "8.6.1",
"chalk": "4.1.2",
"debug": "4.3.3",
"fast-glob": "3.2.11",
"graphql-config": "4.1.0",
"graphql-depth-limit": "1.1.0",
"lodash.lowercase": "4.3.0"
Expand Down
9 changes: 8 additions & 1 deletion packages/plugin/src/graphql-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { dirname } from 'path';
import debugFactory from 'debug';
import { GraphQLConfig, GraphQLExtensionDeclaration, loadConfigSync, SchemaPointer } from 'graphql-config';
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
import { ParserOptions } from './types';
import { dirname } from 'path';

const debug = debugFactory('graphql-eslint:graphql-config');
let graphQLConfig: GraphQLConfig;

export function loadGraphQLConfig(options: ParserOptions): GraphQLConfig {
Expand All @@ -22,6 +24,11 @@ export function loadGraphQLConfig(options: ParserOptions): GraphQLConfig {
extensions: [addCodeFileLoaderExtension],
});

debug('options.skipGraphQLConfig: %o', options.skipGraphQLConfig);
if (onDiskConfig) {
debug('Graphql-config path %o', onDiskConfig.filepath);
}

const configOptions = options.projects
? { projects: options.projects }
: {
Expand Down
17 changes: 8 additions & 9 deletions packages/plugin/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { parseGraphQLSDL } from '@graphql-tools/utils';
import { ASTNode, GraphQLError, TypeInfo, Source } from 'graphql';
import { Linter } from 'eslint';
import debugFactory from 'debug';
import { convertToESTree, extractTokens } from './estree-parser';
import { GraphQLESLintParseResult, ParserOptions, ParserServices } from './types';
import { getSchema } from './schema';
import { getSiblingOperations } from './sibling-operations';
import { loadGraphQLConfig } from './graphql-config';

export function parse(code: string, options?: ParserOptions): Linter.ESLintParseResult['ast'] {
return parseForESLint(code, options).ast;
}
const debug = debugFactory('graphql-eslint:parser');

debug('cwd %o', process.cwd())

export function parseForESLint(code: string, options: ParserOptions = {}): GraphQLESLintParseResult {
const gqlConfig = loadGraphQLConfig(options);
Expand All @@ -36,15 +36,14 @@ export function parseForESLint(code: string, options: ParserOptions = {}): Graph

return {
services: parserServices,
parserServices,
ast: {
type: 'Program',
body: [rootTree as any],
sourceType: 'script',
comments,
tokens,
loc: rootTree.loc,
range: rootTree.range as [number, number],
tokens,
type: 'Program',
sourceType: 'script',
body: [rootTree as any],
},
};
} catch (e) {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { GraphQLSchema } from 'graphql';
import { GraphQLConfig } from 'graphql-config';
import { asArray } from '@graphql-tools/utils';
import debugFactory from 'debug'
import fastGlob from 'fast-glob';
import { ParserOptions } from './types';
import { getOnDiskFilepath, loaderCache, logger } from './utils';

const schemaCache: Map<string, GraphQLSchema> = new Map();
const debug = debugFactory('graphql-eslint:schema')

export function getSchema(options: ParserOptions = {}, gqlConfig: GraphQLConfig): GraphQLSchema | null {
const realFilepath = options.filePath ? getOnDiskFilepath(options.filePath) : null;
Expand All @@ -21,10 +24,18 @@ export function getSchema(options: ParserOptions = {}, gqlConfig: GraphQLConfig)

let schema: GraphQLSchema | null;
try {
debug('Loading schema from %o', projectForFile.schema)
schema = projectForFile.loadSchemaSync(projectForFile.schema, 'GraphQLSchema', {
cache: loaderCache,
...options.schemaOptions,
});
if (debug.enabled) {
debug('Schema loaded: %o', schema instanceof GraphQLSchema);
const schemaPaths = fastGlob.sync(projectForFile.schema as string | string[], {
absolute: true,
});
debug('Schema pointers %O', schemaPaths)
}
} catch (e) {
schema = null;
logger.error('Error while loading schema\n', e);
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin/src/sibling-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import {
} from 'graphql';
import { Source, asArray } from '@graphql-tools/utils';
import { GraphQLConfig } from 'graphql-config';
import debugFactory from 'debug';
import fastGlob from 'fast-glob';
import { ParserOptions } from './types';
import { getOnDiskFilepath, loaderCache, logger } from './utils';

export type FragmentSource = { filePath: string; document: FragmentDefinitionNode };
export type OperationSource = { filePath: string; document: OperationDefinitionNode };

const debug = debugFactory('graphql-eslint:operations');

export type SiblingOperations = {
available: boolean;
getFragment(fragmentName: string): FragmentSource[];
Expand Down Expand Up @@ -61,10 +65,18 @@ const getSiblings = (filePath: string, gqlConfig: GraphQLConfig): Source[] => {
let siblings = operationsCache.get(documentsKey);

if (!siblings) {
debug('Loading operations from %o', projectForFile.documents);
const documents = projectForFile.loadDocumentsSync(projectForFile.documents, {
skipGraphQLImport: true,
cache: loaderCache,
});
if (debug.enabled) {
debug('Loaded %d operations', documents.length);
const operationsPaths = fastGlob.sync(projectForFile.documents as string | string[], {
absolute: true
});
debug('Operations pointers %O', operationsPaths);
}
siblings = handleVirtualPath(documents);
operationsCache.set(documentsKey, siblings);
}
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3314,6 +3314,13 @@ debug@4, debug@^4.1.0, debug@^4.1.1, debug@^4.3.2:
dependencies:
ms "2.1.2"

debug@4.3.3, debug@^4.3.1, debug@^4.3.3:
version "4.3.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.3.tgz#04266e0b70a98d4462e6e288e38259213332b664"
integrity sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==
dependencies:
ms "2.1.2"

debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand All @@ -3328,13 +3335,6 @@ debug@^3.2.7:
dependencies:
ms "^2.1.1"

debug@^4.3.1, debug@^4.3.3:
version "4.3.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.3.tgz#04266e0b70a98d4462e6e288e38259213332b664"
integrity sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==
dependencies:
ms "2.1.2"

decamelize-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/decamelize-keys/-/decamelize-keys-1.1.0.tgz#d171a87933252807eb3cb61dc1c1445d078df2d9"
Expand Down

0 comments on commit 3fe3761

Please sign in to comment.