Skip to content

Instantly share code, notes, and snippets.

@verystack
Created August 3, 2021 14:46
Show Gist options
  • Select an option

  • Save verystack/70111e906568554c2abc27ea96b1b1ed to your computer and use it in GitHub Desktop.

Select an option

Save verystack/70111e906568554c2abc27ea96b1b1ed to your computer and use it in GitHub Desktop.
// Here's what I'm implementing typewise to stop dynamically defined stuff throwing errors on MatchKicks
// The important things about the syntax are:
// The type that implements Node (ParsedSneaker here) needs to be the same as your Gatsby type itself.
// You don't need to define things that aren't necessary - for ParsedSneaker, there is way more fields than just sneakerSettings
// - but that's the only one that is potentially missing data
// Syntax:
// Every JSON object needs a type for strict validation
// You create the type and then add it to your existing type - like on ParsedSneaker,
// I implement a type called SneakerSettings on the sneakerSettings key
// The SneakerSettings type contains all the fields that I want to ensure exist in the schema
//
// You can define an array of whatever (Boolean/String/Object(type)/etc) by adding [] around the primitive or type.
// I.e. if we had an array of SneakerSettings type on the sneakerSettings key - we'd go sneakerSettings: [SneakerSettings]
//
// Your primitive types - i.e. boolean/string/etc are always uppercased - i.e. Boolean, String.
//
// If you want to force that under no circumstances can a type/primitive return null, add an exclamation mark to it - i.e. String!
// This means Gatsby will throw an error if there is no string available for a certain field.
// Probably not super useful, but good if you are writing queries themselves on templates so you can make sure you're always
// giving the correct pagecontext to the template.
//
// Once you've defined all the nestings and fields, pass the typeDefs to createTypes and Gatsby will make them always available on the Schema.
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type ParsedSneaker implements Node {
sneakerSettings: SneakerSettings
}
type SneakerSettings {
age: String
bestFor: String
gender: String
skipFirst: Boolean
mcolors: [MColors]
}
type MColors {
color: String
type: String
pattern: Pattern
}
type Pattern {
link: String
}
type ParsedDesign implements Node {
designSettings: DesignSettings
}
type DesignSettings {
layers: LayerSet
}
type LayerSet {
l1: LayerSingle
l2: LayerSingle
l3: LayerSingle
l4: LayerSingle
l5: LayerSingle
l6: LayerSingle
}
type LayerSingle {
id: String
label: String
}
`
createTypes(typeDefs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment