How to use graphql-language-service-utils - 10 common examples

To help you get started, we’ve selected a few graphql-language-service-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hasura / graphqurl / src / ui.js View on Github external
const tk = require('terminal-kit');
const {introspectionQuery, buildClientSchema, parse} = require('graphql');
const {cli} = require('cli-ux');
const query = require('./query');
const {validateQuery, getAutocompleteSuggestions} = require('graphql-language-service-interface');
const {Position} = require('graphql-language-service-utils');

// FIXME: needs js idiomatic refactor eslint-disable-line no-warning-comments

var term = tk.terminal;

let qs = '';
let p = new Position(1, 0);
let ib;
let qReady = false;
let schema;
let menuOn = false;
let exit = false;
let gResolve, gReject;

const terminate = error => {
  term.nextLine(1);
  term.grabInput(false);
  term.fullscreen(false);
  if (error) {
    gReject(error);
    return;
  }
  gResolve(qs);
github graphql / graphiql / src / interfaces / getDefinition.js View on Github external
function getDefinitionForFragmentDefinition(
  path: Uri,
  text: string,
  definition: FragmentDefinitionNode,
): Definition {
  return {
    path,
    position: offsetToPosition(text, definition.name.loc.start),
    range: locToRange(text, definition.loc),
    name: definition.name.value,
    language: LANGUAGE,
    // This is a file inside the project root, good enough for now
    projectRoot: path,
  };
}
github graphql / graphiql / packages / graphql-language-service-interface / src / getDiagnostics.ts View on Github external
? node.name
        : 'variable' in node
        ? node.variable
        : node;

    invariant(error.locations, 'GraphQL validation error requires locations.');
    // @ts-ignore
    // https://github.com/microsoft/TypeScript/pull/32695
    const loc = error.locations[0];
    const highlightLoc = getLocation(highlightNode);
    const end = loc.column + (highlightLoc.end - highlightLoc.start);
    return {
      source: `GraphQL: ${type}`,
      message: error.message,
      severity,
      range: new Range(
        new Position(loc.line - 1, loc.column - 1),
        new Position(loc.line - 1, end),
      ),
    };
  });
}
github graphql / graphiql / packages / graphql-language-service-interface / src / getDiagnostics.ts View on Github external
const style = parser.token(stream, state);
      if (style === 'invalidchar') {
        break;
      }
    }
  }

  invariant(stream, 'Expected Parser stream to be available.');
  const line = location.line - 1;
  // @ts-ignore
  // https://github.com/microsoft/TypeScript/pull/32695
  const start = stream.getStartOfToken();
  // @ts-ignore
  // https://github.com/microsoft/TypeScript/pull/32695
  const end = stream.getCurrentPosition();
  return new Range(new Position(line, start), new Position(line, end));
}
github graphql / graphiql / src / interfaces / getDefinition.js View on Github external
function getDefinitionForFragmentDefinition(
  path: Uri,
  text: string,
  definition: FragmentDefinitionNode,
): Definition {
  return {
    path,
    position: offsetToPosition(text, definition.name.loc.start),
    range: locToRange(text, definition.loc),
    name: definition.name.value,
    language: LANGUAGE,
    // This is a file inside the project root, good enough for now
    projectRoot: path,
  };
}
github graphql / graphiql / packages / graphql-language-service-interface / src / getDiagnostics.ts View on Github external
export function validateQuery(
  ast: DocumentNode,
  schema: GraphQLSchema | null | undefined = null,
  customRules?: Array,
  isRelayCompatMode?: boolean,
): Array {
  // We cannot validate the query unless a schema is provided.
  if (!schema) {
    return [];
  }

  const validationErrorAnnotations = mapCat(
    validateWithCustomRules(schema, ast, customRules, isRelayCompatMode),
    error => annotations(error, SEVERITY.ERROR, 'Validation'),
  );

  // Note: findDeprecatedUsages was added in graphql@0.9.0, but we want to
  // support older versions of graphql-js.
  const deprecationWarningAnnotations = !findDeprecatedUsages
    ? []
    : mapCat(findDeprecatedUsages(schema, ast), error =>
        annotations(error, SEVERITY.WARNING, 'Deprecation'),
      );

  return validationErrorAnnotations.concat(deprecationWarningAnnotations);
}
github graphql / graphiql / packages / graphql-language-service-interface / src / GraphQLLanguageService.ts View on Github external
} catch (error) {
      // the query string is already checked to be parsed properly - errors
      // from this parse must be from corrupted fragment dependencies.
      // For IDEs we don't care for errors outside of the currently edited
      // query, so we return an empty array here.
      return [];
    }

    // Check if there are custom validation rules to be used
    let customRules;
    const customRulesModulePath = extensions.customValidationRules;
    if (customRulesModulePath) {
      /* eslint-disable no-implicit-coercion */
      const rulesPath = resolveFile(customRulesModulePath);
      if (rulesPath) {
        const customValidationRules = await requireFile(rulesPath);
        if (customValidationRules) {
          customRules = customValidationRules(this._graphQLConfig);
        }
      }
      /* eslint-enable no-implicit-coercion */
    }
    const schema = await this._graphQLCache
      .getSchema(projectName, queryHasExtensions)
      .catch(() => null);

    if (!schema) {
      return [];
    }

    return validateQuery(validationAst, schema, customRules, isRelayCompatMode);
  }
