I have CreateSessionMutation with sid field defined in getMutation and getFatQuery.
export default class CreateSessionMutation extends Relay.Mutation {
getMutation() {
return Relay.QL`mutation {createSession { session { sid } }}`
}
getVariables() {
return {
email: this.props.email,
password: this.props.password
}
}
getFatQuery() {
return Relay.QL`
fragment on CreateSessionPayload {
session {
sid
}
}
`
}
getConfigs() {
return []
}
getCollisionKey() {
return `createSession`
}
}
I want to call that mutation inside flux, get id of created session, store that id inside localStorage.
Relay.update(mutation, {onSuccess}) returns only {createSession:{clientMutationId:"0"}} without sid.
How to get created sid from flux?
Thanks for filing an issue! There's a couple steps to get this mutation working:
getConfigs should be configured to handle a FIELD_CHANGE - see the example at https://facebook.github.io/relay/docs/guides-mutations.html#a-complete-example... { session { sid } }. When the mutation is complete, the value of that data will be the new sid. Here's a rough example:
class LoginComponent extends React.Component {
...
createSession() {
Relay.update(new CreateSessionMutation(...), {
onSuccess: () => {
// after the mutation completes the `sid` will be populated
console.log(this.props.user.session.sid);
}
});
}
}
Relay.createContainer(LoginComponent, {
fragments: {
user: () => Relay.QL`
# query for session{sid} which is initially undefined
fragment on YourTypeWithSession {
session {
sid
}
}
`
}
});
If what you are adding is a completely new node in the system, where you have not had a chance to fetch any fields to yet, FIELDS_CHANGE might not see any fields that it needs to update. If that's the case, you can use REQUIRED_CHILDREN with a query to fetch any field you want on that might not have been fetched yet. Since no view needs this information, you can use the onSuccess callback with the mutation to do wtv you want with the payload.
@yuzhi Thank you! It works! But why REQUIRED_CHILDREN isn't documented?
Not sure, we'll update it!
Sorry, but I think this common issue have not resolved.
How to complete a /auth/signin action/mutation ?
This is a test:
class SignInMutation extends Relay.Mutation {
getMutation() {
return Relay.QL`mutation { signin }`;
}
getVariables() {
return {
username: this.props.username,
password: this.props.password,
}
}
getFatQuery() {
return Relay.QL`
fragment on SignInMutation {
sid
expired
}
`;
}
getConfigs() {
return [{
type: 'REQUIRED_CHILDREN',
children: [Relay.QL`
fragment on SignInMutation {
sid,
expired
}
`]
}];
}
}
In the server side, I use python graphene define:
type SignInMutation {
clientMutationId: String!
sid: String
expired: String
}
input SignInMutationInput {
clientMutationId: String!
username: String!
password: String!
}
this would send a query string to server:
mutation Signin($input_0:SignInMutationInput!){signin(input:$input_0){clientMutationId,...F0}} fragment F0 on SignInMutation{sid,expired}
if drop 'REQUIRED_CHILDREN' usage, the query string send to server:
mutation Signin($input_0:SignInMutationInput!){signin(input:$input_0){clientMutationId}}
Is there nice method to fetch columns like 'sid, expired' ?
any help would be appreciated,
Thanks
Is there nice method to fetch columns like 'sid, expired' ?
@gwind The canonical way to fetch fields on a new object such as this - where no fields could have been fetched yet - is to use REQUIRED_CHILDREN. It looks like those two fields are getting queried if you use that config - what part isn't working?
@josephsavona Thanks :smile:
Yes, no parts doesn't working. But my concerns is:
REQUIRED_CHILDREN is not pointed in the formal document - Mutator configuration and anywhere else.Does this config is not suggested? #236
addons field from mutation except REQUIRED_CHILDREN ?the query string is always like mutation Signin($input_0:SignInMutationInput!){signin(input:$input_0){clientMutationId}} by default, only clientMutationId field.
can we define getMutation like this in the future ?
javascript
getMutation() {
return Relay.QL`mutation { signin { sid, expired } }`;
}
thanks again
Reopening bc we should either document this approach or support an alternative.
I encountered #745 and started searching for ways to get REQUIRED_CHILDREN results into the store (currently these results are not added to the store, which makes sense鈥攈ow would Relay know to add them?). I suspect that #745 leads a fair number of people to REQUIRED_CHILDREN, with new nodes being the other use case?
+1 to documenting or finding another approach and documenting that. Just got very confused by this, and had to grep through the source+issues to find out what a REQUIRED_CHILDREN config signature looked like.
Most helpful comment
Sorry, but I think this common issue have not resolved.
How to complete a
/auth/signinaction/mutation ?This is a test:
In the server side, I use python graphene define:
this would send a query string to server:
if drop 'REQUIRED_CHILDREN' usage, the query string send to server:
Is there nice method to fetch columns like 'sid, expired' ?
any help would be appreciated,
Thanks