Skip to content

Commit

Permalink
fix: empty root schema #45
Browse files Browse the repository at this point in the history
* Added unittest
* Added getDocumentFromSDL function (supports empty SDL/Documents)
  • Loading branch information
mwoelk committed Jan 15, 2018
1 parent 4f1c0a4 commit 65dedd7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions fixtures/imports-only/a.graphql
@@ -0,0 +1,4 @@
type Query {
first: String
second: Float
}
2 changes: 2 additions & 0 deletions fixtures/imports-only/all.graphql
@@ -0,0 +1,2 @@
# import Query.* from "a.graphql"
#import Query.* from "b.graphql"
3 changes: 3 additions & 0 deletions fixtures/imports-only/b.graphql
@@ -0,0 +1,3 @@
type Query {
third: String
}
11 changes: 11 additions & 0 deletions src/index.test.ts
Expand Up @@ -54,6 +54,17 @@ test('parse: multi line import', t => {
])
})

test('importSchema: imports only', t => {
const expectedSDL = `\
type Query {
first: String
second: Float
third: String
}
`
t.is(importSchema('fixtures/imports-only/all.graphql'), expectedSDL)
})

test('importSchema: field types', t => {
const expectedSDL = `\
type A {
Expand Down
39 changes: 36 additions & 3 deletions src/index.ts
@@ -1,5 +1,5 @@
import * as fs from 'fs'
import { DefinitionNode, parse, print, TypeDefinitionNode, GraphQLObjectType, ObjectTypeDefinitionNode } from 'graphql'
import { DefinitionNode, parse, print, TypeDefinitionNode, GraphQLObjectType, ObjectTypeDefinitionNode, DocumentNode, Kind } from 'graphql'
import { flatten, groupBy } from 'lodash'
import * as path from 'path'

Expand Down Expand Up @@ -73,7 +73,7 @@ export function parseSDL(sdl: string): RawModule[] {
*/
export function importSchema(schema: string, schemas?: { [key: string]: string }): string {
const sdl = read(schema, schemas) || schema
const document = parse(sdl, { noLocation: true })
const document = getDocumentFromSDL(sdl)

// Recursively process the imports, starting by importing all types from the initial schema
let { allDefinitions, typeDefinitions } = collectDefinitions(
Expand Down Expand Up @@ -112,6 +112,39 @@ export function importSchema(schema: string, schemas?: { [key: string]: string }
// Return the schema as string
return print(document)
}

/**
* Parses a schema into a graphql DocumentNode.
* If the schema is empty a DocumentNode with empty definitions will be created.
*
* @param sdl Schema to parse
* @returns A graphql DocumentNode with definitions of the parsed sdl.
*/
function getDocumentFromSDL(sdl: string): DocumentNode {
if (isEmptySDL(sdl)) {
return {
kind: Kind.DOCUMENT,
definitions: [],
}
} else {
return parse(sdl, { noLocation: true })
}
}

/**
* Check if a schema contains any type definitions at all.
*
* @param sdl Schema to parse
* @returns True if SDL only contains comments and/or whitespaces
*/
function isEmptySDL(sdl: string): boolean {
return sdl
.split('\n')
.map(l => l.trim())
.filter(l => !(l.length === 0 || l.startsWith('#')))
.length === 0
}

/**
* Recursively process all schema files. Keeps track of both the filtered
* type definitions, and all type definitions, because they might be needed
Expand Down Expand Up @@ -141,7 +174,7 @@ function collectDefinitions(
const dirname = path.dirname(filePath)

// Get TypeDefinitionNodes from current schema
const document = parse(sdl)
const document = getDocumentFromSDL(sdl)

// Add all definitions to running total
allDefinitions.push(filterTypeDefinitions(document.definitions))
Expand Down

0 comments on commit 65dedd7

Please sign in to comment.