Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-image): support multiple sources using gatsby-plugin-image #32544

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/gatsby-plugin-image/src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { GatsbyNode } from "gatsby"
import { getCacheDir } from "./node-apis/node-utils"
import {
ImageFormatType,
ImageLayoutType,
ImagePlaceholderType,
} from "./resolver-utils"

export * from "./node-apis/preprocess-source"

export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
({ actions, schema }) => {
actions.createTypes([
schema.buildEnumType(ImageFormatType),
schema.buildEnumType(ImageLayoutType),
schema.buildEnumType(ImagePlaceholderType),
])
}

export const onCreateBabelConfig: GatsbyNode["onCreateBabelConfig"] = ({
actions,
store,
Expand Down
120 changes: 78 additions & 42 deletions packages/gatsby-plugin-image/src/resolver-utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { GraphQLFieldResolver } from "gatsby/graphql"
import {
GraphQLNonNull,
GraphQLJSON,
GraphQLInt,
GraphQLList,
GraphQLString,
GraphQLFieldConfig,
GraphQLFieldResolver,
GraphQLFieldConfigArgumentMap,
GraphQLEnumType,
GraphQLFloat,
} from "gatsby/graphql"
EnumTypeComposerAsObjectDefinition,
ObjectTypeComposerFieldConfigAsObjectDefinition,
ObjectTypeComposerArgumentConfigMapDefinition,
} from "graphql-compose"
import { stripIndent } from "common-tags"
import { ISharpGatsbyImageArgs, IImageSizeArgs } from "./image-utils"

export const ImageFormatType = new GraphQLEnumType({
export const ImageFormatType: EnumTypeComposerAsObjectDefinition = {
name: `GatsbyImageFormat`,
values: {
NO_CHANGE: { value: `` },
Expand All @@ -22,26 +17,26 @@ export const ImageFormatType = new GraphQLEnumType({
WEBP: { value: `webp` },
AVIF: { value: `avif` },
},
})
}

export const ImageLayoutType = new GraphQLEnumType({
export const ImageLayoutType: EnumTypeComposerAsObjectDefinition = {
name: `GatsbyImageLayout`,
values: {
FIXED: { value: `fixed` },
FULL_WIDTH: { value: `fullWidth` },
CONSTRAINED: { value: `constrained` },
},
})
}

export const ImagePlaceholderType = new GraphQLEnumType({
export const ImagePlaceholderType: EnumTypeComposerAsObjectDefinition = {
name: `GatsbyImagePlaceholder`,
values: {
DOMINANT_COLOR: { value: `dominantColor` },
TRACED_SVG: { value: `tracedSVG` },
BLURRED: { value: `blurred` },
NONE: { value: `none` },
},
})
}

export interface IGatsbyGraphQLFieldConfig<TSource, TContext, TArgs> {
description?: string
Expand All @@ -54,27 +49,46 @@ export interface IGatsbyGraphQLResolverArgumentConfig<TValue = any> {
type: string | Array<string>
defaultValue?: TValue
}
export type IGatsbyImageResolverArgs = Pick<
ISharpGatsbyImageArgs & IImageSizeArgs,
| "aspectRatio"
| "backgroundColor"
| "breakpoints"
| "height"
| "layout"
| "outputPixelDensities"
| "sizes"
| "width"
>

export function getGatsbyImageResolver<TSource, TContext, TArgs>(
resolve: GraphQLFieldResolver<TSource, TContext, TArgs>,
extraArgs?: Record<string, IGatsbyGraphQLResolverArgumentConfig>
): IGatsbyGraphQLFieldConfig<TSource, TContext, TArgs> {
resolve: GraphQLFieldResolver<
TSource,
TContext,
IGatsbyImageResolverArgs & TArgs
>,
extraArgs?: ObjectTypeComposerArgumentConfigMapDefinition<TArgs>
): ObjectTypeComposerFieldConfigAsObjectDefinition<
TSource,
TContext,
IGatsbyImageResolverArgs & TArgs
> {
return {
type: `JSON!`,
args: {
layout: {
type: `enum GatsbyImageLayout { FIXED, FULL_WIDTH, CONSTRAINED }`,
type: ImageLayoutType.name,
description: stripIndent`
The layout for the image.
FIXED: A static image sized, that does not resize according to the screen width
FULL_WIDTH: The image resizes to fit its container. Pass a "sizes" option if it isn't going to be the full width of the screen.
FULL_WIDTH: The image resizes to fit its container. Pass a "sizes" option if it isn't going to be the full width of the screen.
CONSTRAINED: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
`,
},
width: {
type: `Int`,
description: stripIndent`
The display width of the generated image for layout = FIXED, and the display width of the largest image for layout = CONSTRAINED.
The display width of the generated image for layout = FIXED, and the display width of the largest image for layout = CONSTRAINED.
The actual largest image resolution will be this value multiplied by the largest value in outputPixelDensities
Ignored if layout = FULL_WIDTH.
`,
Expand All @@ -87,14 +101,14 @@ export function getGatsbyImageResolver<TSource, TContext, TArgs>(
aspectRatio: {
type: `Float`,
description: stripIndent`
If set along with width or height, this will set the value of the other dimension to match the provided aspect ratio, cropping the image if needed.
If set along with width or height, this will set the value of the other dimension to match the provided aspect ratio, cropping the image if needed.
If neither width or height is provided, height will be set based on the intrinsic width of the source image.
`,
},
sizes: {
type: `String`,
description: stripIndent`
The "sizes" property, passed to the img tag. This describes the display size of the image.
The "sizes" property, passed to the img tag. This describes the display size of the image.
This does not affect the generated images, but is used by the browser to decide which images to download. You can leave this blank for fixed images, or if the responsive image
container will be the full width of the screen. In these cases we will generate an appropriate value.
`,
Expand Down Expand Up @@ -125,15 +139,37 @@ export function getGatsbyImageResolver<TSource, TContext, TArgs>(
}
}

export function getGatsbyImageFieldConfig<TSource, TContext>(
resolve: GraphQLFieldResolver<TSource, TContext>,
extraArgs?: GraphQLFieldConfigArgumentMap
): GraphQLFieldConfig<TSource, TContext> {
export type IGatsbyImageFieldArgs = Pick<
ISharpGatsbyImageArgs & IImageSizeArgs,
| "aspectRatio"
| "backgroundColor"
| "breakpoints"
| "formats"
| "height"
| "layout"
| "outputPixelDensities"
| "placeholder"
| "sizes"
| "width"
>

export function getGatsbyImageFieldConfig<TSource, TContext, TArgs>(
resolve: GraphQLFieldResolver<
TSource,
TContext,
IGatsbyImageFieldArgs & TArgs
>,
extraArgs?: ObjectTypeComposerArgumentConfigMapDefinition<TArgs>
): ObjectTypeComposerFieldConfigAsObjectDefinition<
TSource,
TContext,
IGatsbyImageFieldArgs & TArgs
> {
return {
type: new GraphQLNonNull(GraphQLJSON),
type: `JSON!`,
args: {
layout: {
type: ImageLayoutType,
type: ImageLayoutType.name,
description: stripIndent`
The layout for the image.
FIXED: A static image sized, that does not resize according to the screen width
Expand All @@ -142,27 +178,27 @@ export function getGatsbyImageFieldConfig<TSource, TContext>(
`,
},
width: {
type: GraphQLInt,
type: `Int`,
description: stripIndent`
The display width of the generated image for layout = FIXED, and the display width of the largest image for layout = CONSTRAINED.
The actual largest image resolution will be this value multiplied by the largest value in outputPixelDensities
Ignored if layout = FLUID.
`,
},
height: {
type: GraphQLInt,
type: `Int`,
description: stripIndent`
If set, the height of the generated image. If omitted, it is calculated from the supplied width, matching the aspect ratio of the source image.`,
},
aspectRatio: {
type: GraphQLFloat,
type: `Float`,
description: stripIndent`
If set along with width or height, this will set the value of the other dimension to match the provided aspect ratio, cropping the image if needed.
If set along with width or height, this will set the value of the other dimension to match the provided aspect ratio, cropping the image if needed.
If neither width or height is provided, height will be set based on the intrinsic width of the source image.
`,
},
placeholder: {
type: ImagePlaceholderType,
type: ImagePlaceholderType.name,
description: stripIndent`
Format of generated placeholder image, displayed while the main image loads.
BLURRED: a blurred, low resolution image, encoded as a base64 data URI (default)
Expand All @@ -171,40 +207,40 @@ export function getGatsbyImageFieldConfig<TSource, TContext>(
NONE: no placeholder. Set the argument "backgroundColor" to use a fixed background color.`,
},
formats: {
type: GraphQLList(ImageFormatType),
type: `[${ImageFormatType.name}]`,
description: stripIndent`
The image formats to generate. Valid values are AUTO (meaning the same format as the source image), JPG, PNG, WEBP and AVIF.
The default value is [AUTO, WEBP], and you should rarely need to change this. Take care if you specify JPG or PNG when you do
not know the formats of the source images, as this could lead to unwanted results such as converting JPEGs to PNGs. Specifying
both PNG and JPG is not supported and will be ignored.
both PNG and JPG is not supported and will be ignored.
`,
defaultValue: [``, `webp`],
},
outputPixelDensities: {
type: GraphQLList(GraphQLFloat),
type: `[Float]`,
description: stripIndent`
A list of image pixel densities to generate for FIXED and CONSTRAINED images. You should rarely need to change this. It will never generate images larger than the source, and will always include a 1x image.
Default is [ 1, 2 ] for fixed images, meaning 1x, 2x, 3x, and [0.25, 0.5, 1, 2] for fluid. In this case, an image with a fluid layout and width = 400 would generate images at 100, 200, 400 and 800px wide.
`,
},
breakpoints: {
type: GraphQLList(GraphQLInt),
type: `[Int]`,
description: stripIndent`
Specifies the image widths to generate. You should rarely need to change this. For FIXED and CONSTRAINED images it is better to allow these to be determined automatically,
based on the image size. For FULL_WIDTH images this can be used to override the default, which is [750, 1080, 1366, 1920].
It will never generate any images larger than the source.
`,
},
sizes: {
type: GraphQLString,
type: `String`,
description: stripIndent`
The "sizes" property, passed to the img tag. This describes the display size of the image.
This does not affect the generated images, but is used by the browser to decide which images to download. You can leave this blank for fixed images, or if the responsive image
container will be the full width of the screen. In these cases we will generate an appropriate value.
`,
},
backgroundColor: {
type: GraphQLString,
type: `String`,
description: `Background color applied to the wrapper, or when "letterboxing" an image to another aspect ratio.`,
},
...extraArgs,
Expand Down
17 changes: 3 additions & 14 deletions packages/gatsby-source-shopify/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
SourceNodesArgs,
} from "gatsby"
import { makeResolveGatsbyImageData } from "./resolve-gatsby-image-data"
import {
getGatsbyImageResolver,
IGatsbyGraphQLResolverArgumentConfig,
} from "gatsby-plugin-image/graphql-utils"
import { getGatsbyImageFieldConfig } from "gatsby-plugin-image/graphql-utils"
import { makeSourceFromOperation } from "./make-source-from-operation"
export { createSchemaCustomization } from "./create-schema-customization"
import { createNodeId } from "./node-builder"
Expand Down Expand Up @@ -240,13 +237,6 @@ export function createResolvers(
}: ShopifyPluginOptions
): void {
if (!downloadImages) {
const args = {
placeholder: {
description: `Low resolution version of the image`,
type: `String`,
defaultValue: null,
} as IGatsbyGraphQLResolverArgumentConfig,
}
const imageNodeTypes = [
`ShopifyProductImage`,
`ShopifyProductVariantImage`,
Expand All @@ -262,9 +252,8 @@ export function createResolvers(
return {
...r,
[`${typePrefix}${nodeType}`]: {
gatsbyImageData: getGatsbyImageResolver(
makeResolveGatsbyImageData(cache),
args
gatsbyImageData: getGatsbyImageFieldConfig(
makeResolveGatsbyImageData(cache)
),
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import {
IImage,
ImageFormat,
} from "gatsby-plugin-image"
import { IGatsbyImageFieldArgs } from "gatsby-plugin-image/graphql-utils"
import { readFileSync } from "fs"
import { IShopifyImage, urlBuilder } from "./get-shopify-image"

type ImageLayout = "constrained" | "fixed" | "fullWidth"

type IImageWithPlaceholder = IImage & {
placeholder: string
}
Expand Down Expand Up @@ -48,7 +47,7 @@ export function makeResolveGatsbyImageData(cache: any) {
formats = [`auto`],
layout = `constrained`,
...options
}: { formats: Array<ImageFormat>; layout: ImageLayout }
}: IGatsbyImageFieldArgs
): Promise<IGatsbyImageData> {
const remainingOptions = options as Record<string, any>
let [basename] = image.originalSrc.split(`?`)
Expand Down