gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
1 lines • 2.55 kB
Source Map (JSON)
{"version":3,"file":"fieldModelsRecordToGraphQLType.cjs","sources":["../../../src/lib/fieldModelsRecordToGraphQLType.ts"],"sourcesContent":["import type { CustomTypeModelField, SharedSliceModel } from \"@prismicio/client\";\nimport type { GatsbyGraphQLType, NodePluginArgs } from \"gatsby\";\n\nimport type { PluginOptions } from \"../types\";\n\nimport { defaultTransformFieldName } from \"./defaultTransformFieldName\";\nimport { fieldModelToGraphQLConfig } from \"./fieldModelToGraphQLConfig\";\nimport { pascalCase } from \"./pascalCase\";\n\ntype FieldModelsRecordToGraphQLTypeArgs = {\n\tpath: string[];\n\tmodels: Record<string, CustomTypeModelField>;\n\tsharedSliceModels: SharedSliceModel[];\n\tgatsbyNodeArgs: NodePluginArgs;\n\tpluginOptions: PluginOptions;\n};\n\nexport const fieldModelsRecordToGraphQLType = (\n\targs: FieldModelsRecordToGraphQLTypeArgs,\n): GatsbyGraphQLType => {\n\tconst type = args.gatsbyNodeArgs.schema.buildObjectType({\n\t\tname: pascalCase(\n\t\t\t\"Prismic\",\n\t\t\targs.pluginOptions.typePrefix,\n\t\t\targs.path.join(\" \"),\n\t\t),\n\t\tfields: {},\n\t});\n\n\tfor (const fieldName in args.models) {\n\t\tconst model = args.models[fieldName];\n\t\tconst transformedFieldName = args.pluginOptions.transformFieldName\n\t\t\t? args.pluginOptions.transformFieldName(fieldName)\n\t\t\t: defaultTransformFieldName(fieldName);\n\n\t\tif (type.config.fields) {\n\t\t\tconst graphQLConfig = fieldModelToGraphQLConfig({\n\t\t\t\t...args,\n\t\t\t\tmodel,\n\t\t\t\tpath: [...args.path, fieldName],\n\t\t\t});\n\n\t\t\t// Only include fields where\n\t\t\t// `fieldModelToGraphQLConfig()` returned a config. In\n\t\t\t// some cases, the field model does not map to a valid\n\t\t\t// GraphQL type, forcing us to exclude the field.\n\t\t\tif (graphQLConfig) {\n\t\t\t\ttype.config.fields[transformedFieldName] = graphQLConfig;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn type;\n};\n"],"names":["pascalCase","defaultTransformFieldName","fieldModelToGraphQLConfig"],"mappings":";;;;;AAiBa,MAAA,iCAAiC,CAC7C,SACsB;AACtB,QAAM,OAAO,KAAK,eAAe,OAAO,gBAAgB;AAAA,IACvD,MAAMA,WACL,WAAA,WACA,KAAK,cAAc,YACnB,KAAK,KAAK,KAAK,GAAG,CAAC;AAAA,IAEpB,QAAQ,CAAE;AAAA,EAAA,CACV;AAEU,aAAA,aAAa,KAAK,QAAQ;AAC9B,UAAA,QAAQ,KAAK,OAAO,SAAS;AAC7B,UAAA,uBAAuB,KAAK,cAAc,qBAC7C,KAAK,cAAc,mBAAmB,SAAS,IAC/CC,0BAAA,0BAA0B,SAAS;AAElC,QAAA,KAAK,OAAO,QAAQ;AACvB,YAAM,gBAAgBC,0BAAAA,0BAA0B;AAAA,QAC/C,GAAG;AAAA,QACH;AAAA,QACA,MAAM,CAAC,GAAG,KAAK,MAAM,SAAS;AAAA,MAAA,CAC9B;AAMD,UAAI,eAAe;AACb,aAAA,OAAO,OAAO,oBAAoB,IAAI;AAAA,MAC3C;AAAA,IACD;AAAA,EACD;AAEM,SAAA;AACR;;"}