Skip to content

Commit

Permalink
Increased type checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 9, 2019
1 parent e853edc commit 2a803c4
Show file tree
Hide file tree
Showing 47 changed files with 325 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
EventEmitter,
ChangeDetectionStrategy
} from '@angular/core';
import { GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
import { GraphQLInterfaceType, GraphQLObjectType, GraphQLSchema } from 'graphql';

@Component({
selector: 'app-doc-viewer-type',
Expand All @@ -17,7 +17,7 @@ import { GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
export class DocViewerTypeComponent implements OnInit {

@Input() data: any = {};
@Input() gqlSchema = null;
@Input() gqlSchema: GraphQLSchema;
@Output() goToFieldChange = new EventEmitter();
@Output() goToTypeChange = new EventEmitter();
@Output() addToEditorChange = new EventEmitter();
Expand All @@ -37,9 +37,9 @@ export class DocViewerTypeComponent implements OnInit {
}

switch (type) {
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType().name:
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType().name:
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType().name:
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType()!.name:
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType()!.name:
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType()!.name:
return true;
}

Expand Down
88 changes: 64 additions & 24 deletions src/app/components/doc-viewer/doc-viewer/doc-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ import { debug } from 'app/utils/logger';
import { DomSanitizer } from '@angular/platform-browser';

import { untilDestroyed } from 'ngx-take-until-destroy';
import { GraphQLType, GraphQLArgs, GraphQLSchema, GraphQLObjectType, GraphQLInterfaceType } from 'graphql';

interface DocumentView {
view: string;
parentType: string;
name: string;
}

interface DocumentIndexEntry {
search: string;
name: string;
description: string;
args: GraphQLArgs,
cat: string;
type: string;
isQuery: Boolean;
highlight: string;
}

@Component({
selector: 'app-doc-viewer',
Expand All @@ -26,12 +44,12 @@ import { untilDestroyed } from 'ngx-take-until-destroy';
})
export class DocViewerComponent implements OnChanges, OnDestroy {

@Input() gqlSchema = null;
@Input() gqlSchema: GraphQLSchema
@Input() allowIntrospection = true;
@Input() isLoading = false;
@Input() addQueryDepthLimit = config.add_query_depth_limit;
@Input() tabSize = config.tab_size;
@Input() docView = {
@Input() docView: DocumentView = {
view: 'root', // type, field, root, search
parentType: 'Query', // used by field views
name: 'Conference' // identifies type/field
Expand All @@ -44,18 +62,18 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

@HostBinding('style.flex-grow') public resizeFactor;

rootTypes = [];
index = [];
rootTypes: GraphQLType[] = [];
index: DocumentIndexEntry[] = [];

searchInputPlaceholder = 'Search docs...';

// Used to determine if index related actions (like search, add query, etc.)
// should be available
hasSearchIndex = false;

docHistory = [];
docHistory: DocumentView[] = [];

searchResult = [];
searchResult: DocumentIndexEntry[] = [];
searchTerm = '';

constructor(
Expand Down Expand Up @@ -84,7 +102,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
schema.getQueryType(),
schema.getMutationType(),
schema.getSubscriptionType()
].filter(val => !!val);
].filter(Boolean);

try {
this.generateIndex(schema);
Expand All @@ -100,8 +118,8 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param schema
*/
generateIndex(schema) {
let getFieldsIndices = null;
let getTypeIndices = null;
let getFieldsIndices;
let getTypeIndices;

/**
* Gets the indices for fields
Expand All @@ -111,8 +129,8 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param {array} curIndexStack contains all the currently mapped indices in the stack
* @return {array} the indices for the given fields
*/
getFieldsIndices = (fields, type, isQuery, curIndexStack) => {
let index = [];
getFieldsIndices = (fields, type, isQuery: Boolean, curIndexStack) => {
let index: DocumentIndexEntry[] = [];

Object.keys(fields).forEach(fieldKey => {
const field = fields[fieldKey];
Expand All @@ -128,7 +146,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
isQuery,
highlight: 'field'
};
index = [...index, fieldIndex];
index = [ ...index, fieldIndex ];

// For each argument of the field, create an entry in the index for the field,
// searchable by the argument name
Expand Down Expand Up @@ -307,19 +325,23 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param parentType parent type of the current field
* @param parentFields preceding parent field and type combinations
*/
generateQuery(name, parentType): { query: String, meta: any } {
generateQuery(name, parentType) {
let query = '';
let hasArgs = false;

if (!this.gqlSchema) {
return;
}

// Add the root type of the query
switch (parentType) {
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType().name:
case this.gqlSchema.getQueryType() && this.gqlSchema.getQueryType()!.name:
query += 'query';
break;
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType().name:
case this.gqlSchema.getMutationType() && this.gqlSchema.getMutationType()!.name:
query += 'mutation';
break;
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType().name:
case this.gqlSchema.getSubscriptionType() && this.gqlSchema.getSubscriptionType()!.name:
query += 'subscription';
break;
default:
Expand All @@ -329,10 +351,14 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

const fieldData = this.generateFieldData(name, parentType, [], 1);

if (!fieldData) {
return;
}

// Add the query fields
query += `{\n${fieldData.query}\n}`;

const meta = {...fieldData.meta};
const meta = { ...fieldData.meta };

// Update hasArgs option
meta.hasArgs = hasArgs || meta.hasArgs;
Expand All @@ -347,17 +373,24 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
* @param parentFields preceding parent field and type combinations
* @param level current depth level of the current field
*/
private generateFieldData(name, parentType, parentFields, level): { query: String, meta: any } {
private generateFieldData(name, parentType, parentFields, level: number): { query: string, meta: { hasArgs?: boolean } } {

if (!name || !parentType || !parentFields) {
return { query: '', meta: {} };
}
const tabSize = this.tabSize || 2;
const field = this.gqlSchema.getType(parentType).getFields()[name];
const parentTypeObject = this.gqlSchema.getType(parentType) as GraphQLObjectType | undefined;
const field = parentTypeObject && parentTypeObject.getFields()[name];

if (!field) {
return { query: '', meta: {} };
}
const meta = {
hasArgs: false
};

// Start the query with the field name
let fieldStr: String = ' '.repeat(level * tabSize) + field.name;
let fieldStr: string = ' '.repeat(level * tabSize) + field.name;

// If the field has arguments, add them
if (field.args && field.args.length) {
Expand All @@ -372,7 +405,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

// Retrieve the current field type
const curTypeName = this.cleanName(field.type.inspect());
const curType = this.gqlSchema.getType(curTypeName);
const curType = this.gqlSchema.getType(curTypeName) as GraphQLObjectType | undefined;

// Don't add a field if it has been added in the query already.
// This happens when there is a recursive field
Expand All @@ -387,7 +420,7 @@ export class DocViewerComponent implements OnChanges, OnDestroy {

// Get all the fields of the field type, if available
const innerFields = curType && curType.getFields && curType.getFields();
let innerFieldsData: String = null;
let innerFieldsData = '';
if (innerFields) {
innerFieldsData = Object.keys(innerFields).reduce((acc, cur) => {
// Don't add a field if it has been added in the query already.
Expand All @@ -397,10 +430,14 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
}

const curInnerFieldData = this.generateFieldData(cur, curTypeName, [...parentFields, { name, type: curTypeName }], level + 1);
if (!curInnerFieldData) {
return acc;
}

const curInnerFieldStr: String = curInnerFieldData.query;

// Set the hasArgs meta if the inner field has args
meta.hasArgs = meta.hasArgs || curInnerFieldData.meta.hasArgs;
meta.hasArgs = meta.hasArgs || curInnerFieldData.meta.hasArgs || false;

// Don't bother adding the field if there was nothing generated.
// This should fix the empty line issue in the inserted queries
Expand All @@ -426,7 +463,10 @@ export class DocViewerComponent implements OnChanges, OnDestroy {
if (!this.hasSearchIndex) {
return false;
}
this.addQueryToEditorChange.next(this.generateQuery(name, parentType));
const generatedQuery = this.generateQuery(name, parentType);
if (generatedQuery) {
this.addQueryToEditorChange.next(generatedQuery);
}
}

exportSDL() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class EnvironmentManagerComponent implements OnInit {
};

selectedEnvironmentId = 'base';
selectedEnvironment: fromEnvironments.EnvironmentState = null;
selectedEnvironment: fromEnvironments.EnvironmentState | undefined;
editorContent = '{}';
editorTitle = '';

Expand Down
8 changes: 4 additions & 4 deletions src/app/components/fancy-input/fancy-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {


highlightData = {
sections: []
sections: [] as any[]
};

private innerValue = '';
Expand Down Expand Up @@ -123,7 +123,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {
}
fixFirefox() {}
getRanges(val, highlight) {
const ranges = [];
const ranges: any[] = [];
let match;
while (match = highlight.exec(val), match !== null) {
ranges.push([match.index, match.index + match[0].length]);
Expand All @@ -138,7 +138,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {

// Prevent overlapping ranges
removeStaggeredRanges(ranges) {
const unstaggeredRanges = [];
const unstaggeredRanges: any[] = [];
ranges.forEach((range) => {
const isStaggered = unstaggeredRanges.some((unstaggeredRange) => {
const isStartInside = range[0] > unstaggeredRange[0] && range[0] < unstaggeredRange[1];
Expand Down Expand Up @@ -185,7 +185,7 @@ export class FancyInputComponent implements ControlValueAccessor, OnInit {
});
}
generateHighlightSections(val: string, boundaries: BoundaryMarker[]) {
const sections = [];
const sections: any[] = [];
let lastBoundary = {
index: 0,
type: ''
Expand Down
15 changes: 8 additions & 7 deletions src/app/components/flex-resizer/flex-resizer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FlexResizerComponent implements OnInit, OnDestroy {
* Element to be resized
*/
resizeElement: Element;
resizeContainer: Element;
resizeContainer: Element | null;
draggingMode = false;
px: number;
py: number;
Expand Down Expand Up @@ -94,14 +94,15 @@ export class FlexResizerComponent implements OnInit, OnDestroy {
this.originalWidth = this.resizeElement.clientWidth;
this.originalX = event.clientX;

this.siblingGrowthFactor = Array.from(this.resizeElement.parentElement.children)
.filter(el => this.resizeElement !== el)
.reduce((acc, el) => +getComputedStyle(el).getPropertyValue('flex-grow') + acc, 0);

if (this.resizeElement.parentElement) {
this.siblingGrowthFactor = Array.from(this.resizeElement.parentElement.children)
.filter(el => this.resizeElement !== el)
.reduce((acc, el) => +getComputedStyle(el).getPropertyValue('flex-grow') + acc, 0);
}
}

onResizerMove(event: MouseEvent) {
if (!this.draggingMode) {
if (!this.draggingMode || !this.resizeContainer) {
return true;
}
event.stopPropagation();
Expand Down Expand Up @@ -131,7 +132,7 @@ export class FlexResizerComponent implements OnInit, OnDestroy {
}

getResizeContainer() {
let el = this.resizeElement;
let el: Element | null = this.resizeElement;

while (el && !el.hasAttribute('data-resize-container')) {
el = el.parentElement;
Expand Down
4 changes: 3 additions & 1 deletion src/app/components/fork-repo/fork-repo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export class ForkRepoComponent implements OnInit {
electron.shell.openExternal(url);
} else {
const win = window.open(url, '_blank');
win.focus();
if (win) {
win.focus();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div
*ngIf="pluginData"
#pluginElRef
[ngClass]="{ 'plugin__hide-content': !plugin.isActive }"
[ngClass]="{ 'plugin__hide-content': !pluginData.isActive }"
class="plugin__holder"
></div>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('PluginElementComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(PluginElementComponent);
component = fixture.componentInstance;
component.plugin = {};
fixture.detectChanges();
});

Expand Down

0 comments on commit 2a803c4

Please sign in to comment.