尝试向本地GQL端点发出请求时出现Perculiar错误

时间:2019-09-11 13:48:27

标签: android-emulator graphql react-native-android

我试图在react-native中设置一个android应用程序,它正在调用本地API。 理想情况下,我希望看到API请求实际上成功,或者至少返回一个格式化的GraphQL异常,该异常应在后端生成。

我的GraphQL api在localhost:3000上运行

我已经为遇到的任何问题尝试了通用解决方案

  1. 将Android模拟器HTTP代理设置为10.0.2.2:3000

我已经像这样设置了ApolloClient

const client = new ApolloClient({
    link: new HttpLink({
        uri: Platform.select({
            android: 'http://"my machine ip here" / ipv4 /graphql'
        })
    }),
    cache: new InMemoryCache(),
});

我也尝试过

const client = new ApolloClient({
    link: new HttpLink({
        uri: Platform.select({
            android: 'http://10.0.2.2:3000/graphql'
        })
    }),
    cache: new InMemoryCache(),
});

在此处发出API请求:

    const [login, {data, loading, error}] = useMutation(LoginUserMutation);

    return <LoginWrapper>
        <Formik
            initialValues={{email: '', password: ''}}
            onSubmit={async ({email, password}) => {
                const user = await login({variables: {email, password}});

            }}
        >
            {({
                  values,
                  handleChange,
                  errors,
                  setFieldTouched,
                  touched,
                  isValid,
                  handleSubmit
              }) => <>
                <Input
                    value={values.email}
                    onChangeText={handleChange('email')}
                    onBlur={() => setFieldTouched('email')}
                    placeholder='E-mail'
                />
                <Input
                    value={values.password}
                    onChangeText={handleChange('password')}
                    placeholder='Password'
                    onBlur={() => setFieldTouched('password')}
                    secureTextEntry={true}
                />
                <Button
                    onClick={handleSubmit}
                    text='Sign In'
                />
            </>}
        </Formik>
    </LoginWrapper>
};

在这里可以看到graphql突变

export const LoginUserMutation =
    gql(
        mutation('loginUser',
            params(
                {
                    $args: 'LoginUserInput!'
                },
                {
                    updateUser: params({args: '$args'},
                        {
                            email: types.string,
                        }
                    ),

                })
        )
    );

可以在此处找到错误图片- https://imgur.com/a/6odLPnU

此处有部分堆栈跟踪-

可能的未处理的承诺拒绝(id:0): 错误:网络错误:响应失败:收到状态码400 ApolloError @ blob:http://localhost:8081/86efb950-3eff-404f-bc63-41535a310e3a:92518:28 错误@blob:http://localhost:8081/86efb950-3eff-404f-bc63-41535a310e3a:93755:30 notifySubscription @ blob:http://localhost:8081/86efb950-3eff-404f-bc63-41535a310e3a:141965:20 onNotify @ blob:http://localhost:8081/86efb950-3eff-404f-bc63-41535a310e3a:142004:23

1 个答案:

答案 0 :(得分:0)

所以我解决了这个问题。

突变应该是

export const LoginUserMutation = gql(
    mutation('login',
        params({$args: 'LoginUserInput!'}, {
            login: params({args: '$args'}, {
                token: types.string,
            }),
        })
    )
);

突变的呼唤应该是

    const [login, {data, loading, error}] = useMutation(LoginUserMutation);

    await login({variables: {args: {email, password}}})