diff --git a/packages/apollo-language-server/src/providers/schema/file.ts b/packages/apollo-language-server/src/providers/schema/file.ts index 8a94b41a0f..f52a79a370 100644 --- a/packages/apollo-language-server/src/providers/schema/file.ts +++ b/packages/apollo-language-server/src/providers/schema/file.ts @@ -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; @@ -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, { @@ -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"