Skip to content

Commit

Permalink
Merge branch 'master' into noviny/use-new-changesets-package
Browse files Browse the repository at this point in the history
  • Loading branch information
Noviny committed May 7, 2019
2 parents ea1df57 + 3aeabc5 commit 32532d1
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 170 deletions.
1 change: 1 addition & 0 deletions .changeset/03c2ad30/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "@keystone-alpha/keystone", "type": "patch" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/03c2ad30/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix bug in resolver for createMany mutations
1 change: 1 addition & 0 deletions .changeset/9048213f/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "@keystone-alpha/fields", "type": "patch" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/9048213f/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Refactor Content Type to extend Relationship Type to simplify implementation and enable future enhancements
4 changes: 4 additions & 0 deletions packages/fields/src/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export class Block {
get path() {
throw new Error(`${this.constructor.name} must have a 'path' getter`);
}

get fieldDefinitions() {
return {};
}
}
94 changes: 44 additions & 50 deletions packages/fields/src/types/CloudinaryImage/ImageBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import CloudinaryImage from './';
import SelectType from '../Select';
import RelationshipType from '../Relationship';

const RelationshipWrapper = {
...RelationshipType,
implementation: class extends RelationshipType.implementation {
async resolveNestedOperations(operations, item, context, ...args) {
const result = await super.resolveNestedOperations(operations, item, context, ...args);
context._blockMeta = context._blockMeta || {};
context._blockMeta[this.listKey] = context._blockMeta[this.listKey] || {};
context._blockMeta[this.listKey][this.path] = result;
return result;
}
},
};

export class ImageBlock extends Block {
constructor({ adapter }, { type, fromList, createAuxList, getListByKey, listConfig }) {
constructor({ adapter }, { type, fromList, joinList, createAuxList, getListByKey }) {
super();

this.fromList = fromList;
this.type = type;
this.joinList = joinList;

const auxListKey = `_Block_${fromList}_${this.type}`;

Expand All @@ -19,72 +32,53 @@ export class ImageBlock extends Block {
if (!auxList) {
auxList = createAuxList(auxListKey, {
fields: {
image: { type: CloudinaryImage, isRequired: true, adapter },
image: {
type: CloudinaryImage,
isRequired: true,
adapter,
schemaDoc: 'Cloudinary Image data returned from the Cloudinary API',
},
align: {
type: SelectType,
defaultValue: 'center',
options: ['left', 'center', 'right'],
schemaDoc: 'Set the image alignment',
},
// Useful for doing reverse lookups such as:
// - "Get all images in this post"
// - "List all users mentioned in comment"
from: {
type: RelationshipType,
isRequired: true,
ref: `${joinList}.${this.path}`,
schemaDoc:
'A reference back to the Slate.js Serialised Document this image is embedded within',
},
// TODO: Inject the back reference to the item & field which created
// this entry in the aux list
//from: { type: RelationshipType, isRequired: true, ref: fromList },
//field: { type: TextType, isRequired: true },
},
});
}

this.auxList = auxList;

// When content blocks are specified that have complex KS5 datatypes, the
// client needs to send them along as graphQL inputs separate to the
// `document`. Those inputs are relationships to our join tables. Here we
// create a Relationship field to leverage existing functionality for
// generating the graphQL schema.
this._inputFields = [
new RelationshipType.implementation(
this.path,
{ ref: auxListKey, many: true, withMeta: false },
listConfig
),
];

this._outputFields = [
new RelationshipType.implementation(
this.path,
{ ref: auxListKey, many: true, withMeta: false },
listConfig
),
];
}

get path() {
return pluralize.plural(this.type);
}

getGqlInputFields() {
return this._inputFields;
}

getGqlOutputFields() {
return this._outputFields;
get fieldDefinitions() {
return {
[this.path]: {
type: RelationshipWrapper,
ref: this.auxList.key,
many: true,
schemaDoc: 'Images which have been added to the Content field',
},
};
}

async processMutations(input, { existingItem, context }) {
const mutationState = {
afterChangeStack: [], // post-hook stack
queues: {}, // backlink queues
transaction: {}, // transaction
getMutationOperationResults({ context }) {
return {
[this.path]: context._blockMeta[this.joinList][this.path],
};
// TODO: Inject the back reference into `input` once we have the `from`
// field setup on the aux list.
const operations = await this._inputFields[0].resolveNestedOperations(
input,
existingItem,
context,
undefined,
mutationState
);

return operations;
}
}
Loading

0 comments on commit 32532d1

Please sign in to comment.