-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd1099
commit 9696c8e
Showing
2 changed files
with
45 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
/** | ||
* Implement Gatsby's Node APIs in this file. | ||
* | ||
* See: https://www.gatsbyjs.org/docs/node-apis/ | ||
*/ | ||
const { printType } = require("graphql") | ||
const fs = require("fs-extra") | ||
const path = require("path") | ||
|
||
exports.createResolvers = ({ createResolvers }) => { | ||
createResolvers({ | ||
CommentsJson: { | ||
post: { | ||
type: `PostsJson`, | ||
resolve (source, args, context, info) { | ||
resolve(source, args, context, info) { | ||
const allNodes = context.nodeModel.getAllNodes() | ||
return allNodes | ||
.filter(node => node.internal.type === 'PostsJson') | ||
.filter(node => node.internal.type === "PostsJson") | ||
.find(page => { | ||
const parent = allNodes.find(n => n.id === page.parent) | ||
return parent && parent.name === source.postId | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const schemaFilePath = path.join(__dirname, "./src/schema.gql") | ||
|
||
exports.sourceNodes = async ({ actions }) => { | ||
const { createTypes } = actions | ||
|
||
if (await fs.exists(schemaFilePath)) { | ||
const typeDefs = (await fs.readFile(schemaFilePath)).toString() | ||
createTypes(typeDefs) | ||
} | ||
} | ||
|
||
exports.onPostBootstrap = async ({ store }) => { | ||
const { schema } = store.getState() | ||
const types = ["CommentsJson", "PostsJson"] | ||
const typeDefs = types | ||
.map(type => printType(schema.getType(type))) | ||
.join("\n\n") | ||
await fs.writeFile(schemaFilePath, typeDefs + "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
type CommentsJson implements Node { | ||
id: ID! | ||
parent: Node | ||
children: [Node!]! | ||
internal: Internal! | ||
postId: String | ||
message: String | ||
post: PostsJson | ||
} | ||
|
||
type PostsJson implements Node { | ||
id: ID! | ||
parent: Node | ||
children: [Node!]! | ||
internal: Internal! | ||
content: String | ||
} |