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

allow multiple server display via option #381

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
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
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