-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
3dbc3eb
commit a0370a9
Showing
19 changed files
with
1,419 additions
and
970 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 |
---|---|---|
|
@@ -21,7 +21,8 @@ | |
}, | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
"node": true, | ||
"jest": true | ||
}, | ||
"rules": { | ||
"no-console": "off", | ||
|
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
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
File renamed without changes.
95 changes: 95 additions & 0 deletions
95
packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap
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,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
98
packages/gatsby/src/schema/__tests__/build-node-types-test.js
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,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`) | ||
}) | ||
}) |
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
Oops, something went wrong.