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

refactor(gatsby-transformer-sharp): Use explicit graphql type definition vs inferring #18871

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const getTracedSVG = async ({ file, image, fieldArgs, cache, reporter }) =>
})

const fixedNodeType = ({
type,
pathPrefix,
getNodeAndSavePathDependency,
reporter,
Expand Down Expand Up @@ -200,7 +199,6 @@ const fixedNodeType = ({
}

const fluidNodeType = ({
type,
pathPrefix,
getNodeAndSavePathDependency,
reporter,
Expand Down Expand Up @@ -357,19 +355,13 @@ const fluidNodeType = ({
}
}

module.exports = ({
type,
const createFields = ({
pathPrefix,
getNodeAndSavePathDependency,
reporter,
cache,
}) => {
if (type.name !== `ImageSharp`) {
return {}
}

const nodeOptions = {
type,
pathPrefix,
getNodeAndSavePathDependency,
reporter,
Expand Down Expand Up @@ -546,3 +538,35 @@ module.exports = ({
},
}
}

module.exports = ({
actions,
schema,
pathPrefix,
getNodeAndSavePathDependency,
reporter,
cache,
}) => {
const { createTypes } = actions

const imageSharpType = schema.buildObjectType({
name: `ImageSharp`,
fields: createFields({
pathPrefix,
getNodeAndSavePathDependency,
reporter,
cache,
}),
interfaces: [`Node`],
extensions: {
infer: true,
freiksenet marked this conversation as resolved.
Show resolved Hide resolved
childOf: {
types: [`File`],
},
},
})

if (createTypes) {
createTypes([imageSharpType])
}
}
24 changes: 3 additions & 21 deletions packages/gatsby-transformer-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
const fs = require(`fs-extra`)

exports.onCreateNode = require(`./on-node-create`)
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`)
exports.createSchemaCustomization = require(`./customize-schema`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to bump dependency on gatsby since older versions won't have this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in 2fb1c20


exports.onPreExtractQueries = async ({ store, getNodesByType }) => {
exports.onPreExtractQueries = async ({ store }) => {
const program = store.getState().program

// Check if there are any ImageSharp nodes. If so add fragments for ImageSharp.
// The fragment will cause an error if there are no ImageSharp nodes.
if (getNodesByType(`ImageSharp`).length == 0) {
return
}

// We have ImageSharp nodes so let's add our fragments to .cache/fragments.
// Add fragments for ImageSharp to .cache/fragments.
await fs.copy(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is no longer necessary as we run babel on plugin code now. We should be able to remove the copy operation.

require.resolve(`gatsby-transformer-sharp/src/fragments.js`),
`${program.directory}/.cache/fragments/image-sharp-fragments.js`
)
}

exports.sourceNodes = ({ actions }) => {
const { createTypes } = actions

if (createTypes) {
createTypes(`
type ImageSharp implements Node @infer @childOf(types: ["File"]) {
id: ID!
}
`)
}
}