Skip to content

Commit

Permalink
Use a consistent if (this.many) {} else {} pattern in relationships (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Jul 24, 2020
1 parent 271f1a4 commit fd2b8d1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-bikes-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/fields': patch
---

Refactored relationship internals. No functional changes.
76 changes: 38 additions & 38 deletions packages/fields/src/types/Relationship/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export class Relationship extends Implementation {
`${this.path}(${filterArgs}): [${refList.gqlNames.outputTypeName}!]!`,
this.withMeta ? `_${this.path}Meta(${filterArgs}): _QueryMeta` : '',
];
} else {
return [`${this.path}: ${refList.gqlNames.outputTypeName}`];
}

return [`${this.path}: ${refList.gqlNames.outputTypeName}`];
}

extendAdminMeta(meta) {
Expand Down Expand Up @@ -103,8 +103,27 @@ export class Relationship extends Implementation {
return [];
}

// to-one relationships are much easier to deal with.
if (!this.many) {
if (this.many) {
return {
[this.path]: (item, args, context, info) => {
return refList.listQuery(args, context, info.fieldName, info, {
fromList: this.getListByKey(this.listKey),
fromId: item.id,
fromField: this.path,
});
},

...(this.withMeta && {
[`_${this.path}Meta`]: (item, args, context, info) => {
return refList.listQueryMeta(args, context, info.fieldName, info, {
fromList: this.getListByKey(this.listKey),
fromId: item.id,
fromField: this.path,
});
},
}),
};
} else {
return {
[this.path]: (item, _, context, info) => {
// No ID set, so we return null for the value
Expand All @@ -119,26 +138,6 @@ export class Relationship extends Implementation {
},
};
}

return {
[this.path]: (item, args, context, info) => {
return refList.listQuery(args, context, info.fieldName, info, {
fromList: this.getListByKey(this.listKey),
fromId: item.id,
fromField: this.path,
});
},

...(this.withMeta && {
[`_${this.path}Meta`]: (item, args, context, info) => {
return refList.listQueryMeta(args, context, info.fieldName, info, {
fromList: this.getListByKey(this.listKey),
fromId: item.id,
fromField: this.path,
});
},
}),
};
}

/**
Expand Down Expand Up @@ -267,33 +266,34 @@ export class Relationship extends Implementation {
}
`,
];
}
if (refList.access[schemaName].create) {
operations.push(`# Provide data to create a new ${refList.key}.
} else {
if (refList.access[schemaName].create) {
operations.push(`# Provide data to create a new ${refList.key}.
create: ${refList.gqlNames.createInputName}`);
}
operations.push(
`# Provide a filter to link to an existing ${refList.key}.
}
operations.push(
`# Provide a filter to link to an existing ${refList.key}.
connect: ${refList.gqlNames.whereUniqueInputName}`,
`# Provide a filter to remove to an existing ${refList.key}.
`# Provide a filter to remove to an existing ${refList.key}.
disconnect: ${refList.gqlNames.whereUniqueInputName}`,
`# Remove the existing ${refList.key} (if any).
`# Remove the existing ${refList.key} (if any).
disconnectAll: Boolean`
);
return [
`input ${refList.gqlNames.relateToOneInputName} {
);
return [
`input ${refList.gqlNames.relateToOneInputName} {
${operations.join('\n')}
}
`,
];
];
}
}
get gqlUpdateInputFields() {
const { refList } = this.tryResolveRefList();
if (this.many) {
return [`${this.path}: ${refList.gqlNames.relateToManyInputName}`];
} else {
return [`${this.path}: ${refList.gqlNames.relateToOneInputName}`];
}

return [`${this.path}: ${refList.gqlNames.relateToOneInputName}`];
}
get gqlCreateInputFields() {
return this.gqlUpdateInputFields;
Expand Down

0 comments on commit fd2b8d1

Please sign in to comment.