Skip to content

Commit 3e97be9

Browse files
committed
feat: sourcing note.com api
1 parent b5be62b commit 3e97be9

File tree

1 file changed

+204
-14
lines changed

1 file changed

+204
-14
lines changed

packages/gatsby-source-note-com/src/gatsby-node.ts

Lines changed: 204 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { NodeInput, GatsbyNode } from "gatsby";
2-
import { makeNoteApiClient } from "note-com-js";
2+
import {
3+
type EmbeddedContent,
4+
type HashtagNote,
5+
type Picture,
6+
makeNoteApiClient,
7+
} from "note-com-js";
38

49
import type { NoteUserNodeSource, NoteTextNoteNodeSource, PluginOptions } from "./types";
510

@@ -39,6 +44,14 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
3944
type: "String!",
4045
resolve: (source: NoteTextNoteNodeSource) => source.key,
4146
},
47+
userId: {
48+
type: "Int!",
49+
resolve: (source: NoteTextNoteNodeSource) => source.user_id,
50+
},
51+
readingUuid: {
52+
type: "String!",
53+
resolve: (source: NoteTextNoteNodeSource) => source.reading_uuid,
54+
},
4255
title: {
4356
type: "String!",
4457
resolve: (source: NoteTextNoteNodeSource) => source.name,
@@ -47,6 +60,10 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
4760
type: "String!",
4861
resolve: (source: NoteTextNoteNodeSource) => source.body,
4962
},
63+
eyecatch: {
64+
type: "String!",
65+
resolve: (source: NoteTextNoteNodeSource) => source.eyecatch,
66+
},
5067
author: {
5168
type: "NoteUser!",
5269
resolve(source: NoteTextNoteNodeSource, _args, context) {
@@ -62,13 +79,41 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
6279
});
6380
},
6481
},
82+
hashtags: {
83+
type: "[NoteHashtag!]!",
84+
resolve: (source: NoteTextNoteNodeSource) => source.hashtag_notes,
85+
},
86+
pictures: {
87+
type: "[NotePicture!]!",
88+
resolve: (source: NoteTextNoteNodeSource) => source.pictures,
89+
},
90+
embeddedContents: {
91+
type: "[NoteEmbeddedContent!]!",
92+
resolve: (source: NoteTextNoteNodeSource) => source.embedded_contents,
93+
},
94+
likeCount: {
95+
type: "Int!",
96+
resolve: (source: NoteTextNoteNodeSource) => source.like_count,
97+
},
98+
anonymousLikeCount: {
99+
type: "Int!",
100+
resolve: (source: NoteTextNoteNodeSource) => source.anonymous_like_count,
101+
},
102+
commentCount: {
103+
type: "Int!",
104+
resolve: (source: NoteTextNoteNodeSource) => source.comment_count,
105+
},
106+
tweetText: {
107+
type: "String!",
108+
resolve: (source: NoteTextNoteNodeSource) => source.tweet_text,
109+
},
65110
publishedAt: {
66111
type: "Date!",
67112
resolve: (source: NoteTextNoteNodeSource) => new Date(source.publish_at),
68113
},
69-
eyecatchImageUrl: {
114+
noteUrl: {
70115
type: "String!",
71-
resolve: (source: NoteTextNoteNodeSource) => source.eyecatch,
116+
resolve: (source: NoteTextNoteNodeSource) => source.note_url,
72117
},
73118
noteShareUrl: {
74119
type: "String!",
@@ -86,6 +131,152 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
86131
type: "String!",
87132
resolve: (source: NoteTextNoteNodeSource) => source.line_share_url,
88133
},
134+
popularSiblingNotes: {
135+
type: "[NoteTextNote!]!",
136+
async resolve(source: NoteTextNoteNodeSource, _args, context) {
137+
const { entries } = await context.nodeModel.findAll({
138+
type: "NoteTextNote",
139+
query: {
140+
filter: {
141+
noteId: {
142+
in: source.popular_sibling_notes.map((note) => note.id.toString()),
143+
},
144+
},
145+
},
146+
});
147+
return entries;
148+
},
149+
},
150+
prevNote: {
151+
type: "NoteTextNote",
152+
resolve(source: NoteTextNoteNodeSource, _args, context) {
153+
return context.nodeModel.findOne({
154+
type: "NoteTextNote",
155+
query: {
156+
filter: {
157+
noteId: {
158+
eq: source.prev_note?.id.toString() ?? null,
159+
},
160+
},
161+
},
162+
});
163+
},
164+
},
165+
nextNote: {
166+
type: "NoteTextNote",
167+
resolve(source: NoteTextNoteNodeSource, _args, context) {
168+
return context.nodeModel.findOne({
169+
type: "NoteTextNote",
170+
query: {
171+
filter: {
172+
noteId: {
173+
eq: source.next_note?.id.toString() ?? null,
174+
},
175+
},
176+
},
177+
});
178+
},
179+
},
180+
},
181+
}),
182+
schema.buildObjectType({
183+
name: "NotePicture",
184+
extensions: {
185+
infer: false,
186+
},
187+
fields: {
188+
key: {
189+
type: "String!",
190+
resolve: (source: Picture) => source.key,
191+
},
192+
url: {
193+
type: "String!",
194+
resolve: (source: Picture) => source.url,
195+
},
196+
thumbnailUrl: {
197+
type: "String!",
198+
resolve: (source: Picture) => source.thumbnail_url,
199+
},
200+
alt: {
201+
type: "String!",
202+
resolve: (source: Picture) => source.alt,
203+
},
204+
caption: {
205+
type: "String",
206+
resolve: (source: Picture) => source.caption,
207+
},
208+
width: {
209+
type: "Int",
210+
resolve: (source: Picture) => source.width,
211+
},
212+
height: {
213+
type: "Int",
214+
resolve: (source: Picture) => source.height,
215+
},
216+
},
217+
}),
218+
schema.buildObjectType({
219+
name: "NoteEmbeddedContent",
220+
extensions: {
221+
infer: false,
222+
},
223+
fields: {
224+
key: {
225+
type: "String!",
226+
resolve: (source: EmbeddedContent) => source.key,
227+
},
228+
url: {
229+
type: "String!",
230+
resolve: (source: EmbeddedContent) => source.url,
231+
},
232+
embedHtml: {
233+
type: "String",
234+
resolve: (source: EmbeddedContent) => source.html_for_embed,
235+
},
236+
displayHtml: {
237+
type: "String",
238+
resolve: (source: EmbeddedContent) => source.html_for_display,
239+
},
240+
embeddableType: {
241+
type: "String!",
242+
resolve: (source: EmbeddedContent) => source.embeddable_type,
243+
},
244+
service: {
245+
type: "String!",
246+
resolve: (source: EmbeddedContent) => source.service,
247+
},
248+
identifier: {
249+
type: "String",
250+
resolve: (source: EmbeddedContent) => source.identifier,
251+
},
252+
caption: {
253+
type: "String",
254+
resolve: (source: EmbeddedContent) => source.caption,
255+
},
256+
createdAt: {
257+
type: "Date!",
258+
resolve: (source: EmbeddedContent) => source.created_at,
259+
},
260+
},
261+
}),
262+
schema.buildObjectType({
263+
name: "NoteHashtag",
264+
extensions: {
265+
infer: false,
266+
},
267+
fields: {
268+
id: {
269+
type: "String!",
270+
resolve: (source: HashtagNote) => source.id.toString(),
271+
},
272+
createdAt: {
273+
type: "Date!",
274+
resolve: (source: HashtagNote) => source.created_at,
275+
},
276+
name: {
277+
type: "String!",
278+
resolve: (source: HashtagNote) => source.hashtag.name,
279+
},
89280
},
90281
}),
91282
]);
@@ -101,10 +292,9 @@ export const sourceNodes: GatsbyNode["sourceNodes"] = async (
101292
const client = makeNoteApiClient();
102293

103294
const user = await client.getUser(creator);
104-
const contents = await Promise.all(
105-
(
106-
await client.getUserContents(creator)
107-
).contents.map((content) => {
295+
const contents = await client.getUserContents(creator);
296+
const noteTexts = await Promise.all(
297+
contents.map((content) => {
108298
return client.getNoteText(content.key);
109299
}),
110300
);
@@ -123,19 +313,19 @@ export const sourceNodes: GatsbyNode["sourceNodes"] = async (
123313

124314
createNode(userNode);
125315

126-
const contentsNodes: Array<NodeInput & NoteTextNoteNodeSource> = contents.map((content) => ({
127-
...content,
128-
noteId: content.id,
129-
id: createNodeId(`NoteTextNote-${content.id}`),
316+
const noteTextsNode: Array<NodeInput & NoteTextNoteNodeSource> = noteTexts.map((noteText) => ({
317+
...noteText,
318+
noteId: noteText.id,
319+
id: createNodeId(`NoteTextNote-${noteText.id}`),
130320
parent: null,
131321
children: [],
132322
internal: {
133323
type: "NoteTextNote",
134-
contentDigest: createContentDigest(content),
324+
contentDigest: createContentDigest(noteText),
135325
},
136326
}));
137327

138-
for (const contentsNode of contentsNodes) {
139-
createNode(contentsNode);
328+
for (const noteTextNode of noteTextsNode) {
329+
createNode(noteTextNode);
140330
}
141331
};

0 commit comments

Comments
 (0)