-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from rintoj/fix/schema-management
fix: improve schema manager and show an error when no schema is loaded
- Loading branch information
Showing
2 changed files
with
66 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,60 @@ | ||
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader' | ||
import { loadSchema } from '@graphql-tools/load' | ||
import { UrlLoader } from '@graphql-tools/url-loader' | ||
import EventEmitter from 'events' | ||
import { existsSync } from 'fs-extra' | ||
import * as gql from 'graphql' | ||
import { resolve } from 'path' | ||
import { toNonNullArray } from 'tsds-tools' | ||
import { GQLAssistConfig } from '../config' | ||
|
||
enum SchemaEvent { | ||
LOAD = 'load:Schema', | ||
CHANGE = 'change:Schema', | ||
REMOVE = 'remove:Schema', | ||
} | ||
|
||
export class SchemaManager { | ||
private schemaFiles: string[] = [] | ||
private path: string | undefined | ||
private schema: gql.GraphQLSchema | undefined | ||
private changeEmitter = new EventEmitter() | ||
|
||
private setSchema(path: string | undefined, schema: gql.GraphQLSchema | undefined) { | ||
const currentSchema = this.schema | ||
this.path = path | ||
this.schema = schema | ||
if (!currentSchema) { | ||
this.changeEmitter.emit(SchemaEvent.LOAD) | ||
} else if (!schema) { | ||
this.changeEmitter.emit(SchemaEvent.REMOVE) | ||
} else { | ||
this.changeEmitter.emit(SchemaEvent.CHANGE) | ||
} | ||
return this | ||
} | ||
|
||
async loadSchemaFromUrl(url: string) { | ||
return this.setSchema(url, await loadSchema(url, { loaders: [new UrlLoader()] })) | ||
} | ||
|
||
async loadSchemaFromFile(path: string) { | ||
return this.setSchema(path, await loadSchema(path, { loaders: [new GraphQLFileLoader()] })) | ||
} | ||
|
||
async loadSchemaIfValid(path: string | undefined) { | ||
if (!path) return this | ||
if (path.startsWith('http')) { | ||
return this.loadSchemaFromUrl(path) | ||
} | ||
if (this.schemaFiles.includes(path)) { | ||
return this.loadSchemaFromFile(path) | ||
} | ||
return this | ||
} | ||
|
||
onDidLoad(callback: (schema: gql.GraphQLSchema | undefined, path: string | undefined) => any) { | ||
const handler = () => callback(this.schema, this.path) | ||
this.changeEmitter.on(SchemaEvent.LOAD, handler) | ||
return { dispose: () => this.changeEmitter.off(SchemaEvent.LOAD, handler) } | ||
getSchemaFSPath() { | ||
return this.path | ||
} | ||
|
||
onDidChange(callback: (schema: gql.GraphQLSchema | undefined, path: string | undefined) => any) { | ||
const handler = () => callback(this.schema, this.path) | ||
this.changeEmitter.on(SchemaEvent.CHANGE, handler) | ||
return { dispose: () => this.changeEmitter.off(SchemaEvent.CHANGE, handler) } | ||
getSchema() { | ||
return this.schema | ||
} | ||
|
||
onDidRemove(callback: (schema: gql.GraphQLSchema | undefined, path: string | undefined) => any) { | ||
const handler = () => callback(this.schema, this.path) | ||
this.changeEmitter.on(SchemaEvent.REMOVE, handler) | ||
return { dispose: () => this.changeEmitter.off(SchemaEvent.REMOVE, handler) } | ||
getSchemaFiles() { | ||
return this.schemaFiles | ||
} | ||
|
||
findSchemaFiles(folders: string[], config: GQLAssistConfig) { | ||
const possibleFileNames = toNonNullArray([...config.reactHook.schemaFileNames].flat()) | ||
const files = (this.schemaFiles = folders | ||
return (this.schemaFiles = folders | ||
.flatMap(folder => possibleFileNames.map(fileName => resolve(folder, fileName))) | ||
.filter(path => existsSync(path))) | ||
if (this.path && !files.includes(this.path)) { | ||
this.setSchema(undefined, undefined) | ||
} | ||
return files | ||
} | ||
|
||
getSchemaFSPath() { | ||
return this.path | ||
async loadSchemaFromUrl(url: string) { | ||
this.schema = await loadSchema(url, { loaders: [new UrlLoader()] }) | ||
this.path = url | ||
return this | ||
} | ||
|
||
getSchemaFiles() { | ||
return this.schemaFiles | ||
async loadSchemaFromFile(path: string) { | ||
this.schema = await loadSchema(path, { loaders: [new GraphQLFileLoader()] }) | ||
this.path = path | ||
return this | ||
} | ||
|
||
getSchema() { | ||
return this.schema | ||
async loadSchema(pathOrUrl: string | undefined) { | ||
if (pathOrUrl?.startsWith('http')) { | ||
await this.loadSchemaFromUrl(pathOrUrl) | ||
} else if (pathOrUrl) { | ||
await this.loadSchemaFromFile(pathOrUrl) | ||
} | ||
return this | ||
} | ||
|
||
removeSchema() { | ||
this.schema = undefined | ||
this.path = undefined | ||
return this | ||
} | ||
} |