import { Node , SourceNodesArgs } from 'gatsby' ; import debug from '../debug' ; import { SanityInputNode } from '../types/gatsby' ; import { SanityDocument } from '../types/sanity' ; import createNodeManifest from './createNodeManifest' ; import { prefixId , unprefixId } from './documentIds' ; import { getTypeName , ProcessingOptions , toGatsbyNode } from './normalize' ; import { TypeMap } from './remoteGraphQLSchema' ; export type SyncWithGatsby = ( id : string , document ? : SanityDocument ) => void ; returns export default function getSyncWithGatsby ( props ) { const { documents , gatsbyNodes , processingOptions , args } = props ; const { typeMap , overlayDrafts } = processingOptions ; const { reporter , actions } = args ; const { createNode , deleteNode } = actions ; return ( id , updatedDocument ) => { const publishedId = unprefixId ( id ) ; const draftId = prefixId ( id ) ; if ( updatedDocument ) { documents . set ( id , updatedDocument ) ; } const published = documents . get ( publishedId ) ; const draft = documents . get ( draftId ) ; const doc = draft || published ; if ( doc ) { const type = getTypeName ( doc . _type ) ; if ( ! typeMap . objects [ type ] ) { reporter . warn ( `[sanity] Document "${ doc . _id }" has type ${ doc . _type } (${ type }), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.` ) ; return ; } } if ( id === draftId && ! overlayDrafts ) { debug ( 'overlayDrafts is not enabled, so skipping createNode for draft' ) ; return ; } if ( id === publishedId ) { if ( draft && overlayDrafts ) { debug ( 'skipping createNode of %s since there is a draft and overlayDrafts is enabled' , publishedId ) ; return ; } if ( gatsbyNodes . has ( publishedId ) ) { if ( published ) { debug ( 'updating gatsby node for %s' , publishedId ) ; const node = toGatsbyNode ( published , processingOptions ) ; gatsbyNodes . set ( publishedId , node ) ; createNode ( node ) ; createNodeManifest ( actions , args , node , publishedId ) ; } else { debug ( 'deleting gatsby node for %s since there is no draft and overlayDrafts is not enabled' , publishedId ) ; deleteNode ( gatsbyNodes . get ( publishedId ) ! ) ; gatsbyNodes . delete ( publishedId ) ; } } else if ( published ) { debug ( 'creating gatsby node for %s' , publishedId ) ; const node = toGatsbyNode ( published , processingOptions ) ; gatsbyNodes . set ( publishedId , node ) ; createNode ( node ) ; createNodeManifest ( actions , args , node , publishedId ) ; } } if ( id === draftId && overlayDrafts ) { if ( gatsbyNodes . has ( publishedId ) && ! draft && ! published ) { debug ( 'deleting gatsby node for %s since there is neither a draft nor a published version of it any more' , publishedId ) ; deleteNode ( gatsbyNodes . get ( publishedId ) ! ) ; gatsbyNodes . delete ( publishedId ) ; return ; } debug ( 'Replacing gatsby node for %s using the %s document' , publishedId , draft ? 'draft' : 'published' ) ; const node = toGatsbyNode ( ( draft || published ) ! , processingOptions ) ; gatsbyNodes . set ( publishedId , node ) ; createNode ( node ) ; createNodeManifest ( actions , args , node , publishedId ) ; } } ; }