如何从GraphQL变异返回数据?

时间:2017-12-25 16:52:28

标签: reactjs graphql apollo graphql-js react-apollo

我在下面有以下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

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 });
      },
    }),
  }
);

谢谢!节日快乐

1 个答案:

答案 0 :(得分:0)

在我看来,它应该是result.data.signup.id,但我们需要查看解析器和您的架构定义。这取决于解析器发回的内容。它应该是用户类型。

无论如何,你必须在变异中询问身份否则就无法解决:

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

如果你有对服务器的graphiql访问权限:

  • 点击DOCS按钮的右上角,然后点击
  • 点击mutation: Mutation
  • 然后你会看到类似的东西:

signup(authProvider: AuthProviderNewcomerData!): User

然后你知道你会得到一个User