-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-recipes): add GatsbyPage provider (#25248)
* WIP: Add GatsbyPage provider to recipes * Use pages.values() * Store port for develop proxy in service lock file * Export UnlockFn from service-lock.ts * Make fetching gatsby pages from the recipes graphql API work * Implement fetching single Gatsbypage * Update test snapshots * async/await style
- Loading branch information
Showing
5 changed files
with
171 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
const Joi = require(`@hapi/joi`) | ||
const { getService } = require(`gatsby-core-utils/dist/service-lock`) | ||
const fetch = require(`node-fetch`) | ||
|
||
const { REQUIRES_KEYS } = require(`./utils/constants`) | ||
const resourceSchema = require(`../resource-schema`) | ||
|
||
module.exports.create = () => {} | ||
module.exports.update = () => {} | ||
module.exports.read = async ({ root }, id) => { | ||
const result = await queryDevelopAPI( | ||
{ root }, | ||
` | ||
{ | ||
sitePage(id: { eq: "${id}" }) { | ||
path | ||
component | ||
internalComponentName | ||
componentChunkName | ||
matchPath | ||
id | ||
componentPath | ||
isCreatedByStatefulCreatePages | ||
} | ||
} | ||
` | ||
) | ||
|
||
return result.data.sitePage | ||
} | ||
module.exports.destroy = () => {} | ||
module.exports.config = {} | ||
|
||
const queryDevelopAPI = async ({ root }, query) => { | ||
const { port } = await getService(root, `developproxy`) | ||
|
||
const res = await fetch(`http://localhost:${port}/___graphql`, { | ||
method: `POST`, | ||
body: JSON.stringify({ | ||
query, | ||
}), | ||
headers: { | ||
Accept: `application/json`, | ||
"Content-Type": `application/json`, | ||
}, | ||
}) | ||
const body = await res.json() | ||
|
||
return body | ||
} | ||
|
||
module.exports.all = async ({ root }) => { | ||
const result = await queryDevelopAPI( | ||
{ root }, | ||
` | ||
{ | ||
allSitePage { | ||
nodes { | ||
path | ||
component | ||
internalComponentName | ||
componentChunkName | ||
matchPath | ||
id | ||
componentPath | ||
isCreatedByStatefulCreatePages | ||
} | ||
} | ||
} | ||
` | ||
) | ||
|
||
return result.data.allSitePage.nodes | ||
} | ||
const schema = { | ||
internalComponentName: Joi.string(), | ||
path: Joi.string(), | ||
matchPath: Joi.string().optional(), | ||
component: Joi.string(), | ||
componentChunkName: Joi.string(), | ||
isCreatedByStatefulCreatePages: Joi.boolean(), | ||
pluginCreatorId: Joi.string(), | ||
componentPath: Joi.string(), | ||
...resourceSchema, | ||
} | ||
|
||
const validate = resource => { | ||
if (REQUIRES_KEYS.includes(resource.name) && !resource.key) { | ||
return { | ||
error: `${resource.name} requires a key to be set`, | ||
} | ||
} | ||
|
||
if (resource.key && resource.key === resource.name) { | ||
return { | ||
error: `${resource.name} requires a key to be different than the plugin name`, | ||
} | ||
} | ||
|
||
return Joi.validate(resource, schema, { abortEarly: false }) | ||
} | ||
|
||
exports.schema = schema | ||
exports.validate = validate | ||
|
||
module.exports.plan = async () => { | ||
return {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters