Browse Source

feat(server): Add GraphQL config endpoint

Michael Bromley 7 years ago
parent
commit
e773d02643

+ 1 - 1
server/package.json

@@ -31,6 +31,7 @@
     "body-parser": "^1.18.3",
     "graphql": "^0.13.2",
     "graphql-tools": "^3.0.2",
+    "graphql-type-json": "^0.2.1",
     "i18next": "^11.3.3",
     "i18next-express-middleware": "^1.2.0",
     "i18next-icu": "^0.4.0",
@@ -72,7 +73,6 @@
       "json",
       "ts"
     ],
-
     "roots": [
       "src",
       "../shared"

+ 1 - 0
server/src/api/common/common.graphql

@@ -0,0 +1 @@
+scalar JSON

+ 7 - 0
server/src/api/config/config.api.graphql

@@ -0,0 +1,7 @@
+type Query {
+  config: Config!
+}
+
+type Config {
+    customFields: JSON
+}

+ 19 - 0
server/src/api/config/config.resolver.ts

@@ -0,0 +1,19 @@
+import { Query, Resolver } from '@nestjs/graphql';
+
+import { getConfig, VendureConfig } from '../../config/vendure-config';
+import { ConfigService } from '../../service/config.service';
+
+@Resolver('Config')
+export class ConfigResolver {
+    constructor(private configService: ConfigService) {}
+
+    /**
+     * Exposes a subset of the VendureConfig which may be of use to clients.
+     */
+    @Query()
+    config(): Partial<VendureConfig> {
+        return {
+            customFields: this.configService.customFields,
+        };
+    }
+}

+ 6 - 0
server/src/app.module.ts

@@ -2,9 +2,11 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
 import { GraphQLFactory, GraphQLModule } from '@nestjs/graphql';
 import { TypeOrmModule } from '@nestjs/typeorm';
 import { graphiqlExpress, graphqlExpress } from 'apollo-server-express';
+import * as GraphQLJSON from 'graphql-type-json';
 
 import { AdministratorResolver } from './api/administrator/administrator.resolver';
 import { AuthController } from './api/auth/auth.controller';
+import { ConfigResolver } from './api/config/config.resolver';
 import { CustomerController } from './api/customer/customer.controller';
 import { CustomerResolver } from './api/customer/customer.resolver';
 import { ProductOptionResolver } from './api/product-option/product-option.resolver';
@@ -33,6 +35,7 @@ const config = getConfig();
         AdministratorResolver,
         AdministratorService,
         AuthService,
+        ConfigResolver,
         ConfigService,
         JwtStrategy,
         I18nService,
@@ -86,6 +89,9 @@ export class AppModule implements NestModule {
             resolverValidationOptions: {
                 requireResolversForResolveType: false,
             },
+            resolvers: {
+                JSON: GraphQLJSON,
+            },
         });
     }
 }

+ 5 - 1
server/src/service/config.service.ts

@@ -4,7 +4,7 @@ import { ConnectionOptions } from 'typeorm';
 
 import { ReadOnlyRequired } from '../common/common-types';
 import { EntityIdStrategy } from '../config/entity-id-strategy/entity-id-strategy';
-import { getConfig, VendureConfig } from '../config/vendure-config';
+import { CustomFields, getConfig, VendureConfig } from '../config/vendure-config';
 import { LanguageCode } from '../locale/language-code';
 
 @Injectable()
@@ -37,6 +37,10 @@ export class ConfigService implements VendureConfig {
         return this.activeConfig.dbConnectionOptions;
     }
 
+    get customFields(): CustomFields {
+        return this.activeConfig.customFields;
+    }
+
     private activeConfig: ReadOnlyRequired<VendureConfig>;
 
     constructor() {

+ 4 - 0
server/yarn.lock

@@ -2113,6 +2113,10 @@ graphql-tools@^3.0.2:
     iterall "^1.1.3"
     uuid "^3.1.0"
 
+graphql-type-json@^0.2.1:
+  version "0.2.1"
+  resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.2.1.tgz#d2c177e2f1b17d87f81072cd05311c0754baa420"
+
 graphql@^0.13.2:
   version "0.13.2"
   resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.13.2.tgz#4c740ae3c222823e7004096f832e7b93b2108270"