Skip to content

Commit

Permalink
refactor(gatsby-transformer-sharp): Use explicit graphql type definit…
Browse files Browse the repository at this point in the history
…ion vs inferring (#18871)

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

* Change minimal version of gatsby dependency to the one having schema customization API
  • Loading branch information
vladar authored and GatsbyJS Bot committed Oct 21, 2019
1 parent 1fdc4a6 commit 452ac09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-sharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.33",
"gatsby": "^2.12.0",
"gatsby-plugin-sharp": "^2.0.0-beta.3"
},
"repository": {
Expand Down
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,
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`)

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(
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!
}
`)
}
}

0 comments on commit 452ac09

Please sign in to comment.