-
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): Spike out contentful provider and basic renderi…
…ng (#24655) * feat(gatsby-recipes): Spike out contentful provider and basic rendering * Update packages/gatsby-recipes/src/gui.js
- Loading branch information
Showing
9 changed files
with
225 additions
and
31 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
66 changes: 66 additions & 0 deletions
66
packages/gatsby-recipes/src/providers/contentful/environment.js
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,66 @@ | ||
import Joi from "@hapi/joi" | ||
|
||
import client from "./client" | ||
import space from "./client" | ||
import resourceSchema from "../resource-schema" | ||
|
||
const create = async (context, { name }) => { | ||
const spaceId = context.ContentfulSpace.id | ||
const space = await client.getSpace(spaceId) | ||
const environment = await space.createEnvironment({ name }) | ||
|
||
return { | ||
name: environment.name, | ||
id: environment.sys.id, | ||
_message: message(environment), | ||
} | ||
} | ||
|
||
const read = async (context, name) => { | ||
console.log({ context, name }) | ||
|
||
return undefined | ||
} | ||
|
||
const destroy = async (_context, id) => {} | ||
|
||
const all = async () => {} | ||
|
||
const schema = { | ||
name: Joi.string(), | ||
...resourceSchema, | ||
} | ||
|
||
const validate = resource => | ||
Joi.validate(resource, schema, { abortEarly: false }) | ||
|
||
const plan = async (context, { id, name }) => { | ||
console.log({ context, name, id }) | ||
const currentResource = await read(context, id || name) | ||
|
||
if (!currentResource) { | ||
return { | ||
currentState: ``, | ||
describe: `Create Contentful environment ${name}`, | ||
diffExists: true, | ||
skipDiff: true, | ||
} | ||
} else { | ||
return { | ||
currentState: currentResource, | ||
describe: `Contentful environment ${name} already exists`, | ||
diff: ``, | ||
} | ||
} | ||
} | ||
|
||
const message = resource => `Created Contentful environment "${resource.name}"` | ||
|
||
module.exports.schema = schema | ||
module.exports.validate = validate | ||
module.exports.plan = plan | ||
module.exports.create = create | ||
module.exports.read = read | ||
module.exports.update = create | ||
module.exports.destroy = destroy | ||
module.exports.all = all |
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,69 @@ | ||
import Joi from "@hapi/joi" | ||
|
||
import client from "./client" | ||
import space from "./client" | ||
import resourceSchema from "../resource-schema" | ||
import getGraphqlFields from "../utils/get-graphql-fields" | ||
|
||
const GRAPHQL_FIELD_OPTIONS = { | ||
metadata: [`type`, `name`], | ||
} | ||
|
||
const create = async (context, { schema }) => { | ||
const spaceId = context.ContentfulSpace.id | ||
const space = await client.getSpace(spaceId) | ||
|
||
const fields = getGraphqlFields(schema, GRAPHQL_FIELD_OPTIONS)[0] | ||
const contentType = await space.createContentTypeWithId(fields.name, fields) | ||
await contentType.publish() | ||
|
||
return { | ||
name: contentType.name, | ||
id: contentType.sys.id, | ||
_message: message(contentType), | ||
} | ||
} | ||
|
||
const read = async (context, name) => {} | ||
|
||
const destroy = async (_context, id) => {} | ||
|
||
const all = async () => {} | ||
|
||
const schema = { | ||
schema: Joi.string(), | ||
...resourceSchema, | ||
} | ||
|
||
const validate = resource => | ||
Joi.validate(resource, schema, { abortEarly: false }) | ||
|
||
const plan = async (context, { id, schema }) => { | ||
const currentResource = await read(context, id) | ||
|
||
if (!currentResource) { | ||
return { | ||
currentState: ``, | ||
describe: `Create Contentful type`, | ||
diffExists: true, | ||
skipDiff: true, | ||
} | ||
} else { | ||
return { | ||
currentState: currentResource, | ||
describe: `Contentful type ${currentResource.name} already exists`, | ||
diff: ``, | ||
} | ||
} | ||
} | ||
|
||
const message = resource => `Created Contentful type "${resource.name}"` | ||
|
||
module.exports.schema = schema | ||
module.exports.validate = validate | ||
module.exports.plan = plan | ||
module.exports.create = create | ||
module.exports.read = read | ||
module.exports.update = create | ||
module.exports.destroy = destroy | ||
module.exports.all = all |
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
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
Oops, something went wrong.