From 717798805924bc5cdb8be6220e7f342445af463b Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Sat, 16 Jun 2018 17:59:21 -0600 Subject: [PATCH] added mock tenant api --- .graphqlconfig | 3 ++ src/core/server/app/index.ts | 24 +++++++++++- .../resolvers => common/scalars}/cursor.ts | 0 src/core/server/graph/management/context.ts | 13 +++++++ .../server/graph/management/middleware.ts | 12 ++++++ .../server/graph/management/schema/index.ts | 18 +++++++++ .../graph/management/schema/schema.graphql | 39 +++++++++++++++++++ .../server/graph/tenant/resolvers/index.ts | 2 +- src/core/server/graph/tenant/schema/index.ts | 10 +---- 9 files changed, 110 insertions(+), 11 deletions(-) rename src/core/server/graph/{tenant/resolvers => common/scalars}/cursor.ts (100%) create mode 100644 src/core/server/graph/management/context.ts create mode 100644 src/core/server/graph/management/middleware.ts create mode 100644 src/core/server/graph/management/schema/index.ts create mode 100644 src/core/server/graph/management/schema/schema.graphql diff --git a/.graphqlconfig b/.graphqlconfig index 8c7fde676..3a304de48 100644 --- a/.graphqlconfig +++ b/.graphqlconfig @@ -2,6 +2,9 @@ "projects": { "tenant": { "schemaPath": "src/core/server/graph/tenant/schema/schema.graphql" + }, + "management": { + "schemaPath": "src/core/server/graph/management/schema/schema.graphql" } } } \ No newline at end of file diff --git a/src/core/server/app/index.ts b/src/core/server/app/index.ts index ba0597fbc..bb0966906 100644 --- a/src/core/server/app/index.ts +++ b/src/core/server/app/index.ts @@ -2,7 +2,6 @@ import express, { Express, Router } from 'express'; import { Db } from 'mongodb'; import { Config } from 'talk-server/config'; -import schema from 'talk-server/graph/tenant/schema'; import { create } from 'talk-server/services/mongodb'; import serveStatic from './middleware/serveStatic'; @@ -13,6 +12,26 @@ import { error as errorLogger, } from './middleware/logging'; import tenantGraphMiddleware from 'talk-server/graph/tenant/middleware'; +import managementGraphMiddleware from 'talk-server/graph/management/middleware'; + +async function createManagementRouter(config: Config, db: Db): Promise { + const router = express.Router(); + + if (config.get('env') === 'development') { + // GraphiQL + router.get( + '/graphiql', + playground(() => ({ + endpoint: `/api/management/graphql`, + })) + ); + } + + // Tenant API + router.use('/graphql', express.json(), managementGraphMiddleware(db)); + + return router; +} async function createTenantRouter(config: Config, db: Db): Promise { const router = express.Router({ mergeParams: true }); @@ -40,6 +59,9 @@ async function createAPIRouter(config: Config, db: Db): Promise { // Configure the tenant routes. router.use('/tenant/:tenantID', await createTenantRouter(config, db)); + // Configure the management routes. + router.use('/management', await createManagementRouter(config, db)); + return router; } diff --git a/src/core/server/graph/tenant/resolvers/cursor.ts b/src/core/server/graph/common/scalars/cursor.ts similarity index 100% rename from src/core/server/graph/tenant/resolvers/cursor.ts rename to src/core/server/graph/common/scalars/cursor.ts diff --git a/src/core/server/graph/management/context.ts b/src/core/server/graph/management/context.ts new file mode 100644 index 000000000..60f113f37 --- /dev/null +++ b/src/core/server/graph/management/context.ts @@ -0,0 +1,13 @@ +import { Db } from 'mongodb'; + +export interface ContextOptions { + db: Db; +} + +export default class TenantContext { + public db: Db; + + constructor({ db }: ContextOptions) { + this.db = db; + } +} diff --git a/src/core/server/graph/management/middleware.ts b/src/core/server/graph/management/middleware.ts new file mode 100644 index 000000000..95013eba2 --- /dev/null +++ b/src/core/server/graph/management/middleware.ts @@ -0,0 +1,12 @@ +import { graphqlExpress } from 'apollo-server-express'; +import schema from './schema'; +import Context from './context'; +import { Db } from 'mongodb'; + +export default (db: Db) => + graphqlExpress(async req => { + return { + schema, + context: new Context({ db }), + }; + }); diff --git a/src/core/server/graph/management/schema/index.ts b/src/core/server/graph/management/schema/index.ts new file mode 100644 index 000000000..64bc74fc4 --- /dev/null +++ b/src/core/server/graph/management/schema/index.ts @@ -0,0 +1,18 @@ +import { addMockFunctionsToSchema } from 'graphql-tools'; +import { getGraphQLProjectConfig } from 'graphql-config'; + +// Load the configuration from the provided `.graphqlconfig` file. +const config = getGraphQLProjectConfig(__dirname, 'management'); + +// Get the GraphQLSchema from the configuration. +const schema = config.getSchema(); + +// Attach resolvers to the schema. +addMockFunctionsToSchema({ + schema, + mocks: { + Cursor: () => new Date().toISOString(), + }, +}); // FIXME: remove mocks + +export default schema; diff --git a/src/core/server/graph/management/schema/schema.graphql b/src/core/server/graph/management/schema/schema.graphql new file mode 100644 index 000000000..67655516d --- /dev/null +++ b/src/core/server/graph/management/schema/schema.graphql @@ -0,0 +1,39 @@ +################################################################################ +## Custom Scalar Types +################################################################################ + +""" +Time represented as an ISO8601 string. +""" +scalar Time + +""" +Cursor represents a paginating cursor. +""" +scalar Cursor + +################################################################################ +## Tenant +################################################################################ + +type Tenant { + id: ID! + + """ + organizationName is the name of the organization. + """ + organizationName: String + + """ + organizationContactEmail is the email of the organization. + """ + organizationContactEmail: String +} + +################################################################################ +## Query +################################################################################ + +type Query { + tenant(id: ID!): Tenant +} diff --git a/src/core/server/graph/tenant/resolvers/index.ts b/src/core/server/graph/tenant/resolvers/index.ts index a11f73bae..5b75890a8 100644 --- a/src/core/server/graph/tenant/resolvers/index.ts +++ b/src/core/server/graph/tenant/resolvers/index.ts @@ -1,6 +1,6 @@ import Asset from './asset'; import Comment from './comment'; -import Cursor from './cursor'; +import Cursor from '../../common/scalars/cursor'; import Query from './query'; export default { diff --git a/src/core/server/graph/tenant/schema/index.ts b/src/core/server/graph/tenant/schema/index.ts index 250991cbe..53b72d385 100644 --- a/src/core/server/graph/tenant/schema/index.ts +++ b/src/core/server/graph/tenant/schema/index.ts @@ -2,7 +2,7 @@ import { addMockFunctionsToSchema, addResolveFunctionsToSchema, } from 'graphql-tools'; -import resolvers from 'talk-server/graph/tenant/resolvers'; +import resolvers from '../resolvers'; import { getGraphQLProjectConfig } from 'graphql-config'; // Load the configuration from the provided `.graphqlconfig` file. @@ -14,12 +14,4 @@ const schema = config.getSchema(); // Attach the resolvers to the schema. addResolveFunctionsToSchema({ schema, resolvers }); -// // Attach resolvers to the schema. -// addMockFunctionsToSchema({ -// schema, -// mocks: { -// Cursor: () => new Date().toISOString(), -// }, -// }); // FIXME: remove mocks - export default schema;