Skip to content

Commit 5f79cd5

Browse files
committed
chore: update
1 parent 8fa3939 commit 5f79cd5

File tree

6 files changed

+65
-37
lines changed

6 files changed

+65
-37
lines changed

gatsby-node.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,51 @@ exports.onCreateNode = (gatsbyCreateNodeArgs) => {
2727
}
2828
};
2929

30+
/**
31+
* We create the preview cards within `onCreateNode`
32+
* but we have to tell gatsby about the foreign key relationship
33+
* of the linked File.
34+
*/
35+
exports.createSchemaCustomization = ({ actions, schema }) => {
36+
const { createTypes } = actions;
37+
38+
const typeDefs = [
39+
/**
40+
* Variant 1: use `@link` and link the given field
41+
* You can't replace fields.socialCard hence the creation of the node in the parent
42+
*/
43+
`type MarkdownRemark implements Node {
44+
socialCard: File @link(from: "fields.socialCard")
45+
}`,
46+
/**
47+
* Variant 2: use `buildObjectType` and resolve the
48+
* foreign key relation through a resolver. The `@link` above
49+
* is basically syntax sugar for this. I want to show both variants
50+
* here for education purposes as it was a long bumpy road.
51+
*
52+
* Before ew used the deprecated fielname___NODE way of adding
53+
* the FK relationship which fails for incremental builds in gatsby 4
54+
*/
55+
schema.buildObjectType({
56+
name: 'SyPersonioJob',
57+
fields: {
58+
socialCard: {
59+
type: 'File',
60+
resolve: (source, args, context, info) => {
61+
return context.nodeModel.getNodeById({
62+
id: source.fields.socialCard,
63+
type: 'File',
64+
});
65+
},
66+
},
67+
},
68+
interfaces: ['Node'],
69+
}),
70+
];
71+
72+
createTypes(typeDefs);
73+
};
74+
3075
exports.createPages = async (createPagesArgs) => {
3176
await createCareerPages(createPagesArgs);
3277
await createBlogPosts(createPagesArgs);

gatsby/create-node/create-preview-cards.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const createPreviewCard = async (
2626
if (fileNode) {
2727
createNodeField({
2828
node,
29-
name: `socialCard___NODE`,
29+
name: `socialCard`,
3030
value: fileNode.id,
3131
});
3232
}

src/pages/career.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import { IGatsbyImageData } from 'gatsby-plugin-image';
88

99
interface CareerMarkdownQuery {
1010
htmlAst: string;
11-
fields: {
12-
socialCard: {
13-
childImageSharp: {
14-
fixed: {
15-
src: string;
16-
};
11+
socialCard: {
12+
childImageSharp: {
13+
fixed: {
14+
src: string;
1715
};
1816
};
1917
};
@@ -34,7 +32,7 @@ interface CareerProps {
3432
const Career = (props: CareerProps) => {
3533
const { t } = useTranslation();
3634
const markdown = props.data.markdownRemark;
37-
const socialCardPath = markdown.fields.socialCard.childImageSharp.fixed.src;
35+
const socialCardPath = markdown.socialCard.childImageSharp.fixed.src;
3836

3937
return (
4038
<>
@@ -88,12 +86,10 @@ export const CareerPageQuery = graphql`
8886
fileAbsolutePath: { regex: "/(pages/career)/" }
8987
frontmatter: { language: { eq: $language } }
9088
) {
91-
fields {
92-
socialCard {
93-
childImageSharp {
94-
fixed(width: 1440, height: 760) {
95-
src
96-
}
89+
socialCard {
90+
childImageSharp {
91+
fixed(width: 1440, height: 760) {
92+
src
9793
}
9894
}
9995
}

src/templates/blog-post.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const BlogArticleTemplate: React.FC<BlogArticleTemplateProps> = ({
2020
const { t } = useTranslation();
2121
const markdown = data.markdownRemark;
2222

23-
const socialCardPath = markdown.fields.socialCard.childImageSharp.fixed.src;
2423
const shareImagePath =
2524
markdown.frontmatter.shareImage.childImageSharp.fixed.src;
2625

@@ -69,14 +68,6 @@ export const BlogPostPageQuery = graphql`
6968
html
7069
htmlAst
7170
fields {
72-
socialCard {
73-
childImageSharp {
74-
fixed(width: 1440, height: 760) {
75-
src
76-
}
77-
}
78-
}
79-
8071
readingTime {
8172
minutes
8273
}

src/templates/career-details.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface CareerPageProps {
2323
const CareerPage: React.FC<CareerPageProps> = (props): JSX.Element => {
2424
const { pageContext } = props;
2525
const position = props.data.syPersonioJob;
26-
const socialCardPath = position.fields.socialCard.childImageSharp.fixed.src;
26+
const socialCardPath = position.socialCard.childImageSharp.fixed.src;
2727
const { t } = useTranslation();
2828

2929
return (
@@ -52,12 +52,10 @@ const CareerPage: React.FC<CareerPageProps> = (props): JSX.Element => {
5252
export const CareerDetailsPageQuery = graphql`
5353
query ($language: String!, $id: String!) {
5454
syPersonioJob(id: { eq: $id }) {
55-
fields {
56-
socialCard {
57-
childImageSharp {
58-
fixed(width: 1440, height: 760) {
59-
src
60-
}
55+
socialCard {
56+
childImageSharp {
57+
fixed(width: 1440, height: 760) {
58+
src
6159
}
6260
}
6361
}

src/types.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ export interface SyPersonioJob {
4040
slug: string;
4141
sections: SyPersonioJobSection[];
4242

43-
// now owned by the source plugin, added via `onCreateNode`
44-
fields: {
45-
socialCard: {
46-
childImageSharp: {
47-
fixed: {
48-
src: string;
49-
};
43+
// added through onCreateNode
44+
socialCard: {
45+
childImageSharp: {
46+
fixed: {
47+
src: string;
5048
};
5149
};
5250
};

0 commit comments

Comments
 (0)