Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby): allow schema customization #11480

Merged
merged 111 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
9926e2d
Schema refactor WIP
freiksenet Feb 1, 2019
d9e6873
Oops
freiksenet Feb 1, 2019
08ab5c2
First working version
freiksenet Feb 4, 2019
1aef564
First working version
freiksenet Feb 4, 2019
ac49f5b
Almost working site
freiksenet Feb 4, 2019
f369296
progress
freiksenet Feb 5, 2019
6add8d9
WWW working
freiksenet Feb 6, 2019
2e25030
Working add type defs
freiksenet Feb 7, 2019
b532d1b
addResolvers
freiksenet Feb 8, 2019
69162b6
Add node model
stefanprobst Feb 8, 2019
b9af766
Handle NonNull and pass context and info in runQuery
stefanprobst Feb 9, 2019
a5b0d55
Don't fetch resolved id again
stefanprobst Feb 9, 2019
f7d80f0
Minor fixes
stefanprobst Feb 9, 2019
23423db
Make www build with Loki
stefanprobst Feb 9, 2019
6e11926
Fix tests
stefanprobst Feb 10, 2019
ed9314b
Pull out resolving nodes
stefanprobst Feb 10, 2019
79754e6
Fix getExampleValue
stefanprobst Feb 10, 2019
6f1e715
Port exampleValue tests
stefanprobst Feb 10, 2019
12a9819
Fixes for type inference
stefanprobst Feb 10, 2019
0ca1294
Infer errors and stuff
freiksenet Feb 11, 2019
b8d0d6f
Merge remote-tracking branch 'stefanprobst/schema-refactor-new-sp' in…
freiksenet Feb 11, 2019
15df9e3
Lint
freiksenet Feb 11, 2019
9a59b47
More changes
freiksenet Feb 12, 2019
7878818
Fix issue with default resolvers
freiksenet Feb 12, 2019
779cb39
Fix tests
freiksenet Feb 12, 2019
d45c97c
Port more tests
freiksenet Feb 12, 2019
10f1405
Move schema composer creation to separate location
freiksenet Feb 13, 2019
c7b1177
Most tests pass
freiksenet Feb 13, 2019
8dbc6e7
Move tests to better location
freiksenet Feb 13, 2019
bf02578
Readd list sorting option
freiksenet Feb 13, 2019
b6ec46f
Node model as a class
freiksenet Feb 13, 2019
add0666
Model layer tests
freiksenet Feb 13, 2019
da8469d
Fix flow type
freiksenet Feb 14, 2019
e7bb44b
Merge remote-tracking branch 'origin/master' into schema-refactor-new
freiksenet Feb 14, 2019
d002f9c
Restore prepare-nodes improvements
freiksenet Feb 14, 2019
73a1f2d
Fixes
freiksenet Feb 14, 2019
448bcfe
Port more tests (#11757)
stefanprobst Feb 14, 2019
2c0b2cf
Merge remote-tracking branch 'origin/master' into schema-refactor-new
freiksenet Feb 14, 2019
292200f
WIP kitchen sink
freiksenet Feb 14, 2019
1cb0aab
Fix tests for Loki, and minor changes (#11765)
stefanprobst Feb 15, 2019
e512743
Satisfy linter
freiksenet Feb 15, 2019
017bfa1
Mix dates and strings behaviour
freiksenet Feb 15, 2019
ccb0bb6
Test more stuff with runQuery
freiksenet Feb 15, 2019
58297c7
Add resolvers test
freiksenet Feb 15, 2019
1d4272e
Updated API names to match gatsby
freiksenet Feb 18, 2019
7666727
3rd party schema tests
freiksenet Feb 18, 2019
7e0261f
More fixes
freiksenet Feb 18, 2019
7f661eb
Beter example in using-type-defs
freiksenet Feb 18, 2019
3ddcf99
More fixes
freiksenet Feb 18, 2019
ff85e86
Add NodeModel tests (#11880)
stefanprobst Feb 19, 2019
31b11f3
[schema] Better example (#11892)
stefanprobst Feb 19, 2019
0c5e13a
Fix conflicting field issue
freiksenet Feb 19, 2019
a440e8e
Handle multiple return values from setFieldsOnGraphQLNodeType (#12048)
stefanprobst Feb 25, 2019
c310140
Fix conflicting fields priority on nested type (#11900)
stefanprobst Feb 25, 2019
c6a7826
Merge remote-tracking branch 'upstream/master' into schema-refactor-new
stefanprobst Feb 26, 2019
4dfc057
Allow field type on third-party type (#11970)
stefanprobst Feb 27, 2019
879cd7b
Make original field resolver available on info (#12159)
stefanprobst Feb 28, 2019
fbfa6b4
[schema] Make output fields non-null (#12097)
stefanprobst Feb 28, 2019
a1c01a6
[schema] Add schema query tests, inference fixes (#12190)
stefanprobst Mar 1, 2019
e344292
[schema] Add default page dependency path (#12220)
stefanprobst Mar 1, 2019
5db668a
Merge remote-tracking branch 'origin/master' into schema-refactor-new
freiksenet Mar 4, 2019
9992c12
chore(release): Publish
freiksenet Mar 4, 2019
ab9ffac
More tests and some fixes
freiksenet Mar 4, 2019
6cc1992
Revert "chore(release): Publish"
freiksenet Mar 4, 2019
b90b05f
Allow custom resolveType (#12309)
stefanprobst Mar 5, 2019
238cc2b
v2.2.0-alpha.0
freiksenet Mar 4, 2019
b706829
More fixes and tests
freiksenet Mar 5, 2019
9269305
More tests, refine infer logic
freiksenet Mar 5, 2019
52d6012
[schema] Make id field on InputObjectType type String (#12335)
stefanprobst Mar 6, 2019
1e58b54
v2.2.0-alpha.1
freiksenet Mar 5, 2019
7eda0b2
Upgrade schema composer
freiksenet Mar 6, 2019
222f3da
v2.2.0-alpha.2
freiksenet Mar 6, 2019
e76e7e8
Fix loxi test issue
freiksenet Mar 6, 2019
fe5fe1d
Add type builders internally
freiksenet Mar 6, 2019
081fa15
Add type builders internally
freiksenet Mar 6, 2019
f3a7cf6
Added type builders to the API
freiksenet Mar 6, 2019
5522c28
v2.2.0-alpha.3
freiksenet Mar 6, 2019
67ee427
[schema] Add NodeModel docs (#12354)
stefanprobst Mar 7, 2019
8628a48
Check for type names reserved for internal use (#12373)
stefanprobst Mar 7, 2019
4e09a7d
Allow custom scalars in input filter (#12375)
stefanprobst Mar 7, 2019
3e06835
Correctly camelcase connections (#12378)
stefanprobst Mar 7, 2019
587e9ef
v2.2.0-alpha.4
freiksenet Mar 7, 2019
16dab78
v2.2.0-alpha.5
freiksenet Mar 7, 2019
5a9a5f0
[schema] Remove empty input fields (#12380)
stefanprobst Mar 8, 2019
6c86d28
[schema] Add code frame to SDL parsing error (#12401)
stefanprobst Mar 8, 2019
ef567f0
Merge branch 'master' into schema-refactor-new
freiksenet Mar 8, 2019
582bd94
v2.2.0-alpha.6
freiksenet Mar 8, 2019
d61a776
Add blog post
freiksenet Mar 12, 2019
3f3e8fe
Don't use npx in example README
muescha Mar 16, 2019
71c391f
Update docs for new documentationjs version (#12615)
stefanprobst Mar 18, 2019
75da5e5
Merge remote-tracking branch 'origin/master' into schema-refactor-new
freiksenet Mar 18, 2019
1bc4013
v2.2.0-rc.1
freiksenet Mar 18, 2019
0a3a750
Revert "v2.2.0-rc.1"
freiksenet Mar 18, 2019
59a2dd2
Remove extra dep
freiksenet Mar 18, 2019
511abb3
Fix date
freiksenet Mar 18, 2019
f46154b
chore(blog): add schema customization blog post release (#12522)
DSchau Mar 18, 2019
582db20
Update blog as per review
freiksenet Mar 18, 2019
50ed38b
Docs
freiksenet Mar 18, 2019
4a6b108
Update docs/blog/2019-03-18-releasing-new-schema-customization/index.md
stefanprobst Mar 18, 2019
ecf5656
Add tweet
freiksenet Mar 18, 2019
0a48280
Update docs/blog/2019-03-18-releasing-new-schema-customization/index.md
stefanprobst Mar 18, 2019
3f2c4a0
Merge remote-tracking branch 'origin/master' into schema-refactor-new
freiksenet Mar 18, 2019
5c2b25a
Remove dep
stefanprobst Mar 18, 2019
c7c7ae0
Move tweet
freiksenet Mar 18, 2019
78cbab4
[schema] Ignore empty strings in example value creation (#12646)
stefanprobst Mar 19, 2019
7d48322
Add tests
freiksenet Mar 19, 2019
196ef58
Fix snapshot
freiksenet Mar 19, 2019
09f0d91
v2.2.0-rc.2
freiksenet Mar 19, 2019
4dcb431
Revert "v2.2.0-rc.2"
freiksenet Mar 19, 2019
54ff3b6
Change blog date
freiksenet Mar 19, 2019
04f7694
use <PROJECT_ROOT> in fixture (#12662)
pieh Mar 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update docs for new documentationjs version (#12615)
  • Loading branch information
stefanprobst authored and freiksenet committed Mar 18, 2019
commit 71c391f4ad24a865039d94cd4d7c39b36c630894
16 changes: 11 additions & 5 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,14 +1173,16 @@ actions.addThirdPartySchema = (
}
}

import type GatsbyGraphQLType from "../schema/types/type-builders"
/**
* Add type definitions to the GraphQL schema.
*
* @param {TypeDefinitions} types Type definitions, where `type TypeDefinitions = string | GraphQLOutputType | string[] | GraphQLOutputType[] | GatsbyGraphQLType`
* @param {string | GraphQLOutputType | GatsbyGraphQLType | string[] | GraphQLOutputType[] | GatsbyGraphQLType[]} types Type definitions
*
* Type definitions can be provided either as
* [`graphql-js` types](https://graphql.org/graphql-js/), in
* [GraphQL schema definition language (SDL)](https://graphql.org/learn/) or using Gatsby Type Builders.
* [GraphQL schema definition language (SDL)](https://graphql.org/learn/)
* or using Gatsby Type Builders available on the `schema` API argument.
*
* Things to note:
* * needs to be called *before* schema generation. It is recommended to use
Expand All @@ -1193,7 +1195,7 @@ actions.addThirdPartySchema = (
* with inferred field types, and default field resolvers for `Date` (which
* adds formatting options) and `File` (which resolves the field value as
* a `relativePath` foreign-key field) are added. This behavior can be
* customised with `@infer` and `dontInfer` directives, and their
* customised with `@infer` and `@dontInfer` directives, and their
* `noDefaultResolvers` argument.
*
* @example
Expand Down Expand Up @@ -1230,7 +1232,7 @@ actions.addThirdPartySchema = (
* createTypes(typeDefs)
* }
*
* // using type builder API
* // using Gatsby Type Builder API
* exports.sourceNodes = ({ actions, schema }) => {
* const { createTypes } = actions
* const typeDefs = [
Expand Down Expand Up @@ -1260,7 +1262,11 @@ actions.addThirdPartySchema = (
* }
*/
actions.createTypes = (
types: string | GraphQLOutputType | Array<string | GraphQLOutputType>,
types:
| string
| GraphQLOutputType
| GatsbyGraphQLType
| Array<string | GraphQLOutputType | GatsbyGraphQLType>,
plugin: Plugin,
traceId?: string
) => {
Expand Down
51 changes: 31 additions & 20 deletions packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,41 @@ const invariant = require(`invariant`)
type IDOrNode = string | { id: string }
type TypeOrTypeName = string | GraphQLOutputType

interface ConnectionArgs {
/**
* Optional page dependency information.
*
* @typedef {Object} PageDependencies
* @property {string} path The path of the page that depends on the retrieved nodes' data
* @property {string} [connectionType] Mark this dependency as a connection
*/
interface PageDependencies {
path: string;
connectionType?: string;
}

interface QueryArguments {
type: TypeOrTypeName;
query: { filter: Object, sort?: Object, skip?: number, limit?: number };
firstOnly?: boolean;
}

export interface NodeModel {
getNodeById(
{ id: IDOrNode, type?: TypeOrTypeName },
ConnectionArgs
pageDependencies?: PageDependencies
): any | null;
getNodesByIds(
{ ids: Array<IDOrNode>, type?: TypeOrTypeName },
ConnectionArgs
pageDependencies?: PageDependencies
): Array<any>;
getAllNodes(
{ type?: TypeOrTypeName },
pageDependencies?: PageDependencies
): Array<any>;
getAllNodes({ type?: TypeOrTypeName }, ConnectionArgs): Array<any>;
runQuery(args: any): Promise<any>;
runQuery(
args: QueryArguments,
pageDependencies?: PageDependencies
): Promise<any>;
getTypes(): Array<string>;
}

Expand All @@ -38,14 +57,6 @@ class LocalNodeModel {
this.path = path
}

/**
* Optional page dependency information.
*
* @typedef {Object} PageDependencies
* @property {string} path
* @property {string} [connectionType]
*/

/**
* Get a node from the store by ID and optional type.
*
Expand Down Expand Up @@ -83,7 +94,7 @@ class LocalNodeModel {
* @param {Object} args
* @param {string[]} args.ids IDs of the requested nodes
* @param {(string|GraphQLOutputType)} [args.type] Optional type of the nodes
* @param {PageDependencies} [pageDependencies] *
* @param {PageDependencies} [pageDependencies]
* @returns {Node[]}
*/
getNodesByIds(args, pageDependencies) {
Expand Down Expand Up @@ -113,7 +124,7 @@ class LocalNodeModel {
*
* @param {Object} args
* @param {(string|GraphQLOutputType)} [args.type] Optional type of the nodes
* @param {PageDependencies} [pageDependencies] *
* @param {PageDependencies} [pageDependencies]
* @returns {Node[]}
*/
getAllNodes(args, pageDependencies) {
Expand Down Expand Up @@ -142,10 +153,10 @@ class LocalNodeModel {
* Get nodes of a type matching the specified query.
*
* @param {Object} args
* @param {Object} args.query Query arguments
* @param {Object} args.query Query arguments (`filter`, `sort`, `limit`, `skip`)
* @param {(string|GraphQLOutputType)} args.type Type
* @param {boolean} [args.firstOnly] If true, return only first match
* @param {PageDependencies} [pageDependencies] *
* @param {PageDependencies} [pageDependencies]
* @returns {Promise<Node[]>}
*/
async runQuery(args, pageDependencies) {
Expand Down Expand Up @@ -206,12 +217,12 @@ class LocalNodeModel {
* Get the root ancestor node for an object's parent node, or its first
* ancestor matching a specified condition.
*
* @param {Object} obj The object you want to retrieve an ancestor node for
* @param {(Object|Array)} obj An object belonging to a Node, or a Node object
* @param {Function} [predicate] Optional condition to match
* @returns {(Node|null)}
*/
findRootNodeAncestor(...args) {
return this.nodeStore.findRootNodeAncestor(...args)
findRootNodeAncestor(obj, predicate) {
return this.nodeStore.findRootNodeAncestor(obj, predicate)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/api-node-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ exports.setFieldsOnGraphQLNodeType = true
* resolve: (source, args, context, info) => {
* const posts = context.nodeModel.getAllNodes({ type: `BlogPost` })
* const recentPosts = posts.filter(
* post => post.publishedAt > Date.UTC(2018, 0 , 1)
* post => post.publishedAt > Date.UTC(2018, 0, 1)
* )
* return recentPosts
* }
Expand Down
43 changes: 34 additions & 9 deletions www/src/pages/docs/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { graphql } from "gatsby"
import { Helmet } from "react-helmet"
import sortBy from "lodash/sortBy"

import Functions from "../../components/function-list"
import { rhythm, scale } from "../../utils/typography"
import APIReference from "../../components/api-reference"
import { rhythm } from "../../utils/typography"
import { space } from "../../utils/presets"
import Layout from "../../components/layout"
import Container from "../../components/container"
import { itemListDocs } from "../../utils/sidebar/item-list"
Expand All @@ -14,7 +15,7 @@ class NodeModelDocs extends React.Component {
const funcs = sortBy(
this.props.data.file.childrenDocumentationJs,
func => func.name
)
).filter(func => !func.type) // Filter out @typedef
return (
<Layout location={this.props.location} itemList={itemListDocs}>
<Container>
Expand All @@ -28,19 +29,43 @@ class NodeModelDocs extends React.Component {
Gatsby exposes its internal data store and query capabilities to
GraphQL field resolvers on <code>context.nodeModel</code>.
</p>
<div className="gatsby-code-title">Example usage</div>
<div className="gatsby-highlight">
<pre
className="language-javascript"
dangerouslySetInnerHTML={{
__html:
`<code class="language-javascript">\n` +
`<span class="token function">createResolvers</span><span class="token punctuation">(</span><span class="token punctuation">{</span>\n` +
` Query<span class="token punctuation">:</span> <span class="token punctuation">{</span>\n` +
` mood<span class="token punctuation">:</span> <span class="token punctuation">{</span>\n` +
` type<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">\`String\`</span></span><span class="token punctuation">,</span>\n` +
` <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token parameter">source<span class="token punctuation">,</span> args<span class="token punctuation">,</span> context<span class="token punctuation">,</span> info</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n` +
` <span class="token keyword">const</span> coffee <span class="token operator">=</span> context<span class="token punctuation">.</span>nodeModel<span class="token punctuation">.</span><span class="token function">getAllNodes</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">\`Coffee\`</span></span> <span class="token punctuation">}</span><span class="token punctuation">)</span>\n` +
` <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>coffee<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>\n` +
` <span class="token keyword">return</span> 😞\n` +
` <span class="token punctuation">}</span>\n` +
` <span class="token keyword">return</span> 😊\n` +
` <span class="token punctuation">}</span><span class="token punctuation">,</span>\n` +
` <span class="token punctuation">}</span><span class="token punctuation">,</span>\n` +
` <span class="token punctuation">}</span><span class="token punctuation">,</span>\n` +
`<span class="token punctuation">}</span><span class="token punctuation">)</span>\n` +
`</code>`,
}}
/>
</div>
<hr />
<h2 css={{ marginBottom: rhythm(1 / 2) }}>Methods</h2>
<ul css={{ ...scale(-1 / 5) }}>
<h2 css={{ marginBottom: rhythm(space[3]) }}>Methods</h2>
<ul>
{funcs.map((node, i) => (
<li key={`function list ${node.name}`}>
<a href={`#${node.name}`}>{node.name}</a>
</li>
))}
</ul>
<br />
<hr />
<h2>Reference</h2>
<Functions functions={funcs} />
<APIReference docs={funcs} />
</Container>
</Layout>
)
Expand All @@ -51,10 +76,10 @@ export default NodeModelDocs

export const pageQuery = graphql`
query {
file(relativePath: { regex: "/src.*node-model.js/" }) {
file(relativePath: { eq: "gatsby/src/schema/node-model.js" }) {
childrenDocumentationJs {
name
...FunctionList
...DocumentationFragment
}
}
}
Expand Down