github graphql / graphiql / packages / graphql-language-service-interface / src / GraphQLLanguageService.ts View on Github external
try {
      validationAst = parse(source);
    } catch (error) {
      // the query string is already checked to be parsed properly - errors
      // from this parse must be from corrupted fragment dependencies.
      // For IDEs we don't care for errors outside of the currently edited
      // query, so we return an empty array here.
      return [];
    }

    // Check if there are custom validation rules to be used
    let customRules;
    const customRulesModulePath = extensions.customValidationRules;
    if (customRulesModulePath) {
      /* eslint-disable no-implicit-coercion */
      const rulesPath = resolveFile(customRulesModulePath);
      if (rulesPath) {
        const customValidationRules = await requireFile(rulesPath);
        if (customValidationRules) {
          customRules = customValidationRules(this._graphQLConfig);
        }
      }
      /* eslint-enable no-implicit-coercion */
    }
    const schema = await this._graphQLCache
      .getSchema(projectName, queryHasExtensions)
      .catch(() => null);

    if (!schema) {
      return [];
    }
github graphql / graphiql / packages / graphql-language-service-server / src / MessageProcessor.js View on Github external
text.indexOf('graphql`') === -1 &&
      text.indexOf('graphql.experimental`') === -1 &&
      text.indexOf('gql`') === -1
    ) {
      return [];
    }
    const templates = findGraphQLTags(text);
    return templates.map(({ template, range }) => ({ query: template, range }));
  } else {
    const query = text;
    if (!query && query !== '') {
      return [];
    }
    const lines = query.split('\n');
    const range = new Range(
      new Position(0, 0),
      new Position(lines.length - 1, lines[lines.length - 1].length - 1)
    );
    return [{ query, range }];
  }
}
github graphql / graphiql / packages / graphql-language-service-server / src / MessageProcessor.js View on Github external
text.indexOf('graphql.experimental`') === -1 &&
      text.indexOf('gql`') === -1
    ) {
      return [];
    }
    const templates = findGraphQLTags(text);
    return templates.map(({ template, range }) => ({ query: template, range }));
  } else {
    const query = text;
    if (!query && query !== '') {
      return [];
    }
    const lines = query.split('\n');
    const range = new Range(
      new Position(0, 0),
      new Position(lines.length - 1, lines[lines.length - 1].length - 1)
    );
    return [{ query, range }];
  }
}