Skip to content

Commit

Permalink
fix: use resolver for lists (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jul 31, 2023
1 parent e572add commit e587dce
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/util/getGraphQLResolverMap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {camelCase} from 'lodash'
import {CreateResolversArgs} from 'gatsby'
import {GraphQLFieldResolver} from 'gatsby/graphql'
import {GatsbyResolverMap, GatsbyGraphQLContext} from '../types/gatsby'
import {GatsbyResolverMap, GatsbyGraphQLContext, GatsbyNodeModel} from '../types/gatsby'
import {TypeMap, FieldDef} from './remoteGraphQLSchema'
import {resolveReferences} from './resolveReferences'
import {PluginConfig} from './validateConfig'
import {getConflictFreeFieldName, getTypeName} from './normalize'
import {SanityRef} from '../types/sanity'

export function getGraphQLResolverMap(
typeMap: TypeMap,
Expand All @@ -16,6 +18,19 @@ export function getGraphQLResolverMap(
const objectType = typeMap.objects[typeName]
const fieldNames = Object.keys(objectType.fields)

// Add resolvers for lists
resolvers[objectType.name] = fieldNames
.map((fieldName) => ({fieldName, ...objectType.fields[fieldName]}))
.filter((field) => field.isList)
.reduce((fields, field) => {
const targetField = objectType.isDocument
? getConflictFreeFieldName(field.fieldName, pluginConfig.typePrefix)
: field.fieldName

fields[targetField] = {resolve: getResolver(field, pluginConfig.typePrefix)}
return fields
}, resolvers[objectType.name] || {})

// Add raw resolvers
resolvers[objectType.name] = fieldNames
.map((fieldName) => ({fieldName, ...objectType.fields[fieldName]}))
Expand All @@ -29,6 +44,39 @@ export function getGraphQLResolverMap(
return resolvers
}

function getResolver(
field: FieldDef & {fieldName: string},
typePrefix?: string,
): GraphQLFieldResolver<{[key: string]: any}, GatsbyGraphQLContext> {
return (source, args, context) => {
if (field.isList) {
const items: SanityRef[] = source[field.fieldName] || []
return items && Array.isArray(items)
? items.map((item) => maybeResolveReference(item, context.nodeModel, typePrefix))
: []
}

const item: SanityRef | undefined = source[field.fieldName]
return maybeResolveReference(item, context.nodeModel, typePrefix)
}
}

function maybeResolveReference(
item: {_ref?: string; _type?: string; internal?: {}} | undefined,
nodeModel: GatsbyNodeModel,
typePrefix?: string,
) {
if (item && typeof item._ref === 'string') {
return nodeModel.getNodeById({id: item._ref})
}

if (item && typeof item._type === 'string' && !item.internal) {
return {...item, internal: {type: getTypeName(item._type, typePrefix)}}
}

return item
}

function getRawResolver(
field: FieldDef & {fieldName: string},
pluginConfig: PluginConfig,
Expand Down

1 comment on commit e587dce

@vercel
Copy link

@vercel vercel bot commented on e587dce Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gatsby-source-sanity – ./

gatsby-source-sanity.sanity.build
gatsby-source-sanity-git-main.sanity.build

Please sign in to comment.