test: test for basic mutations and schema rebuilding

parent f32bb344
Pipeline #60710801 passed with stages
in 3 minutes and 15 seconds
const express = require('express')
const graphqlHTTP = require('express-graphql')
const {
buildSchema,
GraphQLObjectType,
GraphQLSchema,
GraphQLInt,
......@@ -13,32 +12,59 @@ const app = express()
// Resolver
const hello = () => 'Hello world!'
const hello2 = () => 'Hello world!2'
const foo = (_, { bar }) => {
return bar
}
let queries = {
hello: {
type: GraphQLString,
resolve: hello
}
}
// Schema
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
description: 'Possible queries',
fields: {
hello: {
type: GraphQLString,
resolve: hello
let mutations = {
createFoo: {
type: GraphQLString,
args: {
bar: {
type: GraphQLString
}
}
},
resolve: foo
}
}
/**
* Builds and returns new schema
*/
const buildSchema = () =>
new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
description: 'Possible queries',
fields: queries
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
description: 'Possible mutations',
fields: mutations
})
})
})
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
'/',
graphqlHTTP(async (req, res) => ({
schema: buildSchema(),
graphiql: !process.env.TESTING
})
}))
)
app.get('/', (req, res) => {
res.status(200).send('Hello World!')
})
queries.hello2 = {
type: GraphQLString,
resolve: hello2
}
if (!process.env.TESTING) {
app.listen(port, () => console.log(`Server runs on port ${port}!`))
......
const request = require('supertest')
const express = require('express')
const axios = require('axios')
const app = require('./server')
test('server is online', () => {
test('it should allow basic queries', async done => {
const expected = { data: { hello: 'Hello world!' } }
request(app)
.get('/')
.post('/')
.set('Content-Type', 'application/graphql')
.send('query { hello }')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual(expected)
done()
})
})
test('hello world from graphql', async done => {
const expected = { data: { hello: 'Hello world!' } }
test('it should build schema on each api request', async done => {
const expected = { data: { hello2: 'Hello world!2' } }
request(app)
.post('/')
.set('Content-Type', 'application/graphql')
.send('query { hello2 }')
.then(response => {
expect(response.body).toEqual(expected)
done()
})
})
test('it should allow basic mutations', async done => {
const expected = { data: { createFoo: 'baz' } }
request(app)
.post('/graphql')
.send({ query: '{hello}' })
.post('/')
.set('Content-Type', 'application/graphql')
.send('mutation { createFoo(bar: "baz") }')
.then(response => {
expect(response.body).toEqual(expected)
done()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment