Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new tests for graphql-deduplicator #307

Merged
merged 2 commits into from
May 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 151 additions & 13 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,82 @@
import test from 'ava'
import { GraphQLServer } from './index'
import test, { TestContext, Context } from 'ava'
import { inflate } from 'graphql-deduplicator'
import { GraphQLServer, Options } from './index'
import { promisify } from 'util'
import * as request from 'request-promise-native'

test.beforeEach('start hello world', async t => {
async function startServer(
t: TestContext & Context<any>,
options?: Options
) {
const randomId = () => Math.random().toString(36).substr(2, 5)

const typeDefs = `
type Author {
id: ID!
name: String!
lastName: String!
}

type Book {
id: ID!
name: String!
author: Author!
}

type Query {
hello(name: String): String!
hello(name: String): String!
books: [Book!]!
}
`

const author = {
__typename: 'Author',
id: randomId(),
name: 'Jhon',
lastName: 'Doe'
}
const book = {
__typename: 'Book',
id: randomId(),
name: 'Awesome',
author
}
const resolvers = {
Query: {
hello: (_, { name }) => `Hello ${name || 'World'}`,
books: () => Array(5).fill(book)
},
}

const server = new GraphQLServer({ typeDefs, resolvers })

const http = await server.start({ port: 0 })

const http = await server.start({ port: 0, ...options })
const { port } = http.address()
const uri = `http://localhost:${port}/`

t.context.http = http
if (t.context.httpServers) {
t.context.httpServers.push(http)
} else {
t.context.httpServers = [http]
}

t.context.uri = uri
})
t.context.data = { book }

return t.context
}

test.afterEach.always('stop hello world', async t => {
const { http } = t.context
await promisify(http.close).call(http)
test.afterEach.always('stop graphql servers', async t => {
const { httpServers } = t.context

if (httpServers && httpServers.length) {
await Promise.all(
httpServers.map(server => promisify(server.close).call(server))
)
}
})

test('works with simple hello world server', async t => {
const { uri } = t.context
const { uri } = await startServer(t)

const query = `
query {
Expand All @@ -54,3 +97,98 @@ test('works with simple hello world server', async t => {
},
})
})

test('Response data can be deduplicated with graphql-deduplicator', async t => {
const { uri, data: { book } } = await startServer(t)

const query = `
query {
books {
__typename
id
name
author {
__typename
id
name
lastName
}
}
}
`

const body = await request({
uri,
method: 'POST',
json: true,
body: { query },
}).promise()

const deduplicated = await request({
uri,
method: 'POST',
json: true,
body: { query },
headers: {
'X-GraphQL-Deduplicate': true
}
}).promise()

t.deepEqual(body, {
data: {
books: Array(5).fill(book)
}
})

t.deepEqual(deduplicated, {
data: {
books: [
book,
...Array(4).fill({
__typename: book.__typename,
id: book.id
})
]
}
})

t.deepEqual(body.data, inflate(deduplicated.data))
})

test('graphql-deduplicated can be completely disabled', async t => {
const { uri, data: { book } } = await startServer(t, {
deduplicator: false
})

const query = `
query {
books {
__typename
id
name
author {
__typename
id
name
lastName
}
}
}
`

const body = await request({
uri,
method: 'POST',
json: true,
body: { query },
headers: {
'X-GraphQL-Deduplicate': true
}
}).promise()

t.deepEqual(body, {
data: {
books: Array(5).fill(book)
}
})
})