Skip to content

Commit

Permalink
Move schema inference to separate module
Browse files Browse the repository at this point in the history
See gatsbyjs#1116 for the reason behind this change.
  • Loading branch information
Zalastax committed Nov 2, 2017
1 parent f659fe4 commit b84fbac
Show file tree
Hide file tree
Showing 29 changed files with 242 additions and 93 deletions.
30 changes: 30 additions & 0 deletions packages/gatsby-infer-schema/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

decls
dist
2 changes: 2 additions & 0 deletions packages/gatsby-infer-schema/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src
flow-typed
2 changes: 2 additions & 0 deletions packages/gatsby-infer-schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gatsby-infer-schema
Infer GraphQL schema types from Gatsby's [node interface](https://www.gatsbyjs.org/docs/node-interface/).
55 changes: 55 additions & 0 deletions packages/gatsby-infer-schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "gatsby-infer-schema",
"description": "GraphQL schema inference for Gatsby",
"version": "1.9.92",
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"bluebird": "^3.5.0",
"common-tags": "^1.4.0",
"flat": "^2.0.1",
"gatsby-cli": "^1.1.17",
"graphql": "^0.11.7",
"graphql-skip-limit": "^1.0.7",
"invariant": "^2.2.2",
"lodash": "^4.17.4",
"minimatch": "^3.0.4",
"sift": "^3.2.6",
"type-of": "^2.0.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"cross-env": "^5.0.5",
"iflow-debug": "^1.0.15",
"iflow-lodash": "^1.1.24",
"iflow-react-router": "^1.2.1",
"nyc": "^7.0.0",
"rimraf": "^2.6.1"
},
"engines": {
"node": ">4.0.0"
},
"resolutions": {
"graphql": "^0.11.7"
},
"keywords": [
"gatsby",
"graphql"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/gatsbyjs/gatsby.git"
},
"scripts": {
"build": "rimraf dist && npm run build:src",
"build:src": "babel src --out-dir dist --source-maps --ignore __tests__",
"clean-test-bundles": "find test/ -type f -name bundle.js* -exec rm -rf {} +",
"prepublish": "cross-env NODE_ENV=production npm run build",
"test-coverage": "node_modules/.bin/nyc --reporter=lcov --reporter=text npm test",
"watch": "rimraf dist && mkdir dist && npm run build:src -- --watch"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {

const { buildFieldEnumValues } = require(`./data-tree-utils`)

module.exports = type => {
module.exports = (type: any) => {
const enumValues = buildFieldEnumValues(type.nodes)
const { connectionType: groupConnection } = connectionDefinitions({
name: _.camelCase(`${type.name} groupConnection`),
Expand Down Expand Up @@ -42,7 +42,7 @@ module.exports = type => {
}),
},
},
resolve(connection, args) {
resolve(connection: any, args: any) {
let fieldName = args.field
if (_.includes(args.field, `___`)) {
fieldName = args.field.replace(`___`, `.`)
Expand All @@ -62,7 +62,7 @@ module.exports = type => {
}),
},
},
resolve(connection, args) {
resolve(connection: any, args: any) {
const fieldName = args.field.replace(`___`, `.`)
const connectionNodes = connection.edges.map(edge => edge.node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const {
} = require(`./infer-graphql-input-fields-from-fields`)
const createSortField = require(`./create-sort-field`)
const buildConnectionFields = require(`./build-connection-fields`)
const { getNodes } = require(`../redux`)

module.exports = (types: any) => {
module.exports = (
types: any,
getNodes: () => any[],
createPageDependency: (arg: any) => void,
) => {
const connections = {}

_.each(types, (type /* , fieldName*/) => {
Expand Down Expand Up @@ -87,7 +90,7 @@ module.exports = (types: any) => {
connection: true,
path,
type: type.node.type,
})
}, createPageDependency)
},
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
GraphQLList,
} = require(`graphql`)

const apiRunner = require(`../utils/api-runner-node`)
const { inferObjectStructureFromNodes } = require(`./infer-graphql-type`)
const {
inferInputObjectStructureFromFields,
Expand All @@ -16,15 +15,24 @@ const {
inferInputObjectStructureFromNodes,
} = require(`./infer-graphql-input-fields`)
const { nodeInterface } = require(`./node-interface`)
const { getNodes, getNode, getNodeAndSavePathDependency } = require(`../redux`)
const { createPageDependency } = require(`../redux/actions/add-page-dependency`)

import type { ProcessedNodeType } from "./infer-graphql-type"

type reduxDeps = {
getNodes: () => any[],
getNode: (id: any) => any,
getNodeAndSavePathDependency: (id: any, path: any) => any,
store: any,
}

type TypeMap = { [typeName: string]: ProcessedNodeType }

module.exports = async () => {
const types = _.groupBy(getNodes(), node => node.internal.type)
module.exports = async (
createPageDependency: (arg: any) => void,
redux: reduxDeps,
joinPath: (a: any, b: any) => any,
apiRunner: any) => {
const types = _.groupBy(redux.getNodes(), node => node.internal.type)
const processedTypes: TypeMap = {}

function createNodeFields(type: ProcessedNodeType) {
Expand All @@ -37,22 +45,22 @@ module.exports = async () => {
type: nodeInterface,
description: `The parent of this node.`,
resolve(node, a, context) {
return getNodeAndSavePathDependency(node.parent, context.path)
return redux.getNodeAndSavePathDependency(node.parent, context.path)
},
},
children: {
type: new GraphQLList(nodeInterface),
description: `The children of this node.`,
resolve(node, a, { path }) {
return node.children.map(id => getNodeAndSavePathDependency(id, path))
return node.children.map(id => redux.getNodeAndSavePathDependency(id, path))
},
},
}

// Create children fields for each type of children e.g.
// "childrenMarkdownRemark".
const childNodesByType = _(type.nodes)
.flatMap(({ children }) => children.map(getNode))
.flatMap(({ children }) => children.map(redux.getNode))
.groupBy(
node => (node.internal ? _.camelCase(node.internal.type) : undefined)
)
Expand All @@ -71,7 +79,7 @@ module.exports = async () => {
description: `The children of this node of type ${childNodeType}`,
resolve(node, a, { path }) {
const filteredNodes = node.children
.map(id => getNode(id))
.map(redux.getNode)
.filter(
({ internal }) => _.camelCase(internal.type) === childNodeType
)
Expand All @@ -89,7 +97,7 @@ module.exports = async () => {
description: `The child of this node of type ${childNodeType}`,
resolve(node, a, { path }) {
const childNode = node.children
.map(id => getNode(id))
.map(redux.getNode)
.find(
({ internal }) => _.camelCase(internal.type) === childNodeType
)
Expand All @@ -108,8 +116,8 @@ module.exports = async () => {
const inferredFields = inferObjectStructureFromNodes({
nodes: type.nodes,
types: _.values(processedTypes),
allNodes: getNodes(),
})
allNodes: redux.getNodes(),
}, createPageDependency, joinPath, redux)

return {
...defaultNodeFields,
Expand All @@ -126,7 +134,7 @@ module.exports = async () => {

const fieldsFromPlugins = await apiRunner(`setFieldsOnGraphQLNodeType`, {
type: intermediateType,
allNodes: getNodes(),
allNodes: redux.getNodes(),
traceId: `initial-setFieldsOnGraphQLNodeType`,
})

Expand Down Expand Up @@ -166,7 +174,7 @@ module.exports = async () => {
resolve(a, args, context) {
const runSift = require(`./run-sift`)
const latestNodes = _.filter(
getNodes(),
redux.getNodes(),
n => n.internal.type === typeName
)
if (!_.isObject(args)) {
Expand All @@ -177,7 +185,7 @@ module.exports = async () => {
nodes: latestNodes,
path: context.path ? context.path : `LAYOUT___${context.id}`,
type: gqlType,
})
}, createPageDependency)
},
},
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function createSortField(
},
}),
},
__proto__: null,
},
}),
}
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby-infer-schema/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* @flow */

export const buildNodeTypes = require(`./build-node-types`)
export const buildNodeConnections = require(`./build-node-connections`)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function makeNullable(type: GraphQLInputType): GraphQLNullableInputType<any> {

function convertToInputType(
type: GraphQLType,
typeMap: Set
typeMap: Set<*>
): ?GraphQLInputType {
// track types already processed in current tree, to avoid infinite recursion
if (typeMap.has(type)) {
Expand Down Expand Up @@ -188,7 +188,7 @@ export function inferInputObjectStructureFromFields({

// Add sorting (but only to the top level).
if (typeName) {
extractFieldNamesFromInputField(key, inputType, sort)
extractFieldNamesFromInputField(key, (inputType: any), sort)
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,32 @@ function typeFields(type): GraphQLInputFieldConfigMap {
return {
eq: { type: GraphQLBoolean },
ne: { type: GraphQLBoolean },
__proto__: null,
}
case `string`:
return {
eq: { type: GraphQLString },
ne: { type: GraphQLString },
regex: { type: GraphQLString },
glob: { type: GraphQLString },
__proto__: null,
}
case `int`:
return {
eq: { type: GraphQLInt },
ne: { type: GraphQLInt },
__proto__: null,
}
case `float`:
return {
eq: { type: GraphQLFloat },
ne: { type: GraphQLFloat },
__proto__: null,
}
}
return {}
return {
__proto__: null,
}
}

function inferGraphQLInputFields({
Expand Down
Loading

0 comments on commit b84fbac

Please sign in to comment.