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

[schema] Ensure default by: id for link() resolver #13591

Merged
Merged
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const paginate = (results = [], { skip = 0, limit }) => {
}
}

const link = ({ by, from }) => async (source, args, context, info) => {
const link = ({ by = `id`, from }) => async (source, args, context, info) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can/need to generate a unique id (based on timestamp or something) to set as default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sidharthachatterjee This is about the default node field for foreign-key relations. So that it is possible to say something like this in SDL:

type Post implements Node {
  author: AuthorJson @addResolver(type: "link")
  # instead of:
  # author: AuthorJson @addResolver(type: "link", options: { by: "id" })
}

The ID itself must already be unique on nodes. Hope that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense! Thank you for the added context, @stefanprobst

const fieldValue = source && source[from || info.fieldName]

if (fieldValue == null || _.isPlainObject(fieldValue)) return fieldValue
Expand Down