Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const customTypeJsonToGraphQLSchema = (customTypeId, json) => {
const { uid, ...dataFields } = R.pipe(
R.values,
R.mergeAll,
R.mapObjIndexed(fieldToGraphQLType(customTypeId)),
)(json)
const fields = {
dataString: { type: new GraphQLNonNull(GraphQLString) },
first_publication_date: { type: new GraphQLNonNull(GraphQLDate) },
href: { type: new GraphQLNonNull(GraphQLString) },
id: { type: new GraphQLNonNull(GraphQLString) },
lang: { type: new GraphQLNonNull(GraphQLString) },
last_publication_date: { type: new GraphQLNonNull(GraphQLDate) },
tags: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)) },
tags: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)) },
type: { type: new GraphQLNonNull(GraphQLString) },
data: {
type: new GraphQLObjectType({
name: generateNamespacedTypeName(customTypeId, 'Data'),
fields: dataFields,
}),
},
}
if (uid) fields.uid = uid
// GraphQL type must match source plugin type.
const queryType = new GraphQLObjectType({
name: generatePublicTypeName(customTypeId),
fields,
})
const GraphQLPrismicImage = new GraphQLObjectType({
name: generateNamespacedTypeName('Image'),
fields: {
alt: { type: new GraphQLNonNull(GraphQLString) },
copyright: { type: new GraphQLNonNull(GraphQLString) },
dimensions: { type: GraphQLPrismicImageDimensions },
url: { type: GraphQLImageURL },
},
})
const GraphQLPrismicLink = new GraphQLObjectType({
name: generateNamespacedTypeName('Link'),
fields: {
id: { type: new GraphQLNonNull(GraphQLString) },
link_type: { type: new GraphQLNonNull(GraphQLString) },
url: { type: new GraphQLNonNull(GraphQLString) },
target: { type: new GraphQLNonNull(GraphQLString) },
},
})
// Returns a GraphQL type for a given schema field.
const fieldToGraphQLType = (customTypeId, options = {}) => (field, fieldId) => {
switch (field.type) {
case 'Color':
case 'Select':
case 'Text':
case 'UID':
return { type: new GraphQLNonNull(GraphQLString) }
case 'StructuredText':
return { type: GraphQLPrismicHTML }
const fieldToGraphQLType = (customTypeId, options = {}) => (field, fieldId) => {
switch (field.type) {
case 'Color':
case 'Select':
case 'Text':
case 'UID':
return { type: new GraphQLNonNull(GraphQLString) }
case 'StructuredText':
return { type: GraphQLPrismicHTML }
case 'Number':
return { type: new GraphQLNonNull(GraphQLFloat) }
case 'Date':
case 'Timestamp':
return { type: new GraphQLNonNull(GraphQLDate) }
case 'GeoPoint':
return { type: GraphQLPrismicGeoPoint }
case 'Embed':
return { type: GraphQLPrismicEmbed }
case 'Image':
return { type: GraphQLPrismicImage }
case 'Link':
return { type: GraphQLPrismicLink }
}
if (shouldAddFragments) {
const program = store.getState().program
await copy(
path.join(__dirname, '..', 'fragments', 'imageFragments.js'),
`${program.directory}/.cache/fragments/sanity-image-fragments.js`,
)
}
}
const resolveReferencesConfig = new GraphQLInputObjectType({
name: 'SanityResolveReferencesConfiguration',
fields: {
maxDepth: {
type: new GraphQLNonNull(GraphQLInt),
description: 'Max depth to resolve references to',
},
},
})
export const setFieldsOnGraphQLNodeType = async (
context: GatsbyContext & GatsbyOnNodeTypeContext,
pluginConfig: PluginConfig,
) => {
const {type} = context
const typeMapKey = getCacheKey(pluginConfig, CACHE_KEYS.TYPE_MAP)
const typeMap = (stateCache[typeMapKey] || defaultTypeMap) as TypeMap
const schemaType = typeMap.objects[type.name]
let fields: {[key: string]: GraphQLFieldConfig} = {}
switch (field.type) {
case 'Color':
case 'Select':
case 'Text':
case 'UID':
return { type: new GraphQLNonNull(GraphQLString) }
case 'StructuredText':
return { type: GraphQLPrismicHTML }
case 'Number':
return { type: new GraphQLNonNull(GraphQLFloat) }
case 'Date':
case 'Timestamp':
return { type: new GraphQLNonNull(GraphQLDate) }
case 'GeoPoint':
return { type: GraphQLPrismicGeoPoint }
case 'Embed':
return { type: GraphQLPrismicEmbed }
case 'Image':
return { type: GraphQLPrismicImage }
case 'Link':
return { type: GraphQLPrismicLink }
case 'Group':
const group = R.pipe(
R.path(['config', 'fields']),
provider_name: { type: new GraphQLNonNull(GraphQLString) },
provider_url: { type: new GraphQLNonNull(GraphQLString) },
thumbnail_height: { type: new GraphQLNonNull(GraphQLString) },
thumbnail_url: { type: new GraphQLNonNull(GraphQLString) },
thumbnail_width: { type: new GraphQLNonNull(GraphQLString) },
title: { type: new GraphQLNonNull(GraphQLString) },
type: { type: new GraphQLNonNull(GraphQLString) },
version: { type: new GraphQLNonNull(GraphQLString) },
},
})
const GraphQLPrismicImageDimensions = new GraphQLObjectType({
name: generateNamespacedTypeName('Image', 'Dimensions'),
fields: {
width: { type: new GraphQLNonNull(GraphQLInt) },
height: { type: new GraphQLNonNull(GraphQLInt) },
},
})
const GraphQLPrismicImage = new GraphQLObjectType({
name: generateNamespacedTypeName('Image'),
fields: {
alt: { type: new GraphQLNonNull(GraphQLString) },
copyright: { type: new GraphQLNonNull(GraphQLString) },
dimensions: { type: GraphQLPrismicImageDimensions },
url: { type: GraphQLImageURL },
},
})
const GraphQLPrismicLink = new GraphQLObjectType({
name: generateNamespacedTypeName('Link'),
fields: {