Skip to content

Commit

Permalink
better example inferance (#938)
Browse files Browse the repository at this point in the history
* better example inferance

* WIP

* fix null checks, more tests

* MOAR tests

* Some more types

* rm weird yarn file

* rm comment

* Add build-node-tests
  • Loading branch information
jquense authored and KyleAMathews committed May 9, 2017
1 parent 3dbc3eb commit a0370a9
Show file tree
Hide file tree
Showing 19 changed files with 1,419 additions and 970 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"env": {
"es6": true,
"node": true
"node": true,
"jest": true
},
"rules": {
"no-console": "off",
Expand Down
11 changes: 1 addition & 10 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
"description": "React.js Static Site Generator",
"version": "1.0.0-alpha14",
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"ava": {
"require": [
"babel-register"
],
"babel": "inherit"
},
"bin": {
"gatsby": "./dist/gatsby-cli.js"
},
Expand Down Expand Up @@ -148,10 +142,7 @@
"build:src": "babel src --out-dir dist --source-maps --ignore gatsby-cli.js",
"build:cli": "babel src/gatsby-cli.js --out-file dist/gatsby-cli.js --presets es2015",
"clean-test-bundles": "find test/ -type f -name bundle.js* -exec rm -rf {} +",
"test": "npm run lint && npm run test-node && npm run test-integration",
"test-coverage": "node_modules/.bin/nyc --reporter=lcov --reporter=text npm test",
"test-integration": "node_modules/.bin/ava test/integration",
"test-node": "node_modules/.bin/ava test/utils",
"watch": "rimraf dist && babel -w src --out-dir dist"
"watch": "rimraf dist && babel -w lib --out-dir dist --source-maps "
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ Object {
exports[`Gatsby data tree utils builds field examples from an array of nodes 1`] = `
Object {
"anArray": Array [
4,
6,
2,
1,
],
"date": "2006-07-22T22:39:53.000Z",
"emptyArray": null,
"frontmatter": Object {
"blue": 10010,
"circle": "happy",
Expand All @@ -48,6 +47,7 @@ Object {
"title": "The world of slash and adventure",
},
"hair": 4,
"iAmNull": null,
"name": "The Mad Wax",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GraphQL type inferance Infers graphql type from array of nodes 1`] = `
Object {
"data": Object {
"listNode": Array [
Object {
"aBoolean": true,
"anArray": Array [
1,
2,
3,
4,
],
"anObjectArray": Array [
Object {
"aBoolean": true,
"aNumber": 2,
"anArray": null,
"anotherObjectArray": null,
},
Object {
"aBoolean": null,
"aNumber": 2,
"anArray": Array [
1,
2,
],
"anotherObjectArray": null,
},
Object {
"aBoolean": null,
"aNumber": null,
"anArray": null,
"anotherObjectArray": Array [
Object {
"bar": 10,
"baz": null,
},
],
},
],
"date": "1012",
"deepObject": Object {
"deepObject": Object {
"deepObject": Object {
"level": 3,
},
"level": 2,
},
"level": 1,
},
"domain": "pizza.com",
"externalUrl": "https://example.com/awesome.jpg",
"frontmatter": Object {
"date": "1012",
"title": "The world of dash and adventure",
},
"hair": 1,
},
Object {
"aBoolean": null,
"anArray": Array [
1,
2,
5,
4,
],
"anObjectArray": Array [
Object {
"aBoolean": null,
"aNumber": null,
"anArray": null,
"anotherObjectArray": Array [
Object {
"bar": null,
"baz": "quz",
},
],
},
],
"date": "1984",
"deepObject": null,
"domain": null,
"externalUrl": null,
"frontmatter": Object {
"date": "1984",
"title": "The world of slash and adventure",
},
"hair": 2,
},
],
},
}
`;
98 changes: 98 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-node-types-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const { graphql, GraphQLObjectType, GraphQLSchema } = require(`graphql`)
const _ = require(`lodash`)
const buildNodeTypes = require(`../build-node-types`)

describe(`build-node-types`, () => {
let schema, store, types

async function runQuery(query) {
let context = { path: `foo` }
let { data, errors } = await graphql(schema, query, context, context)
expect(errors).not.toBeDefined()
return data
}

beforeEach(async () => {
;({ store } = require(`../../redux`))
;[
{ id: `p1`, type: `Parent`, hair: `red`, children: [`c1`, `c2`, `r1`] },
{ id: `r1`, type: `Relative`, hair: `black`, children: [], parent: `p1` },
{ id: `c1`, type: `Child`, hair: `brown`, children: [], parent: `p1` },
{ id: `c2`, type: `Child`, hair: `blonde`, children: [], parent: `p1` },
].forEach(n => store.dispatch({ type: `CREATE_NODE`, payload: n }))

types = await buildNodeTypes()
schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: `RootQueryType`,
fields: _.mapValues(types, `node`),
}),
})
})

