Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(import): allow configuring custom root fields #280

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fixtures/merged-root-fields/a.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# import Query.* from 'b.graphql'
# import Query.*, Dummy.* from 'b.graphql'

type Query {
helloA: String
Expand Down
4 changes: 4 additions & 0 deletions fixtures/merged-root-fields/b.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ type Post {

input PostFilter {
field3: Int
}

type Dummy {
field2: String
}
3 changes: 0 additions & 3 deletions src/definition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { keyBy, uniqBy, includes } from 'lodash'
import {
DocumentNode,
TypeDefinitionNode,
ObjectTypeDefinitionNode,
InputObjectTypeDefinitionNode,
TypeNode,
NamedTypeNode,
DirectiveNode,
Expand Down
33 changes: 29 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ type C2 {
id: ID!
}
`
t.is(importSchema(schemaA, schemas), expectedSDL)
t.is(importSchema(schemaA, { schemas }), expectedSDL)
})

test(`importSchema: single object schema`, t => {
Expand Down Expand Up @@ -324,7 +324,7 @@ type C2 {
id: ID!
}
`
t.is(importSchema(schemaA, schemas), expectedSDL)
t.is(importSchema(schemaA, { schemas }), expectedSDL)
})

test(`importSchema: import all mix 'n match 2`, t => {
Expand Down Expand Up @@ -443,7 +443,7 @@ type C2 {
id: ID!
}
`
t.is(importSchema(schemaA, schemas), expectedSDL)
t.is(importSchema(schemaA, { schemas }), expectedSDL)
})

test('importSchema: scalar', t => {
Expand Down Expand Up @@ -704,6 +704,31 @@ input PostFilter {
t.is(actualSDL, expectedSDL)
})

test('merged custom root fields imports', t => {
const expectedSDL = `\
type Query {
helloA: String
posts(filter: PostFilter): [Post]
hello: String
}

type Dummy {
field: String
field2: String
}

type Post {
field1: String
}

input PostFilter {
field3: Int
}
`
const actualSDL = importSchema('fixtures/merged-root-fields/a.graphql', { mergeableTypes: ['Dummy'] })
t.is(actualSDL, expectedSDL)
})

test('global schema modules', t => {
const shared = `
type Shared {
Expand All @@ -720,7 +745,7 @@ type Shared {
first: String
}
`
t.is(importSchema('fixtures/global/a.graphql', { shared }), expectedSDL)
t.is(importSchema('fixtures/global/a.graphql', { schemas: { shared } }), expectedSDL)
})

test('missing type on type', t => {
Expand Down
30 changes: 22 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
DefinitionNode,
parse,
print,
TypeDefinitionNode,
GraphQLObjectType,
ObjectTypeDefinitionNode,
DocumentNode,
Kind,
Expand All @@ -24,6 +22,14 @@ export interface RawModule {
from: string
}

/**
* Configuration options that may be passed to `importSchema`
*/
interface ImportSchemaOptions {
schemas?: { [key: string]: string }
mergeableTypes?: [string]
}

const rootFields = ['Query', 'Mutation', 'Subscription']

const read = (schema: string, schemas?: { [key: string]: string }) => {
Expand All @@ -33,7 +39,7 @@ const read = (schema: string, schemas?: { [key: string]: string }) => {
return schemas ? schemas[schema] : schema
}

const isFile = f => f.endsWith('.graphql')
const isFile = (f: string) => f.endsWith('.graphql')

/**
* Parse a single import line and extract imported types and schema filename
Expand Down Expand Up @@ -77,13 +83,20 @@ export function parseSDL(sdl: string): RawModule[] {
/**
* Main entry point. Recursively process all import statement in a schema
*
* @param filePath File path to the initial schema file
* @see https://oss.prisma.io/content/graphql-import/overview#description
* @param schema File path to the initial schema file
* @param options Import configuration options
* @param options.schemas An object of schemas as strings
* @param options.mergeableTypes An array of custom GraphQL types that will
* be treated as [root fields]{@link https://oss.prisma.io/content/graphql-import/overview#import-root-fields}
* @returns Single bundled schema with all imported types
*/
export function importSchema(
schema: string,
schemas?: { [key: string]: string },
options: ImportSchemaOptions = {},
): string {
const { schemas, mergeableTypes = [] } = options
const allMergeableTypes = [...mergeableTypes, ...rootFields]
const sdl = read(schema, schemas) || schema
let document = getDocumentFromSDL(sdl)

Expand All @@ -96,14 +109,15 @@ export function importSchema(
)

// Post processing of the final schema (missing types, unused types, etc.)
// Query, Mutation and Subscription should be merged
// Query, Mutation, Subscription and any custom type defined in `mergeableTypes`
// should be merged
// And should always be in the first set, to make sure they
// are not filtered out.
const firstTypes = flatten(typeDefinitions).filter(d =>
includes(rootFields, d.name.value),
includes(allMergeableTypes, d.name.value),
)
const otherFirstTypes = typeDefinitions[0].filter(
d => !includes(rootFields, d.name.value),
d => !includes(allMergeableTypes, d.name.value),
)
const firstSet = firstTypes.concat(otherFirstTypes)
const processedTypeNames = []
Expand Down