UNPKG

gatsby-source-prismic

Version:

Gatsby source plugin for building websites using Prismic as a data source

1 lines 7.02 kB
{"version":3,"file":"customTypeModelToGraphQLType.cjs","sources":["../../../src/lib/customTypeModelToGraphQLType.ts"],"sourcesContent":["import {\n\tCustomTypeModel,\n\tCustomTypeModelFieldType,\n\tCustomTypeModelTab,\n\tSharedSliceModel,\n\tasLink,\n} from \"@prismicio/client\";\nimport type { GatsbyGraphQLObjectType, NodePluginArgs } from \"gatsby\";\n\nimport type { PluginOptions, PrismicDocumentNodeInput } from \"../types\";\n\nimport { PREVIEWABLE_FIELD_NAME } from \"../constants\";\n\nimport { fieldModelsRecordToGraphQLType } from \"./fieldModelsRecordToGraphQLType\";\nimport { pascalCase } from \"./pascalCase\";\n\ntype CustomTypeModelToGraphQLTypeArgs = {\n\tmodel: CustomTypeModel;\n\tsharedSliceModels: SharedSliceModel[];\n\tgatsbyNodeArgs: NodePluginArgs;\n\tpluginOptions: PluginOptions;\n};\n\nexport const customTypeModelToGraphQLType = (\n\targs: CustomTypeModelToGraphQLTypeArgs,\n): GatsbyGraphQLObjectType => {\n\tconst type = args.gatsbyNodeArgs.schema.buildObjectType({\n\t\tname: pascalCase(\"Prismic\", args.pluginOptions.typePrefix, args.model.id),\n\t\tdescription: `A ${args.model.label} document from Prismic.`,\n\t\tfields: {\n\t\t\tprismicId: {\n\t\t\t\ttype: \"ID!\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"The identifier for the Prismic document. It is guaranteed to be unique within all documents of all types from the same Prismic repository.\",\n\t\t\t},\n\t\t\talternate_languages: {\n\t\t\t\ttype: `[${pascalCase(\n\t\t\t\t\t\"Prismic\",\n\t\t\t\t\targs.pluginOptions.typePrefix,\n\t\t\t\t\t\"AlternateLanguage\",\n\t\t\t\t)}!]!`,\n\t\t\t\tdescription:\n\t\t\t\t\t\"Alternate versions of the document in different languages.\",\n\t\t\t},\n\t\t\tfirst_publication_date: {\n\t\t\t\ttype: \"Date!\",\n\t\t\t\tdescription: \"The timestamp at which the document was first published.\",\n\t\t\t\textensions: { dateformat: {} },\n\t\t\t},\n\t\t\tlast_publication_date: {\n\t\t\t\ttype: \"Date!\",\n\t\t\t\tdescription: \"The timestamp at which the document was last published.\",\n\t\t\t\textensions: { dateformat: {} },\n\t\t\t},\n\t\t\thref: {\n\t\t\t\ttype: \"String!\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"The URL to fetch this document's JSON value from the Prismic REST API.\",\n\t\t\t},\n\t\t\tlang: {\n\t\t\t\ttype: \"String!\",\n\t\t\t\tdescription: \"The language of the Prismic document.\",\n\t\t\t},\n\t\t\ttags: {\n\t\t\t\ttype: \"[String!]!\",\n\t\t\t\tdescription: \"Tags associated with the Prismic document.\",\n\t\t\t},\n\t\t\ttype: {\n\t\t\t\ttype: \"String!\",\n\t\t\t\tdescription: \"The type of the Prismic document.\",\n\t\t\t},\n\t\t\turl: {\n\t\t\t\ttype: \"String\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"The URL of the Prismic document determined using the configured Route Resolvers or Link Resolver. If Route Resolvers or a Link Resolver is not given, this field is `null`.\",\n\t\t\t\tresolve: (source: PrismicDocumentNodeInput) =>\n\t\t\t\t\tasLink(source, args.pluginOptions.linkResolver),\n\t\t\t},\n\t\t\traw: {\n\t\t\t\ttype: \"JSON!\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"**Do not use this field unless you know what you are doing**. The unprocessed Prismic document value returned from the Prismic REST API.\",\n\t\t\t},\n\t\t\t[PREVIEWABLE_FIELD_NAME]: {\n\t\t\t\ttype: \"ID!\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"Query this field to enable preview support on this node. Requires `gatsby-plugin-prismic-preview` integrated in your app to enable previews.\",\n\t\t\t\tresolve: (source: PrismicDocumentNodeInput) => source.prismicId,\n\t\t\t},\n\t\t},\n\t\tinterfaces: [\"Node\"],\n\t\textensions: { infer: false },\n\t});\n\n\tconst { uid: uidFieldModel, ...dataFieldModels }: CustomTypeModelTab =\n\t\tObject.assign({}, ...Object.values(args.model.json));\n\n\tif (uidFieldModel && uidFieldModel.type === CustomTypeModelFieldType.UID) {\n\t\tif (type.config.fields) {\n\t\t\ttype.config.fields.uid = {\n\t\t\t\ttype: \"String!\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"The unique identifier for the Prismic document. It is guaranteed to be unique within all documents of the same type from the same Prismic repository.\",\n\t\t\t};\n\t\t}\n\t}\n\n\tif (Object.keys(dataFieldModels).length > 0) {\n\t\tconst dataType = fieldModelsRecordToGraphQLType({\n\t\t\tpath: [args.model.id, \"data\"],\n\t\t\tmodels: dataFieldModels,\n\t\t\tsharedSliceModels: args.sharedSliceModels,\n\t\t\tgatsbyNodeArgs: args.gatsbyNodeArgs,\n\t\t\tpluginOptions: args.pluginOptions,\n\t\t});\n\n\t\targs.gatsbyNodeArgs.actions.createTypes(dataType);\n\n\t\tif (type.config.fields) {\n\t\t\ttype.config.fields.data = {\n\t\t\t\ttype: `${dataType.config.name}!`,\n\t\t\t\tdescription: \"Content for the document.\",\n\t\t\t};\n\t\t\ttype.config.fields.dataRaw = {\n\t\t\t\ttype: \"JSON!\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"**Do not use this field unless you know what you are doing**. The unprocessed `data` property of the Prismic document. Querying individual fields via GraphQL is much preferred.\",\n\t\t\t\tresolve: (source: PrismicDocumentNodeInput) => source.raw.data,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn type;\n};\n"],"names":["pascalCase","asLink","PREVIEWABLE_FIELD_NAME","CustomTypeModelFieldType","fieldModelsRecordToGraphQLType"],"mappings":";;;;;;AAuBa,MAAA,+BAA+B,CAC3C,SAC4B;AAC5B,QAAM,OAAO,KAAK,eAAe,OAAO,gBAAgB;AAAA,IACvD,MAAMA,WAAAA,WAAW,WAAW,KAAK,cAAc,YAAY,KAAK,MAAM,EAAE;AAAA,IACxE,aAAa,KAAK,KAAK,MAAM;AAAA,IAC7B,QAAQ;AAAA,MACP,WAAW;AAAA,QACV,MAAM;AAA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