it(`should build types`, () => {
expect(Object.keys(types)).toHaveLength(3)
})

it(`should result in a valid queryable schema`, async () => {
let { parent, child, relative } = await runQuery(`
{
parent(id: { eq: "p1" }) {
hair
}
child(id: { eq: "c1" }) {
hair
}
relative(id: { eq: "r1" }) {
hair
}
}
`)
expect(parent.hair).toEqual(`red`)
expect(child.hair).toEqual(`brown`)
expect(relative.hair).toEqual(`black`)
})

it(`should link children automatically`, async () => {
let { parent } = await runQuery(`
{
parent(id: { eq: "p1" }) {
children {
id
}
}
}
`)
expect(parent.children).toBeDefined()
expect(parent.children.map(c => c.id)).toEqual([`c1`, `c2`, `r1`])
})

it(`should create typed children fields`, async () => {
let { parent } = await runQuery(`
{
parent(id: { eq: "p1" }) {
childrenChild { # lol
id
}
}
}
`)
expect(parent.childrenChild).toBeDefined()
expect(parent.childrenChild.map(c => c.id)).toEqual([`c1`, `c2`])
})

it(`should create typed child field for singular children`, async () => {
let { parent } = await runQuery(`
{
parent(id: { eq: "p1" }) {
childRelative { # lol
id
}
}
}
`)

expect(parent.childRelative).toBeDefined()
expect(parent.childRelative.id).toEqual(`r1`)
})
})
13 changes: 7 additions & 6 deletions packages/gatsby/src/schema/__tests__/data-tree-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe(`Gatsby data tree utils`, () => {
name: `The Mad Wax`,
hair: 2,
date: `2006-07-22T22:39:53.000Z`,
emptyArray: [undefined, null],
anArray: [1, 2, 5, 4],
iAmNull: null,
frontmatter: {
Expand Down Expand Up @@ -62,16 +63,16 @@ describe(`Gatsby data tree utils`, () => {
]

it(`builds field examples from an array of nodes`, () => {
expect(extractFieldExamples({ nodes })).toMatchSnapshot()
expect(extractFieldExamples(nodes)).toMatchSnapshot()
})

it(`ignores fields that have a null value`, () => {
expect(extractFieldExamples({ nodes }).iAmNull).not.toBeDefined()
it(`null fields should have a null value`, () => {
expect(extractFieldExamples(nodes).iAmNull).toBeNull()
})

it(`ignores empty arrays`, () => {
expect(extractFieldExamples({ nodes }).emptyArray).not.toBeDefined()
expect(extractFieldExamples({ nodes }).hair).toBeDefined()
it(`turns empty or sparse arrays to null`, () => {
expect(extractFieldExamples(nodes).emptyArray).toBeNull()
expect(extractFieldExamples(nodes).hair).toBeDefined()
})

it(`build enum values for fields from array on nodes`, () => {
Expand Down
Loading

0 comments on commit a0370a9

Please sign in to comment.