Skip to content

Commit

Permalink
add config option to show error on invalid schema
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 18, 2023
1 parent 1d28a4d commit a6e5f6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/cm6-graphql/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GraphQLSchema } from 'graphql';
import { ContextToken, CompletionItem } from 'graphql-language-service';
import { Position } from './helpers';
export interface GqlExtensionsOptions {
showErrorOnInvalidSchema?: boolean;
onShowInDocs?: (field?: string, type?: string, parentType?: string) => void;
onFillAllFields?: (
view: EditorView,
Expand Down
12 changes: 11 additions & 1 deletion packages/cm6-graphql/src/lint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Diagnostic, linter } from '@codemirror/lint';
import { getDiagnostics } from 'graphql-language-service';
import { Position, posToOffset } from './helpers';
import { getSchema, optionsStateField, schemaStateField } from './state';
import {
getOpts,
getSchema,
optionsStateField,
schemaStateField,
} from './state';
import { Extension } from '@codemirror/state';
import { validateSchema } from 'graphql';

Expand All @@ -10,11 +15,16 @@ const SEVERITY = ['error', 'warning', 'info'] as const;
export const lint: Extension = linter(
view => {
const schema = getSchema(view.state);
const options = getOpts(view.state);
if (!schema) {
return [];
}
const validationErrors = validateSchema(schema);
if (validationErrors.length) {
if (!options?.showErrorOnInvalidSchema) {
return [];
}

const combinedError = validationErrors.map(error => {
return error.message;
});
Expand Down
9 changes: 8 additions & 1 deletion packages/cm6-graphql/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ export const getOpts = (state: EditorState) => {
return state.field(optionsStateField);
};

const defaultOpts: GqlExtensionsOptions = {
showErrorOnInvalidSchema: true,
};

export const stateExtensions = (
schema?: GraphQLSchema,
opts?: GqlExtensionsOptions,
) => [schemaStateField.init(() => schema), optionsStateField.init(() => opts)];
) => [
schemaStateField.init(() => schema),
optionsStateField.init(() => ({ ...defaultOpts, ...opts })),
];

0 comments on commit a6e5f6e

Please sign in to comment.