Skip to content

Commit

Permalink
support type extensions in schema files
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeDawkins committed Sep 12, 2019
1 parent 8ea3435 commit 7bb1869
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/apollo-language-server/src/providers/schema/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
buildClientSchema,
Source,
buildSchema,
printSchema
printSchema,
parse
} from "graphql";
import { readFileSync } from "fs";
import { extname, resolve } from "path";
import { GraphQLSchemaProvider, SchemaChangeUnsubscribeHandler } from "./base";
import { NotificationHandler } from "vscode-languageserver";
import { buildSchemaFromSDL } from "apollo-graphql";

export interface FileSchemaProviderConfig {
path?: string;
Expand All @@ -26,27 +28,27 @@ export class FileSchemaProvider implements GraphQLSchemaProvider {
const { path, paths } = this.config;

// load each path and get sdl string from each, if a list, concatenate them all
const sdlResults = path
? this.loadFileAndGetSDL(path)
const documents = path
? [this.loadFileAndGetDocument(path)]
: paths
? paths.map(this.loadFileAndGetSDL).join("\n")
? paths.map(this.loadFileAndGetDocument)
: undefined;

if (!sdlResults)
if (!documents)
throw new Error(
`Schema could not be loaded for [${
path ? path : paths ? paths.join(", ") : "undefined"
}]`
);

this.schema = buildSchema(sdlResults);
this.schema = buildSchemaFromSDL(documents);

if (!this.schema) throw new Error(`Schema could not be loaded for ${path}`);
return this.schema;
}

// load a graphql file or introspection result and return the SDL version
loadFileAndGetSDL(path: string) {
// load a graphql file or introspection result and return the GraphQL DocumentNode
loadFileAndGetDocument(path: string) {
let result;
try {
result = readFileSync(path, {
Expand All @@ -68,9 +70,9 @@ export class FileSchemaProvider implements GraphQLSchemaProvider {
: parsed;

const schema = buildClientSchema({ __schema });
return printSchema(schema);
return parse(printSchema(schema));
} else if (ext === ".graphql" || ext === ".graphqls" || ext === ".gql") {
return result;
return parse(result);
}
throw new Error(
"File Type not supported for schema loading. Must be a .json, .graphql, .gql, or .graphqls file"
Expand Down

0 comments on commit 7bb1869

Please sign in to comment.