How to use the graphql-language-service-interface.getAutocompleteSuggestions function in graphql-language-service-interface

To help you get started, we’ve selected a few graphql-language-service-interface 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 Quramy / ts-graphql-plugin / src / graphql-language-service-adapter.ts View on Github external
if (!this._schema) return delegate(fileName, position, options);
    const node = this._helper.getNode(fileName, position);
    if (!node || node.kind !== ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
      return delegate(fileName, position, options);
    }
    if (this._tagCondition && !isTagged(node, this._tagCondition)) {
      return delegate(fileName, position, options);
    }
    const cursor = position - node.getStart();
    const baseLC = this._helper.getLineAndChar(fileName, node.getStart());
    const cursorLC = this._helper.getLineAndChar(fileName, position);
    const relativeLC = { line: cursorLC.line - baseLC.line, character: cursorLC.character - baseLC.character + 1 };
    const p = new SimplePosition(relativeLC);
    const text = node.getText().slice(1, cursor + 1); // remove the backquote char
    this._logger('Search text: "' + text + '" at ' + cursor + ' position');
    const gqlCompletionItems = getAutocompleteSuggestions(this._schema, text, p);
    this._logger(JSON.stringify(gqlCompletionItems));
    return translateCompletionItems(gqlCompletionItems);
  }
github graphql / graphiql / packages / codemirror-graphql / src / hint.js View on Github external
CodeMirror.registerHelper('hint', 'graphql', (editor, options) => {
  const schema = options.schema;
  if (!schema) {
    return;
  }

  const cur = editor.getCursor();
  const token = editor.getTokenAt(cur);
  const rawResults = getAutocompleteSuggestions(
    schema,
    editor.getValue(),
    cur,
    token,
  );
  /**
   * GraphQL language service responds to the autocompletion request with
   * a different format:
   * type CompletionItem = {
   *   label: string,
   *   kind?: number,
   *   detail?: string,
   *   documentation?: string,
   *   // GraphQL Deprecation information
   *   isDeprecated?: ?string,
   *   deprecationReason?: ?string,
github graphql / graphiql / packages / monaco-graphql / src / monaco-graphql-query.ts View on Github external
export async function provideCompletionItems({
  position,
  model,
  schema
}: ProviderItemInput): Promise<
  languages.ProviderResult
> {
  const graphQLPosition = new GraphQLPosition(
    position.lineNumber - 1,
    position.column - 1,
  );
  graphQLPosition.setCharacter(position.column - 1);
  graphQLPosition.line = position.lineNumber - 1;
  const suggestions = await getAutocompleteSuggestions(
    schema,
    model.getValue(),
    graphQLPosition,
  );
  // @ts-ignore wants range
  return {
    // TODO: possibly return different kinds of completion items?
    // TODO: (optionally?) show all completion items at first?
    suggestions: suggestions.map(s => ({
      label: s.label,
      kind: s.kind,
      detail: s.detail,
      documentation: s.documentation,
      insertText: s.label,
    })),
  };
github orionsoft / atom-graphql-autocomplete / lib / Snippets / getSnippets.js View on Github external
export default async function({schema, query, fileContent, bufferPosition}) {
  if (!isCursorInQuery(query, fileContent, bufferPosition)) return []
  const cursor = {
    line: getRow(fileContent, query, bufferPosition),
    character: getColumn(fileContent, query, bufferPosition)
  }
  const cleanedQuery = cleanQuery(query)
  const hints = getAutocompleteSuggestions(schema, cleanedQuery, cursor)

  const custom = getCustomSnippets({hints, schema, query: cleanedQuery, cursor})
  hints.push(...custom)

  if (!hints) return []
  return hints.map(hint => {
    return {
      snippet: hint.label,
      displayText: hint.label,
      leftLabel: hint.detail,
      description: hint.documentation || 'Self descriptive.'
    }
  })
}
github graphql / graphiql / packages / graphql-language-service / src / client.js View on Github external
function _getAutocompleteSuggestions(
  queryText: string,
  point: Position,
  schemaPath: string,
): EXIT_CODE {
  invariant(
    schemaPath,
    'A schema path is required to provide GraphQL autocompletion',
  );

  try {
    const schema = schemaPath ? generateSchema(schemaPath) : null;
    const resultArray = schema
      ? getAutocompleteSuggestions(schema, queryText, point)
      : [];
    const resultObject = resultArray.reduce((prev, cur, index) => {
      prev[index] = cur;
      return prev;
    }, {});
    process.stdout.write(JSON.stringify(resultObject, null, 2));
    return GRAPHQL_SUCCESS_CODE;
  } catch (error) {
    process.stderr.write(error);
    return GRAPHQL_FAILURE_CODE;
  }
}
github orionsoft / atom-graphql-autocomplete / lib / graphql / getHints.js View on Github external
export default function ({ schema, query, cursor }) {
  return getAutocompleteSuggestions(schema, query, cursor)
}
github hasura / graphqurl / src / ui.js View on Github external
}
    }

    if (key === 'TAB' && !menuOn) {
      qs = ib.getInput();
      let bStack = [];
      for (let c of qs) {
        if (c === '{' || c === '(' || c === '[') {
          bStack.push(c);
        }
        if (c === '}' || c === ')' || c === ']') {
          bStack.pop();
        }
      }
      p.setCharacter(qs.length);
      let acs = getAutocompleteSuggestions(schema, qs, p);
      acs = acs.map(o => o.label);

      let bStackACS = bStack.map(c => {
        switch (c) {
        case '{':
          return '}';
        case '(':
          return ')';
        case '[':
          return ']';
        }
        return undefined;
      });

      if (bStackACS.length > 0) {
        acs.unshift(bStackACS[bStackACS.length - 1]);