mirror of
https://github.com/BradNut/graphbrainz
synced 2025-09-08 17:40:32 +00:00
Improve support for fragments
This commit is contained in:
parent
9e75127d0d
commit
8eb0feaa8a
4 changed files with 26 additions and 16 deletions
|
|
@ -1,6 +1,6 @@
|
|||
# Schema Types
|
||||
|
||||
You may also be interested in the [schema in GraphQL syntax](schema.md).
|
||||
You may also be interested in reading the [schema in GraphQL syntax](schema.md).
|
||||
|
||||
<details><summary>**Table of Contents**</summary><p><ul>
|
||||
<li>[Query](#query)</li>
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ sortBy(interfaces, 'name')
|
|||
|
||||
console.log('# Schema Types\n')
|
||||
|
||||
console.log('You may also be interested in the [schema in GraphQL syntax](schema.md).\n')
|
||||
console.log('You may also be interested in reading the [schema in GraphQL syntax](schema.md).\n')
|
||||
|
||||
console.log('<details><summary>**Table of Contents**</summary><p><ul>')
|
||||
console.log(' <li>[Query](#query)</li>')
|
||||
|
|
|
|||
|
|
@ -6,16 +6,16 @@ import {
|
|||
} from 'graphql-relay'
|
||||
import { getFields, extendIncludes } from './util'
|
||||
|
||||
export function includeRelationships (params, info) {
|
||||
let fields = getFields(info)
|
||||
export function includeRelationships (params, info, fragments = info.fragments) {
|
||||
let fields = getFields(info, fragments)
|
||||
if (info.fieldName !== 'relationships') {
|
||||
if (fields.relationships) {
|
||||
fields = getFields(fields.relationships)
|
||||
fields = getFields(fields.relationships, fragments)
|
||||
} else {
|
||||
if (fields.edges) {
|
||||
fields = getFields(fields.edges)
|
||||
fields = getFields(fields.edges, fragments)
|
||||
if (fields.node) {
|
||||
return includeRelationships(params, fields.node)
|
||||
return includeRelationships(params, fields.node, fragments)
|
||||
}
|
||||
}
|
||||
return params
|
||||
|
|
@ -36,13 +36,13 @@ export function includeRelationships (params, info) {
|
|||
return params
|
||||
}
|
||||
|
||||
export function includeSubqueries (params, info) {
|
||||
export function includeSubqueries (params, info, fragments = info.fragments) {
|
||||
const subqueryIncludes = {
|
||||
aliases: 'aliases',
|
||||
artistCredit: 'artist-credits',
|
||||
tags: 'tags'
|
||||
}
|
||||
let fields = getFields(info)
|
||||
let fields = getFields(info, fragments)
|
||||
const include = []
|
||||
for (const key in subqueryIncludes) {
|
||||
if (fields[key]) {
|
||||
|
|
@ -55,9 +55,9 @@ export function includeSubqueries (params, info) {
|
|||
inc: extendIncludes(params.inc, include)
|
||||
}
|
||||
if (fields.edges) {
|
||||
fields = getFields(fields.edges)
|
||||
fields = getFields(fields.edges, fragments)
|
||||
if (fields.node) {
|
||||
params = includeSubqueries(params, fields.node)
|
||||
params = includeSubqueries(params, fields.node, fragments)
|
||||
}
|
||||
}
|
||||
return params
|
||||
|
|
@ -84,7 +84,7 @@ export function browseResolver () {
|
|||
offset: getOffsetWithDefault(after, -1) + 1
|
||||
}
|
||||
params = includeSubqueries(params, info)
|
||||
params = includeRelationships(params, info)
|
||||
params = includeRelationships(params, info, info.fragments)
|
||||
const formatValue = value => value.toLowerCase().replace(/ /g, '')
|
||||
params.type = params.type.map(formatValue)
|
||||
params.status = params.status.map(formatValue)
|
||||
|
|
|
|||
18
src/util.js
18
src/util.js
|
|
@ -1,14 +1,24 @@
|
|||
import util from 'util'
|
||||
|
||||
export function getFields (info) {
|
||||
export function getFields (info, fragments = info.fragments) {
|
||||
if (info.kind !== 'Field') {
|
||||
info = info.fieldNodes[0]
|
||||
}
|
||||
const selections = info.selectionSet.selections
|
||||
return selections.reduce((fields, selection) => {
|
||||
fields[selection.name.value] = selection
|
||||
const reducer = (fields, selection) => {
|
||||
if (selection.kind === 'FragmentSpread') {
|
||||
const name = selection.name.value
|
||||
const fragment = fragments[name]
|
||||
if (!fragment) {
|
||||
throw new Error(`Fragment '${name}' was not passed to getFields()`)
|
||||
}
|
||||
fragment.selectionSet.selections.reduce(reducer, fields)
|
||||
} else {
|
||||
fields[selection.name.value] = selection
|
||||
}
|
||||
return fields
|
||||
}, {})
|
||||
}
|
||||
return selections.reduce(reducer, {})
|
||||
}
|
||||
|
||||
export function prettyPrint (obj, { depth = 5,
|
||||
|
|
|
|||
Loading…
Reference in a new issue