Android emulator 尝试向本地GQL端点发出请求时出现Perculiar错误
我正试图在react native中设置一个android应用程序,它正在调用一个本地API。 理想情况下,我希望看到API请求实际成功,或者至少返回一个应在后端生成的格式化GraphQL异常 我的GraphQLAPI正在本地主机上运行:3000 对于我遇到的任何问题,我已经尝试过通用解决方案Android emulator 尝试向本地GQL端点发出请求时出现Perculiar错误,android-emulator,graphql,react-native-android,Android Emulator,Graphql,React Native Android,我正试图在react native中设置一个android应用程序,它正在调用一个本地API。 理想情况下,我希望看到API请求实际成功,或者至少返回一个应在后端生成的格式化GraphQL异常 我的GraphQLAPI正在本地主机上运行:3000 对于我遇到的任何问题,我已经尝试过通用解决方案 将android emulator HTTP代理设置为10.0.2.2:3000 我已经这样设置了Apollo客户 const client = new ApolloClient({ link:
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>
};
错误图像可在此处找到-
此处为部分堆栈跟踪-
可能的未处理承诺拒绝(id:0):
错误:网络错误:响应不成功:收到状态代码400
ApolloError@blob:
error@blob:
notifySubscription@blob:
onNotify@blob:所以我解决了这个问题 突变应该是
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}}})
400错误表示您发送的查询格式不正确或验证失败。检查从服务器收到的实际响应以了解问题所在。如果你不知道问题出在哪里,你也可以在这里用你发送的查询更新你的问题。嘿,谢谢你的回复。我已经在我的服务器上启用了日志记录,没有请求通过我刚刚用GQL变体对其进行了更新,这取决于您记录请求的位置,如果请求的格式不正确,您不必记录任何内容。这是真的。它和拦截器一样处于解析器级别,所以很可能是。但是我也没有看到它在网络标签中被触发。。。我不完全确定你是否能看到来自react native的流量。我将向控制器设置一个简单的fetch请求&看看它是否以其他方式返回任何东西,目前还不清楚代码片段中的
mutation
和params
函数的作用。查看传递给gql
的实际字符串会提供更多信息。
const [login, {data, loading, error}] = useMutation(LoginUserMutation);
await login({variables: {args: {email, password}}})