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

Document that "at" and "get" have special meaning on steps #497

Open
benjie opened this issue Sep 2, 2023 · 5 comments · May be fixed by graphile/crystal#2218
Open

Document that "at" and "get" have special meaning on steps #497

benjie opened this issue Sep 2, 2023 · 5 comments · May be fixed by graphile/crystal#2218
Assignees
Labels
good first issue Good for newcomers
Milestone

Comments

@benjie
Copy link
Member

benjie commented Sep 2, 2023

  • at should accept a single number argument and implies that the step is list-like (and you're accessing that index)
  • get should accept a single string argument and implies that the step is object-like (and you're accessing that key)

If steps implement these methods but don't conform to expectations, then odd things might happen.

@benjie benjie added the good first issue Good for newcomers label Sep 2, 2023
@benjie benjie added this to the V5 RC milestone Sep 2, 2023
@benjie
Copy link
Member Author

benjie commented Sep 2, 2023

See isObjectLikeStep and isListLikeStep functions (the latter is currently on a branch)

@benjie benjie added this to V5.0.0 Oct 4, 2023
@github-project-automation github-project-automation bot moved this to 🌳 Triage in V5.0.0 Oct 4, 2023
@benjie benjie moved this from 🌳 Triage to 🐭 Shrew in V5.0.0 Oct 4, 2023
@benjie
Copy link
Member Author

benjie commented Feb 8, 2024

@jemgillam please can you add details of this convention to the Grafast docs, probably in the section talking about writing your own step classes (since these are step class methods)?

@benjie benjie moved this from 🐭 Shrew to 📝 Docs Improvements in V5.0.0 May 10, 2024
@jemgillam
Copy link
Collaborator

jemgillam commented Oct 15, 2024

A plan resolver, rather than using access(), SHOULD use .get and .at methods if present on the steps you're interacting with.

This allows the step to learn more about how it is going to be used

eg optimize its fetching to only retrieve the requested attributes (select id, name, avatar from ... rather than select * from ...)

For some steps this is a MUST, for example if $user is a PgSelectSingleStep then $user.get('username') will represent the user's username; but access($user, 'username') will yield undefined since the internal representation of $user is actually a tuple (array) of just the attributes that were requested via .get(colName).


When writing a step class, if the step represents an object, you SHOULD implement .get. If a list or array, you SHOULD implement the .at method.

If optimizing isn't a concern, then you can implement those methods but you can defer to access under the hood. This means later you can refactor and optimize without affecting code using that step class.

class MyStep extends ExecutableStep {
  // ...
  
  // For steps representing objects:
  get(key) { return access(this, key); }

  // For steps representing lists:
  at(index) { return access(this, index); }

If your step implements a .at or .get method if MUST conform to the expectations; i.e. get() should accept a single argument, a string, representing an attribute to access on an object-like value, and at() should accept a single argument, an integer, representing the index within the list-like value which should be accessed. Grafast relies on these assumptions, so if your step implements .get() or .at() and does not conform, unexpected behaviors may be observed in some circumstances.

@jemgillam
Copy link
Collaborator

#263 might be related

@benjie
Copy link
Member Author

benjie commented Oct 24, 2024

#263 relates to FieldArgs, and though they adhere to the pattern of allowing .get('key') they don't otherwise overlap with this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Status: 📝 Docs Improvements
Development

Successfully merging a pull request may close this issue.

2 participants