Skip to content

Commit

Permalink
feat: provide headers from the graphql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
timonrey committed Feb 20, 2023
1 parent 22cfbb9 commit ca24802
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
27 changes: 5 additions & 22 deletions api-specs/test/api.raml
Original file line number Diff line number Diff line change
Expand Up @@ -515,27 +515,10 @@ traits:
- action-errorable
- right-header
- frontastic-locale
description: Use the GET method to allow the frontend to fetch data from a backend system. For the response, we recommend to use standard HTTP codes and `application/json` encoded content. The response will be structured [as defined by the `body` property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). The following response example contains information about a cart.
description: Use the GET method to allow the frontend to fetch data from a backend system. For the response, we recommend to use standard HTTP codes and `application/json` encoded content. The response will be structured [as defined by the body property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). The following response example contains information about a cart.
responses:
200:
description: The response will be structured [as defined by the `body` property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). We recommend to use standard HTTP response codes and `application/json` encoded content. The following response example contains information about a cart.
body:
application/json:
type: object
example: !include examples/action-success.json
post:
is:
- action-errorable
- right-header
- frontastic-locale
description: Use the POST method to write data to a backend system. Any JSON serializable payload is accepted. The following request example adds a product to a cart. For the response, we recommend to use standard HTTP codes and `application/json` encoded content. The response will be structured [as defined by the `body` property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). The following response example contains the updated cart information, which includes the added product.
body:
application/json:
type: any
example: !include examples/action-payload.json
responses:
200:
description: The response will be structured [as defined by the `body` property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). We recommend to use standard HTTP response codes and `application/json` encoded content. The following response example contains information about a cart.
description: The response will be structured [as defined by the body property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). We recommend to use standard HTTP response codes and `application/json` encoded content. The following response example contains information about a cart.
body:
application/json:
type: object
Expand All @@ -545,17 +528,17 @@ traits:
- action-errorable
- right-header
- frontastic-locale
description: Use the PUT method to write data to a backend system. Any JSON serializable payload is accepted. The following request example adds a product to a cart. For the response, we recommend to use standard HTTP codes and `application/json` encoded content. The response will be structured [as defined by the `body` property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). The following response example contains the updated cart information, which includes the added product.
description: Use the PUT method to write data to a backend system. Any JSON serializable payload is accepted. The following request example adds a product to a cart. For the response, we recommend to use standard HTTP codes and `application/json` encoded content. The response will be structured [as defined by the body property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). The following response example contains the updated cart information, which includes the added product.
body:
application/json:
type: any
example: !include examples/action-payload.json
responses:
200:
description: The response will be structured [as defined by the `body` property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). We recommend to use standard HTTP response codes and `application/json` encoded content. The following response example contains information about a cart.
description: The response will be structured [as defined by the body property of the action](/../frontend-development/developing-an-action-extension#1-implement-the-action). We recommend to use standard HTTP response codes and `application/json` encoded content. The following response example contains information about a cart.
body:
application/json:
type: any
type: object
example: !include examples/action-success.json


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const Responses = ({ apiKey, responses, contentType }) => {
typeLocations,
response.description
)}
{contentType.length > 0 && (
{contentType.length > 0 && !response.description && (
<>
<span>as</span>
<ContentType>{contentType}</ContentType>
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby-theme-api-docs/src/hooks/use-api-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const useApiResources = () => {
type
}
}
headers {
header
builtinType
description
type
required
pattern
}
responses {
code
description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const defineRamlResource = ({ schema, createTypes }) => {
description: String
queryParameters: [RamlResourceQueryParameter!]
responses: [RamlResourceResponse!]
headers: [RamlResourceHeaders!]
codeExamples: [RamlResourceCodeExample!]
}
`,
Expand Down Expand Up @@ -51,6 +52,7 @@ const defineRamlResource = ({ schema, createTypes }) => {
queryParameters: '[RamlResourceQueryParameter!]',
body: 'RamlResourceMethodBody',
responses: '[RamlResourceResponse!]',
headers: '[RamlResourceHeaders!]',
codeExamples: '[RamlResourceCodeExample!]',
},
interfaces: ['Method'],
Expand All @@ -64,6 +66,7 @@ const defineRamlResource = ({ schema, createTypes }) => {
description: 'String',
queryParameters: '[RamlResourceQueryParameter!]',
responses: '[RamlResourceResponse!]',
headers: '[RamlResourceHeaders!]',
codeExamples: '[RamlResourceCodeExample!]',
},
interfaces: ['Method'],
Expand Down Expand Up @@ -113,6 +116,18 @@ const defineRamlResource = ({ schema, createTypes }) => {
},
}),

schema.buildObjectType({
name: 'RamlResourceHeaders',
fields: {
header: 'String!',
pattern: 'String',
type: 'String',
builtinType: 'String',
description: 'String',
required: 'Boolean',
},
}),

schema.buildObjectType({
name: 'RamlResourceMethodBody',
fields: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function headersToArray(headers) {
if (headers) {
return Object.entries(headers).map(([key, value]) => {
return { header: key, ...value };
});
}

return undefined;
}

module.exports = headersToArray;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const parametersToArray = require('../parameters-to-array');
const responsesToArray = require('./responses-to-array');
const headersToArray = require('./headers-to-array');
const codeExamplesToArray = require('./code-examples-to-array');
const examplesToArray = require('./examples-to-array').examplesToArray;
const resolveExampleFile = require('./examples-to-array').resolveExampleFile;
Expand Down Expand Up @@ -33,6 +34,10 @@ function processMethods({
returnedMethods[method].responses
);

returnedMethods[method].headers = headersToArray(
returnedMethods[method].headers
);

returnedMethods[method].codeExamples = codeExamplesToArray(
returnedMethods[method].codeExamples
);
Expand Down

0 comments on commit ca24802

Please sign in to comment.