Skip to content

Commit

Permalink
allow multiple server display via option (#381)
Browse files Browse the repository at this point in the history
* allow multiple server display via option

* changed comment

* fixed tests
  • Loading branch information
newhouse authored Jun 1, 2022
1 parent 441d8e3 commit 66fea15
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 4 deletions.
6 changes: 6 additions & 0 deletions config-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ spectaql:
# Default: true
errorOnInterpolationReferenceNotFound: true

# Would you like to display all the servers listed in the servers area of your config? Otherwise
# it will try to display just the one marked "production: true".
#
# Default: false
displayAllServers: false


introspection:
##############################################
Expand Down
3 changes: 3 additions & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
spectaql:
logoFile: ./test/fixtures/logo.png
faviconFile: ./test/fixtures/favicon.png
displayAllServers: true

introspection:
removeTrailingPeriodFromDescriptions: false
Expand Down Expand Up @@ -29,6 +30,8 @@ info:
description: Some important stuff we wanted you to know. Supports `markdown`

servers:
- url: https://staging.example.com/graphql
description: Staging
- url: https://example.com/graphql
description: Production
production: true
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const defaults = Object.freeze({
// is set, then use these:
const spectaqlOptionDefaults = Object.freeze({
errorOnInterpolationReferenceNotFound: true,
displayAllServers: false,
})

const introspectionOptionDefaults = Object.freeze({
Expand Down
3 changes: 2 additions & 1 deletion src/spectaql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ function run(opts) {

const server =
servers.find((server) => server.production === true) || servers[0]

const headers = server?.headers
? server.headers
.map((header) => {
const { name, example, comment } = header
if (name && example) {
return [comment ? `// ${comment}` : '', `${name}: ${example}`]
return [comment ? `# ${comment}` : '', `${name}: ${example}`]
.filter(Boolean)
.join('\n')
}
Expand Down
36 changes: 36 additions & 0 deletions src/themes/default/helpers/generateApiEndpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = function (options) {
const { url, server, servers } = options?.data?.root || {}

const displayAllServers =
options?.data?.root?.allOptions?.specData?.spectaql?.displayAllServers ===
true
if (!displayAllServers) {
if (url) {
return url
}
if (server?.url) {
return server.url
}
const fallbackServer =
servers?.find((server) => server.url && server.production) ||
servers?.find((server) => server.url)
if (fallbackServer) {
return fallbackServer.url
}
}

if (!servers?.length) {
return '<<url is missing>>'
}

return servers
.map(
(server) =>
'# ' +
(server.description?.trim() || 'Endpoint') +
':\n' +
(server.url?.trim() || '<<url is missing>>') +
'\n'
)
.join('')
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
</div>
{{/if}}

{{#if url}}
{{#if (or url server servers)}}
<div class="example-section welcome-api-endpoints-section example-section-is-code">
<h5>API Endpoints</h5>
<pre><code>{{url}}</code></pre>
<pre><code>{{generateApiEndpoints}}</code></pre>
</div>
{{/if}}

Expand Down
86 changes: 86 additions & 0 deletions test/src/helpers/generateApiEndpoints.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const generateApiEndpoints = require('../../../dist/themes/default/helpers/generateApiEndpoints')

describe('generateApiEndpoints', function () {
def('displayAllServers', false)
def('production', true)

def('options', () => ({
data: {
root: {
allOptions: {
specData: {
spectaql: {
displayAllServers: $.displayAllServers,
},
},
},
url: $.url,
server: $.server,
servers: $.servers,
},
},
}))

def('url', () => 'foorl')
def('server', () => ({
url: 'serverFoorl',
}))
def('servers', () => [
{
url: 'server1url',
description: 'Server One',
},
{
url: 'server2url',
description: 'Server Two',
production: $.production,
},
])

context('displayAllServers is false', function () {
it('uses url', function () {
expect(generateApiEndpoints($.options)).to.eql('foorl')
})

context('url is falsy', function () {
def('url', () => undefined)

it('uses server.url', function () {
expect(generateApiEndpoints($.options)).to.eql('serverFoorl')
})

context('server is falsy', function () {
def('server', () => undefined)

it('uses production server url', function () {
expect(generateApiEndpoints($.options)).to.eql('server2url')
})

context('production is false', function () {
def('production', () => undefined)

it('uses first server url', function () {
expect(generateApiEndpoints($.options)).to.eql('server1url')
})
})
})
})
})

context('displayAllServers is true', function () {
def('displayAllServers', () => true)

it('works', function () {
expect(generateApiEndpoints($.options)).to.eql(
'# Server One:\nserver1url\n# Server Two:\nserver2url\n'
)
})

context('servers are empty', function () {
def('servers', () => [])
it('uses default message', function () {
expect(generateApiEndpoints($.options)).to.eql('<<url is missing>>')
})
})
})
})
2 changes: 1 addition & 1 deletion test/src/spectaql/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('index', function () {
const result = spectaql($.opts)
expect(result).to.be.ok
expect(result.headers).to.eql(
`// Get your Foo from Bar\nFoo: Bar <yo>`
`# Get your Foo from Bar\nFoo: Bar <yo>`
)
})
})
Expand Down

0 comments on commit 66fea15

Please sign in to comment.