-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update description of Fragments to emphasize evolving data needs #1193
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for graphql-spec-draft ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Definitely agree on the motivation for this change; thanks for the submission Janette! |
|
I think that is is great change overall. The challenge is a bit to make it understandable in the context of the spec. Using fragments to declare data needs (like in relay) always requires something on top of the spec to make it work. So we would have to make this clear somehow. |
martinbonnin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple comments but otherwise very supportive of updating that language!
benjie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling the logic friendProfile and the fragment friendProfile is confusing. Can we differentiate the logic - e.g. just call it "profile rendering logic" (prose) rather than using code? (Not sure if this is the best fix.)
spec/Section 2 -- Language.md
Outdated
| duplicated text in the document. Inline Fragments can be used directly within a | ||
| selection to condition upon a type condition when querying against an interface | ||
| or union. | ||
| Fragments allow for the definition of modular selection sets that make it easier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to flush out what modular means. Matt + Pascal's suggestion: single purpose.
spec/Section 2 -- Language.md
Outdated
| Fragments allow for the definition of modular selection sets that make it easier | ||
| to add and remove selections as needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something along the lines of (written whilst on Zoom, not final wording!):
Fragments allow client components and logic to describe their specific
data requirements locally. Fragments can then be composed together to
form a GraphQL operation to issue to the server.
|
Just updated based off some of the feedback from the working group meeting!
|
|
The video is just out. Can we merge this and iterate on the wording if needed. I'd love the message to be consistent. |
michaelstaib
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how Lee feels about using _ in fragment names, but I'd be tempted to adopt Relay naming conventions here and be super explicit.
Perhaps we could lay out the entire scenario showing multi-level composition?
Example
Each data-consuming component (function, class, UI element, and so on) of a client application should declare its data needs in a dedicated fragment. These fragments may then be composed, following the usage of the components themselves, to form a GraphQL operation to issue to the server.
Consider a social media app. The userCard(user, layout) function renders a tiny details card about a user for inclusion in other components. For this, it requires certain information about the user which we can describe with a fragment dedicated to this purpose:
fragment userCard_user on User {
id
name
profilePic(size: 50)
}When rendering a user's profile with the userProfile(user) function, the userCard can be used to display each of the user's friends and mutual friends. The userProfile() function does not need to know about the specific requirements of the userCard() function since it can simply compose it's dedicated fragment in the relevant places:
fragment userProfile_user on User {
id
name
profilePic(size: 250)
biography
friends(first: 10) {
...userCard_user
}
mutualFriends(first: 10) {
...userCard_user
}
}This fragment can be used whenever a user profile is displayed, for example when hovering over a user, or on their dedicated user profile page (which may also need to compose fragments for additional concerns):
query userProfilePage($id: Int!) {
user(id: $id) {
...userProfile_user
}
...sideBar_query
...titleBar_query
}| Each client component should declare its data needs in a dedicated fragment. | ||
| These fragments may then be composed in the same fashion as the components | ||
| themselves to form a GraphQL operation to issue to the server. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned that "component" here will be read too specifically (e.g. to mean a React component) rather than broadly (e.g. a "component" of client logic such as a function, class, or other...). Perhaps something along the lines of:
| Each client component should declare its data needs in a dedicated fragment. | |
| These fragments may then be composed in the same fashion as the components | |
| themselves to form a GraphQL operation to issue to the server. | |
| Each data-consuming component (function, class, UI element, and so on) of | |
| a client application should declare its data needs in a dedicated fragment. | |
| These fragments may then be composed, following the usage of the components | |
| themselves, to form a GraphQL operation to issue to the server. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth noting here that this places a should on the client not the server.
I like this! The spec should be as framework and implementation agnostic as possible
Yes! This is great. I'm hearing from our users (RedwoodJS/CedarJS) that fragments are seen as a very advanced concept. A full example like this really helps. (And when this change lands, I'll definitely have to update the Cedar docs on fragments) |
Would like to keep examples camel case in the spec |
Why these changes?
Fragments are not for reuse.
The current language in section 2.8 on Fragments, namely the sentence...
...encourages using fragments to deduplicate common selections, even if those selections represent independent data needs.
For example, let's consider these two functions
Given these two functions currently both use
authorNameandcontentText, the current language in the spec encourages one to write a fragmentIf
formatPostForFeednow needstimestamp, we will add naturally addtimestamptoPostDisplayFragmentIf we have the following queries
notice how
NotificationQueryis now over-fetching timestamp!The key observation is that
formatPostForFeedandformatPostForNotificationare two independent functions, so by having them both rely on a single fragment to express their data needs, we are creating a dependency where one should not exist (because that dependency does not exist in the product logic itself).What are the proposed changes?
Updated:
The goal is to emphasize that fragments support evolving data needs (as opposed to recommending people identify common selections that are currently in an executable document).
Open to any and all feedback on the motivation for the change and how it's communicated via changes in the spec language!