简体   繁体   English

如何从GraphQL突变返回数据?

[英]How return data from a GraphQL mutation?

I have the following redux-form below... How can I get the console.log to obtain the user.id from the graphQL mutation result data? 我在下面有以下redux-form ...如何获取console.log以从graphQL突变结果数据中获取user.id?

const withForm = compose(
  withMySignupMutation,
  reduxForm({
    ...
    onSubmit: async (values, dispatch, {
      mySignup,
    }) => {
      try {
        const result = await mySignup(values);
        console.log('I Need user.id here');
        console.log(result.user.id);
        ...

withMySignupMutation withMySignupMutation

import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

export const SIGNUP_MUTATION = gql`
mutation (
  $email: String!
  $name: String!
) {
  signup(authProvider: {
    mySignup: {
      email: $email
      name: $name
    }
  }) {
    token
  }
}
`;

export default graphql(
  SIGNUP_MUTATION,
  {
    props: ({ mutate }) => ({
      mySignup: async (variables) => {
        const { data } = await mutate({ variables });
      },
    }),
  }
);

Thank you! 谢谢! happy holidays 节日快乐

In my opinion, it should be result.data.signup.id but we need to see the resolvers and your schema definition. 我认为应该是result.data.signup.id但是我们需要查看解析器和您的架构定义。 It depends on what the resolvers are sends back. 这取决于解析器返回的内容。 It should be a User type. 它应该是一个用户类型。

Anyway, you must ask for the id in your mutation otherwise it won't get resolved: 无论如何, 您必须在您的突变中要求输入id,否则它将无法解析:

export const SIGNUP_MUTATION = gql`
mutation (
  $email: String!
  $name: String!
) {
  signup(authProvider: {
    mySignup: {
      email: $email
      name: $name
    }
  }) {
    token
    id ### <===HERE===
  }
}
`

And if you have a graphiql access to the server: 并且,如果您具有对服务器的graphiql访问权限:

  • click on the top-right corner the DOCS button, then 单击DOCS按钮的右上角,然后
  • click mutation: Mutation 点击mutation: Mutation
  • then you should see something like: 那么您应该会看到类似以下内容的内容:

signup(authProvider: AuthProviderNewcomerData!): User

Then you know you will get a User back. 然后,您知道您将获得一个User

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM