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

Perculiar error when trying to make request to local GQL endpoint

我正在尝试在 react-native 中设置一个 android 应用程序,它正在调用本地 API。 理想情况下,我希望看到 API 请求实际上成功,或者至少 return 我应该在我的后端生成一个格式化的 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 通知订阅@blob:http://localhost:8081/86efb950-3eff-404f-bc63-41535a310e3a:141965:20 onNotify@blob:http://localhost:8081/86efb950-3eff-404f-bc63-41535a310e3a:142004:23

所以我已经解决了这个问题。

突变应该是

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