Skip to content

Commit e020c5b

Browse files
authored
fix: Adding possibility for multiple files in typedefs
2 parents 1906cb2 + 2176c4a commit e020c5b

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The `props` argument accepts the following fields:
7575

7676
| Key | Type | Default | Note |
7777
| --- | --- | --- | --- |
78-
| `typeDefs` | String | `null` | Contains GraphQL type definitions in [SDL](https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51) or file path to type definitions (required if `schema` is not provided \*) |
78+
| `typeDefs` | `String` or `Function` or `DocumentNode` or `array` of previous | `null` | Contains GraphQL type definitions in [SDL](https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51) or file path to type definitions (required if `schema` is not provided \*) |
7979
| `resolvers` | Object | `null` | Contains resolvers for the fields specified in `typeDefs` (required if `schema` is not provided \*) |
8080
| `schema` | Object | `null` | An instance of [`GraphQLSchema`](http://graphql.org/graphql-js/type/#graphqlschema) (required if `typeDefs` and `resolvers` are not provided \*) |
8181
| `context` | Object or Function | `{}` | Contains custom data being passed through your resolver chain. This can be passed in as an object, or as a Function with the signature `(req: ContextParameters) => any` \*\* |

src/index.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class GraphQLServer {
6262
typeDefs,
6363
} = props
6464

65-
const typeDefsString = buildTypeDefsString(typeDefs)
65+
const typeDefsString = mergeTypeDefs(typeDefs)
6666

6767
const uploadMixin = typeDefsString.includes('scalar Upload')
6868
? { Upload: GraphQLUpload }
@@ -294,26 +294,19 @@ export class GraphQLServer {
294294
}
295295
}
296296

297-
function buildTypeDefsString(typeDefs: ITypeDefinitions): string {
298-
let typeDefinitions = mergeTypeDefs(typeDefs)
299-
300-
// read from .graphql file if path provided
301-
if (typeDefinitions.endsWith('graphql')) {
302-
const schemaPath = path.resolve(typeDefinitions)
303-
304-
if (!fs.existsSync(schemaPath)) {
305-
throw new Error(`No schema found for path: ${schemaPath}`)
306-
}
307-
308-
typeDefinitions = importSchema(schemaPath)
309-
}
310-
311-
return typeDefinitions
312-
}
313-
314297
function mergeTypeDefs(typeDefs: ITypeDefinitions): string {
315298
if (typeof typeDefs === 'string') {
316-
return typeDefs
299+
if (typeDefs.endsWith('graphql')) {
300+
const schemaPath = path.resolve(typeDefs)
301+
302+
if (!fs.existsSync(schemaPath)) {
303+
throw new Error(`No schema found for path: ${schemaPath}`)
304+
}
305+
306+
return importSchema(schemaPath)
307+
} else {
308+
return typeDefs
309+
}
317310
}
318311

319312
if (typeof typeDefs === 'function') {
@@ -324,7 +317,11 @@ function mergeTypeDefs(typeDefs: ITypeDefinitions): string {
324317
return print(typeDefs)
325318
}
326319

327-
return typeDefs.reduce<string>((acc, t) => acc + '\n' + mergeTypeDefs(t), '')
320+
if (Array.isArray(typeDefs)) {
321+
return typeDefs.reduce<string>((acc, t) => acc + '\n' + mergeTypeDefs(t), '')
322+
}
323+
324+
throw new Error('Typedef is not string, function, DocumentNode or array of previous')
328325
}
329326

330327
function isDocumentNode(node: any): node is DocumentNode {

0 commit comments

Comments
 (0)