Skip to content

Commit

Permalink
Improved fillFields to also work when cursor in selectionSet position.
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Apr 6, 2019
1 parent 9350936 commit b97fbf3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/app/components/query-editor/query-editor.component.ts
Expand Up @@ -235,8 +235,6 @@ export class QueryEditorComponent implements OnInit, AfterViewInit, OnChanges {
onFillFields(cm) {
const cursor = cm.getCursor();
const token = cm.getTokenAt(cursor);
const typeInfo = getTypeInfo(this.gqlSchema, token.state);

const schema = this.gqlSchema;
if (!schema) {
return;
Expand Down
22 changes: 17 additions & 5 deletions src/app/services/gql/fillFields.ts
Expand Up @@ -7,6 +7,7 @@ import {
parse,
} from 'graphql';
import { debug } from 'app/utils/logger';
import getTypeInfo from 'codemirror-graphql/utils/getTypeInfo';

export const parseQuery = (query: string) => {
if (!query) {
Expand Down Expand Up @@ -91,29 +92,40 @@ export const fillAllFields = (schema, query: string, cursor, token, { maxDepth =
return { insertions, result: query };
}

let tokenState = token.state;
if (token.state.kind === 'SelectionSet') {
tokenState.wasSelectionSet = true;
tokenState = { ...tokenState, ...tokenState.prevState };
}
const fieldType = getTypeInfo(schema, token.state).type;
// Strip out empty selection sets since those throw errors while parsing query
query = query.replace(/{\s*}/g, '');
const ast = parseQuery(query);

if (!ast) {
if (!fieldType || !ast) {
return { insertions, result: query };
}

const typeInfo = new TypeInfo(schema);
const edited = visit(ast, {
leave(node) {
typeInfo.leave(node);
// typeInfo.leave(node);
debug.log(node);
},
enter(node) {
typeInfo.enter(node);
if (node.kind === 'Field') {
const fieldType = typeInfo.getType();
// const fieldType = typeInfo.getType();
if (
1
&& node.name.value === token.state.name
&& node.name.value === tokenState.name

&& node.loc
// With tokenState of SelectionSet (i.e. between braces { | }),
// we wouldn't have the right position since we strip out the empty braces before
// parsing the query to make it valid
// AST line number is 1-indexed while codemirror cursor line number is 0-indexed.
&& node.loc.startToken.line - 1 === cursor.line
&& (tokenState.wasSelectionSet || node.loc.startToken.line - 1 === cursor.line)
) {
debug.log(node, typeInfo, typeInfo.getType(), cursor, token, maxDepth);
const selectionSet = buildSelectionSet(fieldType, { maxDepth });
Expand Down
1 change: 0 additions & 1 deletion src/app/services/gql/gql.service.ts
Expand Up @@ -22,7 +22,6 @@ import {
validateSchema,
visit,
} from 'graphql';
import { getAutocompleteSuggestions } from 'graphql-language-service-interface';
import * as compress from 'graphql-query-compress'; // Somehow this is the way to use this

import { NotifyService } from '../notify/notify.service';
Expand Down

0 comments on commit b97fbf3

Please sign in to comment.