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

Transforming data/graphql #21453

Merged
merged 43 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b87db81
Mark localhost URLs as code snippets
bball07 Jan 27, 2020
6e92a67
Mark localhost URLs as code snippets
bball07 Jan 27, 2020
a65aed5
made styling changes
bball07 Jan 28, 2020
e34de8b
Update docs/docs/recipes/transforming-data.md
bball07 Jan 28, 2020
bd2ec5e
Update docs/docs/recipes/transforming-data.md
bball07 Jan 28, 2020
181a357
Update docs/docs/recipes/transforming-data.md
bball07 Jan 28, 2020
ee7af00
remove section on style guide
bball07 Jan 28, 2020
cc47dfd
Merge branch 'master' into transforming-data/graphQL
bball07 Jan 28, 2020
5e26c40
add recipe to overview page
bball07 Jan 28, 2020
b711658
resolve merge conflicts
bball07 Jan 28, 2020
749c85d
fix up recipes overview page
Jan 28, 2020
af94aad
chore: format
Jan 29, 2020
5d547cb
Update docs/docs/recipes/transforming-data.md
bball07 Jan 30, 2020
80001b4
Update docs/docs/recipes/transforming-data.md
bball07 Jan 30, 2020
90680cc
Update docs/docs/recipes/transforming-data.md
bball07 Jan 30, 2020
71b5f3a
Update docs/docs/recipes/transforming-data.md
bball07 Jan 30, 2020
a5fdd34
updated transforming-data.md
bball07 Jan 31, 2020
a315b59
Update docs/docs/recipes/transforming-data.md
bball07 Feb 6, 2020
5f19427
Update docs/docs/recipes/transforming-data.md
bball07 Feb 6, 2020
c55c3c5
Update docs/docs/recipes/transforming-data.md
bball07 Feb 6, 2020
97945eb
Update docs/docs/recipes/transforming-data.md
bball07 Feb 6, 2020
e61064b
made corrections
bball07 Feb 8, 2020
0b3ead0
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
526c5d8
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
a65ffbb
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
41f2646
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
f61f4b8
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
23ebb61
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
483ee91
made changes to recipe
bball07 Feb 11, 2020
4b6fa35
made changes to recipe
bball07 Feb 11, 2020
cc2e4de
made changes to recipe
bball07 Feb 11, 2020
58ddbf2
made changes to recipe
bball07 Feb 11, 2020
b03f53d
made changes to recipe
bball07 Feb 11, 2020
a3d2990
made changes to recipe
bball07 Feb 11, 2020
7144024
Delete transforming-data.md.rej
Feb 11, 2020
a1f4200
Update docs/docs/recipes/transforming-data.md
bball07 Feb 11, 2020
b6ed635
Update docs/docs/recipes/transforming-data.md
bball07 Feb 12, 2020
8b189d5
added in more details
bball07 Feb 12, 2020
3b737bf
added in more details
bball07 Feb 12, 2020
1f67117
changed url
bball07 Feb 12, 2020
69bcb76
changed url
bball07 Feb 12, 2020
75b3264
made final changes to recipe
bball07 Feb 14, 2020
9c55e9f
Merge remote-tracking branch 'upstream/master' into transforming-data…
Feb 14, 2020
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
1 change: 1 addition & 0 deletions docs/docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Access images as static resources, or automate the process of optimizing them th
Transforming data in Gatsby is plugin-driven. Transformer plugins take data fetched using source plugins, and process it into something more usable (e.g. JSON into JavaScript objects, and more).

- [Transforming Markdown into HTML](/docs/recipes/transforming-data#transforming-markdown-into-html)
- [Transforming images into grayscale using GraphQL](/docs/recipes/transforming-data#transforming-images-into-grayscale-using-graphql)

## [9. Deploying your site](/docs/recipes/deploying-your-site)

Expand Down
91 changes: 91 additions & 0 deletions docs/docs/recipes/transforming-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,94 @@ export const query = graphql`

- [Tutorial on transforming Markdown to HTML](/tutorial/part-six/#transformer-plugins) using `gatsby-transformer-remark`
- Browse available transformer plugins in the [Gatsby plugin library](/plugins/?=transformer)

## Transforming images into grayscale using GraphQL

### Prerequisites

- A [Gatsby site](/docs/quick-start) with a `gatsby-config.js` file and an `index.js` page
- The `gatsby-image`, `gatsby-transformer-sharp`, and `gatsby-plugin-sharp` packages installed
- A source plugin installed, such as `gatsby-source-filesystem`
- An image (`.jpg`, `.png`, `.gif`, `.svg`, etc.) in the `src/images` folder

### Directions

1. Edit your `gatsby-config.js` file to source images and configure plugins for Gatsby's GraphQL data layer. A common approach is to source them from an images directory using the `gatsby-source-filesystem` plugin:

```javascript:title=gatsby-config.js

plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
],
```

2. Query your image using GraphQL and apply a grayscale transformation to the image inline. The `relativePath` should be relative to the path you configured in `gatsby-source-filesystem`.

```graphql
query {
file(relativePath: { eq: "corgi.jpg" }) {
childImageSharp {
// highlight-next-line
fluid(grayscale: true) {
...GatsbyImageSharpFluid
}
}
}
}
```

Note: You can find these and other parameters in your GraphQL playground located at `http://localhost:8000/__graphql`

3. Next import the `Img` component from "gatsby-image". You'll use this inside your JSX to display the image.

```jsx:title=src/pages/index.js
import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Layout from "../components/layout"
// highlight-next-line
import Img from "gatsby-image"

export default () => {
const data = useStaticQuery(graphql`
query {
file(relativePath: { eq: "corgi.jpg" }) {
childImageSharp {
// highlight-next-line
fluid(grayscale: true) {
...GatsbyImageSharpFluid
}
}
}
}
`)
return (
<Layout>
<h1>I love my corgi!</h1>
// highlight-start
<Img
fluid={data.file.childImageSharp.fluid}
alt="A corgi smiling happily"
/>
// highlight-end
</Layout>
)
}
```

4. Run `gatsby develop` to start the development server.

5. View your image in the browser: `http://localhost:8000/`

### Additional resources

- [API docs, including grayscale and duotone query tips](/docs/gatsby-image/#shared-query-parameters)
- [Gatsby Image docs](/docs/gatsby-image/)
- [Image processing examples](https://github.com/gatsbyjs/gatsby/tree/master/examples/image-processing)