1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.applyExtension = applyExtension;
|
7 | exports.createSchema = createSchema;
|
8 |
|
9 | var _graphql = require('graphql');
|
10 |
|
11 | var _graphqlTools = require('graphql-tools');
|
12 |
|
13 | var _queries = require('./queries');
|
14 |
|
15 | var _node = require('./types/node');
|
16 |
|
17 | var _extensions = require('./extensions');
|
18 |
|
19 | const debug = require('debug')('graphbrainz:schema');
|
20 |
|
21 | function applyExtension(extension, schema, options = {}) {
|
22 | let outputSchema = schema;
|
23 | if (extension.extendSchema) {
|
24 | if (typeof extension.extendSchema === 'object') {
|
25 | debug(`Extending schema via an object from the “${extension.name}” extension.`);
|
26 | const { schemas = [], resolvers } = extension.extendSchema;
|
27 | outputSchema = schemas.reduce((updatedSchema, extensionSchema) => {
|
28 | if (typeof extensionSchema === 'string') {
|
29 | extensionSchema = (0, _graphql.parse)(extensionSchema);
|
30 | }
|
31 | return (0, _graphql.extendSchema)(updatedSchema, extensionSchema);
|
32 | }, outputSchema);
|
33 | if (resolvers) {
|
34 | (0, _graphqlTools.addResolveFunctionsToSchema)({ schema: outputSchema, resolvers });
|
35 | }
|
36 | } else if (typeof extension.extendSchema === 'function') {
|
37 | debug(`Extending schema via a function from the “${extension.name}” extension.`);
|
38 | outputSchema = extension.extendSchema(schema, options);
|
39 | } else {
|
40 | throw new Error(`The “${extension.name}” extension contains an invalid ` + `\`extendSchema\` value: ${extension.extendSchema}`);
|
41 | }
|
42 | }
|
43 |
|
44 |
|
45 | if (outputSchema._queryType.description === undefined) {
|
46 | outputSchema._queryType.description = schema._queryType.description;
|
47 | }
|
48 | return outputSchema;
|
49 | }
|
50 |
|
51 | function createSchema(schema, options = {}) {
|
52 | const { extensions = [] } = options;
|
53 | return extensions.reduce((updatedSchema, extension) => {
|
54 | return applyExtension((0, _extensions.loadExtension)(extension), updatedSchema, options);
|
55 | }, schema);
|
56 | }
|
57 |
|
58 | exports.default = new _graphql.GraphQLSchema({
|
59 | query: new _graphql.GraphQLObjectType({
|
60 | name: 'Query',
|
61 | description: `The query root, from which multiple types of MusicBrainz
|
62 | requests can be made.`,
|
63 | fields: () => ({
|
64 | lookup: _queries.lookup,
|
65 | browse: _queries.browse,
|
66 | search: _queries.search,
|
67 | node: _node.nodeField
|
68 | })
|
69 | })
|
70 | }); |
\ | No newline at end of file |