-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(docs): Add loading prop usage on Connect component and mutation code snippet #11
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
Conversation
| <Connect query={graphqlOperation(ListEvents)}> | ||
| {({ data: { listEvents } }) => ( | ||
| <ListView events={listEvents.items} /> | ||
| {({ data: { listEvents }, loading }) => ( |
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.
Can we do something a little more comprehensive that also includes error checking like so:
<Connect query={graphqlOperation(ListEvents)}>
{({ data: { listEvents }, loading, error }) => (
if (error) return <h1>Error fetching results</h1>
if (loading || !data) return <h1>Loading...</h1>
return <ListView events={listEvents.items} />
)}
</Connect>
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.
For sure, I will try this and update the docs. Thanks!
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.
Here is the functioning snippet in JSX:
<Connect query={graphqlOperation(ListTodos)}>
{({ data: { listTodos }, loading, error }) => {
if (error) return <h1>Error</h1>;
if (loading || !listTodos) return <h1>Loading...</h1>;
return <ListView todos={listTodos.items} />
}}
</Connect>
| return prev; }}> | ||
| {({ data: { listEvents } }) => ( | ||
| <AllEvents events={listEvents ? listEvents.items : []} /> | ||
| {({ data: { listEvents }, loading }) => ( |
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.
Similar to the above, let's be more prescriptive on the error handling and also call out the function argument from the GraphQL response. Here is a suggestion for the code and text:
<Connect query={graphqlOperation(ListTodos)}
subscription={graphqlOperation(OnCreateTodo)}
onSubscriptionMsg={(prev, {onCreateTodo}) => {
console.log('Subscription data:', onCreateTodo)
return prev;
}
}>
{({ data: { listTodos }, loading, error }) => {
if (error) return <h1>Error</h1>;
if (loading || !listTodos) return <h1>Loading...</h1>;
return <ListView todos={listTodos.items} />
}}
</Connect>
Note - In the above example OnCreateTodo passed to the subscription prop as subscription={graphqlOperation(OnCreateTodo)} is the name of your GraphQL document. For example when used with the Amplify CLI it will generate a document and you would use import { OnCreateTodo } from './graphql/subscriptions'; at the top of your app code. However the argument onCreateTodo received by the onSubscriptionMsg is defined in the GraphQL selection set. For example your GraphQL statement would look like the following here:
export const OnCreateTodo = `
subscription OnCreateTodo {
onCreateTodo {
id
name
description
}
}
`;
| // const variables = { id: '1' } | ||
| // this.props.onCreate(variables) | ||
| // ... | ||
| class CreateEventView extends React.Component { |
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 since this is an example we can simplify it a bit so that new customers to React don't get lost on the extra state management here for handling input. Also, we should show the input type since that's what we're doing with our coegen. Suggested simplification which builds upon the last example:
class AddTodo extends Component {
constructor(props) {
super(props);
console.log(props);
}
submit = async () => {
const { onCreate } = this.props;
var input = {
name: 'TEST',
description: 'TEST'
}
console.log(input);
await onCreate({input})
}
render(){
return (
<button onClick={this.submit}>Click</button>
);
}
}
class App extends Component {
render() {
const ListView = ({ todos }) => (
<div>
<h3>All Todos</h3>
<ul>
{todos.map(todo => <li key={todo.id}>{todo.name}</li>)}
</ul>
</div>
)
return (
<div className="App">
<Connect mutation={graphqlOperation(CreateTodo)}>
{({mutation}) => (
<AddTodo onCreate={mutation} />
)}
</Connect>
<Connect query={graphqlOperation(ListTodos)}
subscription={graphqlOperation(OnCreateTodo)}
onSubscriptionMsg={(prev, {onCreateTodo}) => {
console.log('Subscription data:', onCreateTodo)
return prev;
}
}>
{({ data: { listTodos }, loading, error }) => {
if (error) return <h1>Error</h1>;
if (loading || !listTodos) return <h1>Loading...</h1>;
return <ListView todos={listTodos.items} />
}}
</Connect>
</div>
);
}
}
undefobj
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.
@elorzafe here are the suggested changes from our chat earlier with working code. One last thought is you might want to use my code for a Todo rather than Event type, since the beginning of the API guide already shows how to use the GraphQL Transformer with this type definition so we can keep it consistent.
|
I agree, it would be much nice if the whole instructions flow from the Todo type. I will prepare the next iteration based on the Todo, It will be required to change all the graphql queries. |
|
Closing this because we will use #16 |
``` E/flutter (30435): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>' E/flutter (30435): #0 new CognitoSignUpOptions (package:amplify_auth_cognito/src/CognitoSignUp/CognitoSignUpOptions.dart:20:87) E/flutter (30435): #1 SignUpState._signUp (package:AwsAmplifySample/components/signUp.dart:29:20) E/flutter (30435): #2 SignUpState.build.<anonymous closure> (package:AwsAmplifySample/components/signUp.dart:190:35) E/flutter (30435): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19) E/flutter (30435): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38) E/flutter (30435): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24) E/flutter (30435): #6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11) E/flutter (30435): #7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5) E/flutter (30435): #8 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:259:7) E/flutter (30435): #9 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27) E/flutter (30435): #10 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:362:20) E/flutter (30435): #11 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:338:22) E/flutter (30435): #12 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:267:11) E/flutter (30435): #13 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:295:7) E/flutter (30435): #14 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:240:7) E/flutter (30435): #15 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:213:7) E/flutter (30435): #16 _rootRunUnary (dart:async/zone.dart:1206:13) E/flutter (30435): #17 _CustomZone.runUnary (dart:async/zone.dart:1100:19) E/flutter (30435): #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7) E/flutter (30435): #19 _invoke1 (dart:ui/hooks.dart:265:10) E/flutter (30435): #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5) E/flutter (30435): ``` If `dynamic` is used
To be merged after publishing change on Connect component
aws-amplify/amplify-js#1868