added mock tenant api

This commit is contained in:
Wyatt Johnson
2018-06-16 17:59:21 -06:00
parent 4318e1ddbe
commit 7177988059
9 changed files with 110 additions and 11 deletions
+3
View File
@@ -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"
}
}
}
+23 -1
View File
@@ -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<Router> {
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<Router> {
const router = express.Router({ mergeParams: true });
@@ -40,6 +59,9 @@ async function createAPIRouter(config: Config, db: Db): Promise<Router> {
// 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;
}
@@ -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;
}
}
@@ -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 }),
};
});
@@ -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;
@@ -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
}
@@ -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 {
+1 -9
View File
@@ -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;