Skip to content

Commit

Permalink
Merge pull request #84 from apollographql/watson/fix-mocks
Browse files Browse the repository at this point in the history
Mocks was only saving locally, added onDidSaveTextDocument for fix
  • Loading branch information
michael-watson authored Apr 16, 2021
2 parents 3026795 + d92a188 commit 4a676f3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/commands/local-supergraph-designs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export async function updateSubgraphSchemaFromURL(item: SubgraphTreeItem) {
}

export async function viewSubgraphCustomMocks(item: SubgraphTreeItem) {
const subgraphMocksUri = WorkbenchUri.supergraph(item.wbFilePath, item.subgraphName, WorkbenchUriType.MOCKS);
const subgraphMocksUri = WorkbenchUri.supergraph(item.supergraphName, item.subgraphName, WorkbenchUriType.MOCKS);
if (!existsSync(subgraphMocksUri.fsPath)) {
const wbFile = FileProvider.instance.workbenchFileFromPath(item.wbFilePath);
const customMocks = wbFile?.schemas[item.subgraphName].customMocks;
Expand Down
13 changes: 13 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createWorkbenchFromPreloaded, startMocks, stopMocks, deleteOperation, a
import { resolve } from 'path';
import { mkdirSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import { WorkbenchUri } from './workbench/file-system/WorkbenchUri';

export const compositionDiagnostics: DiagnosticCollection = languages.createDiagnosticCollection("composition-errors");
export const operationDiagnostics: DiagnosticCollection = languages.createDiagnosticCollection("operation-errors");
Expand Down Expand Up @@ -147,4 +148,16 @@ export async function activate(context: ExtensionContext) {
}
}
})
workspace.onDidSaveTextDocument(async document => {
let uri = document.uri;
if (uri.path.includes('mocks')) {
const querySplit = uri.query.split(':');
const { wbFile, path } = FileProvider.instance.workbenchFileByGraphName(querySplit[0]);
const newMocksText = document.getText();
if (newMocksText != wbFile.schemas[querySplit[1]].customMocks) {
wbFile.schemas[querySplit[1]].customMocks = newMocksText;
FileProvider.instance.saveWorkbenchFile(wbFile, path);
}
}
})
}
2 changes: 1 addition & 1 deletion src/workbench/file-system/WorkbenchUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class WorkbenchUri {
let subgarphMocksPath = resolve(StateManager.instance.extensionGlobalStoragePath ?? '', 'mocks', `${name}-mocks.js`);
if (subgarphMocksPath.toLowerCase().includes('c:')) subgarphMocksPath = subgarphMocksPath.slice(2).replace(/\\/g, '/');

return Uri.parse(subgarphMocksPath);
return Uri.parse(`${subgarphMocksPath}?${path}:${name}`);
}
}
}
15 changes: 15 additions & 0 deletions src/workbench/file-system/fileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class FileProvider implements FileSystemProvider {
let workbenchFiles = this.getWorkbenchFilesInDirectory(workspaceRoot);
workbenchFiles.forEach(workbenchFile => {
try {
let test = JSON.parse(readFileSync(workbenchFile.path, { encoding: 'utf-8' }));
const wbFile = JSON.parse(readFileSync(workbenchFile.path, { encoding: 'utf-8' })) as ApolloWorkbenchFile;
this.workbenchFiles.set(workbenchFile.path, wbFile);
} catch (err) {
Expand Down Expand Up @@ -98,7 +99,21 @@ export class FileProvider implements FileSystemProvider {

return wbFile;
}
workbenchFileByGraphName(name: string) {
let path = '';
let wbFile: ApolloWorkbenchFile = new ApolloWorkbenchFile(name);
this.workbenchFiles.forEach((value, key) => {
if (value.graphName == name) {
path = key;
wbFile = value;
}
})
// let wbFile = this.workbenchFiles.get(path);
// if (!wbFile) //we're on Windows
// wbFile = this.workbenchFiles.get(path.replace(/\//g, '\\'));

return { wbFile, path };
}
//FileSystemProvider Implementations
//File chagnes are watched at the `vscode.workspace.onDidChangeTextDocument` level
readFile(uri: Uri): Uint8Array | Thenable<Uint8Array> {
Expand Down
11 changes: 7 additions & 4 deletions src/workbench/serverManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { OverrideApolloGateway, GatewayForwardHeadersDataSource } from "../graph
import { FileProvider } from "./file-system/fileProvider";
import { extractEntityNames } from "../graphql/parsers/schemaParser";
import { resolve } from "path";
import { mkdirSync, writeFileSync } from "fs";
import { mkdirSync, writeFileSync, readFileSync } from "fs";
import { workspace, Uri, window, StatusBarAlignment, tasks } from "vscode";
import { execSync } from "child_process";
import { Disposable } from "vscode-languageclient";
import { WorkbenchUri, WorkbenchUriType } from "./file-system/WorkbenchUri";

const { name } = require('../../package.json');

Expand All @@ -29,11 +30,11 @@ export class ServerManager {
private serversState: { [port: string]: any } = {};

private corsConfiguration: CorsOptions = {
origin: '*',
credentials: true,
origin: '*',
credentials: true,
};

startSupergraphMocks(wbFilePath: string) {
async startSupergraphMocks(wbFilePath: string) {
if (StateManager.settings_tlsRejectUnauthorized) process.env.NODE_TLS_REJECT_UNAUTHORIZED = '';
else process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

Expand Down Expand Up @@ -124,6 +125,8 @@ export class ServerManager {
const server = new ApolloServer({
cors: this.corsConfiguration,
schema,
// mocks,
// mockEntireSchema: false,
subscriptions: false
});
server.listen({ port }).then(({ url }) => console.log(`${name}:🚀 ${serviceName} mocked server ready at ${url}`));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class SubgraphSummaryTreeItem extends TreeItem {
this.contextValue = 'subgraphSummaryTreeItem';

Object.keys(wbFile.schemas).forEach(subgraphName => {
this.subgraphs.push(new SubgraphTreeItem(subgraphName, wbFile.schemas[subgraphName], filePath))
this.subgraphs.push(new SubgraphTreeItem(wbFile.graphName, subgraphName, wbFile.schemas[subgraphName], filePath))
})
this.iconPath = {
light: path.join(__filename, '..', '..', '..', '..', 'media', 'subgraph.svg'),
Expand All @@ -145,6 +145,7 @@ export class SubgraphTreeItem extends TreeItem {
children: TreeItem[] = new Array<TreeItem>();

constructor(
public readonly supergraphName: string,
public readonly subgraphName: string,
public readonly wbSchema: WorkbenchSchema,
public readonly wbFilePath: string
Expand Down

0 comments on commit 4a676f3

Please sign in to comment.