Skip to content

Commit

Permalink
feat(server): Allow to provide custom mutations.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdesmares committed Apr 2, 2020
1 parent d7bb479 commit 1c54af8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/getStandAloneServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ exports.__esModule = true;
var index_1 = require("./../lib/index");
var express_1 = __importDefault(require("express"));
var spdy_1 = __importDefault(require("spdy"));
function getStandAloneServer(config) {
function getStandAloneServer(config, gsgParams, customMutations) {
if (gsgParams === void 0) { gsgParams = {}; }
if (customMutations === void 0) { customMutations = {}; }
return __awaiter(this, void 0, void 0, function () {
var app, server, port;
var _this = this;
return __generator(this, function (_a) {
app = express_1["default"]();
server = index_1.getApolloServer(config);
server = index_1.getApolloServer(config, gsgParams, customMutations);
server.applyMiddleware({
app: app,
path: '/graphql'
Expand Down
5 changes: 3 additions & 2 deletions lib/graphql/getApolloServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ var pipeline_1 = __importDefault(require("./pipeline"));
* @param dbConfig Sequelize database configuration object
* @param gsgParams Params from graphql-sequelize-generator that overwrite the default ones.
*/
function getApolloServer(dbConfig, gsgParams) {
function getApolloServer(dbConfig, gsgParams, customMutations) {
if (gsgParams === void 0) { gsgParams = {}; }
if (customMutations === void 0) { customMutations = {}; }
var models = models_1["default"](dbConfig);
var types = graphql_sequelize_generator_1.generateModelTypes(models);
var graphqlSchemaDeclaration = {
Expand All @@ -43,6 +44,6 @@ function getApolloServer(dbConfig, gsgParams) {
// ],
// Be sure to enable tracing
tracing: false
}, customMutations: {} }, gsgParams));
}, customMutations: customMutations }, gsgParams));
}
exports["default"] = getApolloServer;
8 changes: 6 additions & 2 deletions src/getStandAloneServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { getApolloServer } from './../lib/index'
import express from 'express'
import http from 'spdy'

export default async function getStandAloneServer(config: SequelizeConfig) {
export default async function getStandAloneServer(
config: SequelizeConfig,
gsgParams: any = {},
customMutations: any = {}
) {
const app = express()
const server = getApolloServer(config)
const server = getApolloServer(config, gsgParams, customMutations)

server.applyMiddleware({
app,
Expand Down
8 changes: 6 additions & 2 deletions src/graphql/getApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import pipeline from './pipeline'
* @param dbConfig Sequelize database configuration object
* @param gsgParams Params from graphql-sequelize-generator that overwrite the default ones.
*/
export default function getApolloServer(dbConfig: any, gsgParams: any = {}) {
export default function getApolloServer(
dbConfig: any,
gsgParams: any = {},
customMutations: any = {}
) {
const models = getModels(dbConfig)

const types = generateModelTypes(models)
Expand All @@ -38,7 +42,7 @@ export default function getApolloServer(dbConfig: any, gsgParams: any = {}) {
// Be sure to enable tracing
tracing: false
},
customMutations: {},
customMutations,
...gsgParams
})
}
8 changes: 8 additions & 0 deletions tests/__snapshots__/job.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ Object {
}
`;

exports[`Test the job endpoint One can add his own mutations to the schema 1`] = `
Object {
"customAcquire": Object {
"id": 1,
},
}
`;

exports[`Test the job endpoint One can create a job of a given type. 1`] = `
Object {
"jobCreate": Object {
Expand Down
21 changes: 21 additions & 0 deletions tests/job.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ const jobCreate = variables => ({
operationName: null
})

const customAcquire = variables => ({
query: `mutation($typeList: [String!]!) {
customAcquire(
typeList: $typeList
) {
id
}
}`,
variables,
operationName: null
})

/**
* Starting the tests
*/
Expand Down Expand Up @@ -360,4 +372,13 @@ describe('Test the job endpoint', () => {
const { createdAt, ...rest } = response
expect(rest).toMatchSnapshot()
})

it('One can add his own mutations to the schema', async () => {
const response = await request(server)
.post('/graphql')
.send(customAcquire({ typeList: ['c'] }))

expect(response.body.errors).toBeUndefined()
expect(response.body.data).toMatchSnapshot()
})
})
36 changes: 35 additions & 1 deletion tests/test-database.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const path = require('path')
const fs = require('fs')
const Umzug = require('umzug')
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLList
} = require('graphql')

const { getStandAloneServer, getModels } = require('./../lib/index')

var dbConfig = require(path.join(__dirname, '/sqliteTestConfig.js')).test
Expand Down Expand Up @@ -104,5 +111,32 @@ exports.closeEverything = async (mainServer, models, done) => {
}

exports.getNewServer = () => {
return getStandAloneServer(dbConfig)
return getStandAloneServer(
dbConfig,
{},
// You can add custom mutations if needed
{
customAcquire: {
type: new GraphQLObjectType({
name: 'customAcquire',
fields: {
id: { type: GraphQLInt }
}
}),
args: {
typeList: {
type: new GraphQLList(GraphQLString)
}
},
resolve: async (source, args, context) => {
// GNJ models can be retreived with the dbConfig if needed
// const models = getModels(dbConfig)
// get a job from the db
const job = await models.job.findByPk(1)

return job
}
}
}
)
}

0 comments on commit 1c54af8

Please sign in to comment.