Skip to content

Commit

Permalink
try to snapshot schema
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicMahieu committed Mar 12, 2019
1 parent 7fd1099 commit 9696c8e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
38 changes: 28 additions & 10 deletions gatsby-node.js
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")
}
17 changes: 17 additions & 0 deletions src/schema.gql
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
}

0 comments on commit 9696c8e

Please sign in to comment.