gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
1 lines • 5.72 kB
Source Map (JSON)
{"version":3,"file":"sharedSliceModelToGraphQLType.cjs","sources":["../../../src/lib/sharedSliceModelToGraphQLType.ts"],"sourcesContent":["import type { SharedSlice, SharedSliceModel } from \"@prismicio/client\";\nimport type { GatsbyGraphQLUnionType, NodePluginArgs } from \"gatsby\";\n\nimport type { PluginOptions } from \"../types\";\n\nimport { fieldModelsRecordToGraphQLType } from \"./fieldModelsRecordToGraphQLType\";\nimport { pascalCase } from \"./pascalCase\";\n\ntype SharedSliceModelToGraphQLTypeArgs = {\n\tmodel: SharedSliceModel;\n\tsharedSliceModels: SharedSliceModel[];\n\tgatsbyNodeArgs: NodePluginArgs;\n\tpluginOptions: PluginOptions;\n};\n\nexport const sharedSliceModelToGraphQLType = (\n\targs: SharedSliceModelToGraphQLTypeArgs,\n): GatsbyGraphQLUnionType => {\n\tlet variationTypeNames: string[] = [];\n\n\tfor (const variation of args.model.variations) {\n\t\tconst type = args.gatsbyNodeArgs.schema.buildObjectType({\n\t\t\tname: pascalCase(\n\t\t\t\t\"Prismic\",\n\t\t\t\targs.pluginOptions.typePrefix,\n\t\t\t\targs.model.id,\n\t\t\t\t\"Slice\",\n\t\t\t\tvariation.id,\n\t\t\t),\n\t\t\tdescription: args.model.description,\n\t\t\tfields: {\n\t\t\t\tid: {\n\t\t\t\t\ttype: \"ID!\",\n\t\t\t\t\tresolve: (source: SharedSlice): string => {\n\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\tsource.id ||\n\t\t\t\t\t\t\targs.gatsbyNodeArgs.createNodeId(\n\t\t\t\t\t\t\t\targs.gatsbyNodeArgs.createContentDigest(source),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tslice_type: {\n\t\t\t\t\ttype: \"String!\",\n\t\t\t\t},\n\t\t\t\tslice_label: {\n\t\t\t\t\ttype: \"String\",\n\t\t\t\t},\n\t\t\t\tversion: {\n\t\t\t\t\ttype: \"String!\",\n\t\t\t\t},\n\t\t\t\tvariation: {\n\t\t\t\t\ttype: \"String!\",\n\t\t\t\t},\n\t\t\t},\n\t\t\tinterfaces: [\"PrismicSlice\", \"PrismicSharedSlice\"],\n\t\t});\n\n\t\tif (variation.primary && Object.keys(variation.primary).length > 0) {\n\t\t\tconst primaryType = fieldModelsRecordToGraphQLType({\n\t\t\t\t...args,\n\t\t\t\tpath: [args.model.id, variation.id, \"primary\"],\n\t\t\t\tmodels: variation.primary,\n\t\t\t});\n\t\t\tprimaryType.config.name = pascalCase(\n\t\t\t\t\"Prismic\",\n\t\t\t\targs.pluginOptions.typePrefix,\n\t\t\t\targs.model.id,\n\t\t\t\t\"Slice\",\n\t\t\t\tvariation.id,\n\t\t\t\t\"Primary\",\n\t\t\t);\n\n\t\t\targs.gatsbyNodeArgs.actions.createTypes(primaryType);\n\n\t\t\tif (type.config.fields) {\n\t\t\t\ttype.config.fields.primary = {\n\t\t\t\t\ttype: `${primaryType.config.name}!`,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\tif (variation.items && Object.keys(variation.items).length > 0) {\n\t\t\tconst itemType = fieldModelsRecordToGraphQLType({\n\t\t\t\t...args,\n\t\t\t\tpath: [args.model.id, variation.id, \"items\"],\n\t\t\t\tmodels: variation.items,\n\t\t\t});\n\t\t\titemType.config.name = pascalCase(\n\t\t\t\t\"Prismic\",\n\t\t\t\targs.pluginOptions.typePrefix,\n\t\t\t\targs.model.id,\n\t\t\t\t\"Slice\",\n\t\t\t\tvariation.id,\n\t\t\t\t\"Item\",\n\t\t\t);\n\n\t\t\targs.gatsbyNodeArgs.actions.createTypes(itemType);\n\n\t\t\tif (type.config.fields) {\n\t\t\t\ttype.config.fields.items = {\n\t\t\t\t\ttype: `[${itemType.config.name}!]!`,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\targs.gatsbyNodeArgs.actions.createTypes(type);\n\n\t\tvariationTypeNames = [...variationTypeNames, type.config.name];\n\t}\n\n\tconst type = args.gatsbyNodeArgs.schema.buildUnionType({\n\t\tname: pascalCase(\n\t\t\t\"Prismic\",\n\t\t\targs.pluginOptions.typePrefix,\n\t\t\targs.model.id,\n\t\t\t\"Slice\",\n\t\t),\n\t\ttypes: variationTypeNames,\n\t\tdescription: args.model.description,\n\t\tresolveType: (source: SharedSlice) => {\n\t\t\treturn pascalCase(\n\t\t\t\t\"Prismic\",\n\t\t\t\targs.pluginOptions.typePrefix,\n\t\t\t\tsource.slice_type,\n\t\t\t\t\"Slice\",\n\t\t\t\tsource.variation,\n\t\t\t);\n\t\t},\n\t});\n\n\treturn type;\n};\n"],"names":["type","pascalCase","fieldModelsRecordToGraphQLType"],"mappings":";;;;AAea,MAAA,gCAAgC,CAC5C,SAC2B;AAC3B,MAAI,qBAA+B,CAAA;AAExB,aAAA,aAAa,KAAK,MAAM,YAAY;AAC9C,UAAMA,QAAO,KAAK,eAAe,OAAO,gBAAgB;AAAA,MACvD,MAAMC,WACL,WAAA,WACA,KAAK,cAAc,YACnB,KAAK,MAAM,IACX,SACA,UAAU,EAAE;AAAA,MAEb,aAAa,KAAK,MAAM;AAAA,MACxB,QAAQ;AAAA,QACP,IAAI;AAAA,UACH,MAAM;AAAA,UACN,SAAS,CAAC,WAA+B;AAEvC,mBAAA,OAAO,MACP,KAAK,eAAe,aACnB,KAAK,eAAe,oBAAoB,MAAM,CAAC;AAAA,UAGlD;AAAA,QACA;AAAA,QACD,YAAY;AAAA,UACX,MAAM;AAAA,QACN;AAAA,QACD,aAAa;AAAA,UACZ,MAAM;AAAA,QACN;AAAA,QACD,SAAS;AAAA,UACR,MAAM;AAAA,QACN;AAAA,QACD,WAAW;AAAA,UACV,MAAM;AAAA,QACN;AAAA,MACD;AAAA,MACD,YAAY,CAAC,gBAAgB,oBAAoB;AAAA,IAAA,CACjD;AAEG,QAAA,UAAU,WAAW,OAAO,KAAK,UAAU,OAAO,EAAE,SAAS,GAAG;A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