[英]Error when connecting to Graphql API using graphql_flutter
I am trying to use graphql_flutter for my flutter app but cannot establish connection and I'm getting the following error:我正在尝试将 graphql_flutter 用于我的 flutter 应用程序,但无法建立连接,并且出现以下错误:
I/flutter ( 6283): OperationException(linkException: ServerException(originalException: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 36582, parsedResponse: null), graphqlErrors: [])
My guess is the error says the port is 36582 but I have my server on localhost at port 4000.我的猜测是错误说端口是 36582,但我的服务器在 localhost 的端口 4000 上。
This is the code for the main app这是主应用程序的代码
void main() async {
await initHiveForFlutter();
final HttpLink httpLink = HttpLink('http://localhost:4000/');
final AuthLink authLink = AuthLink(
getToken: () async => 'Bearer <YOUR_PERSONAL_ACCESS_TOKEN>',
// OR
// getToken: () => 'Bearer <YOUR_PERSONAL_ACCESS_TOKEN>',
);
final Link link = authLink.concat(httpLink);
ValueNotifier<GraphQLClient> client = ValueNotifier(
GraphQLClient(
link: link,
cache: GraphQLCache(
store: HiveStore(),
),
),
);
runApp(MyApp(client));
}
class MyApp extends StatelessWidget {
final ValueNotifier<GraphQLClient> client;
MyApp(this.client);
@override
Widget build(BuildContext context) {
return GraphQLProvider(
client: client,
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
Also the address is correct and does not include "/graphql" at the end.地址也是正确的,最后不包括“/graphql”。 The queries work fine in postman and also in apollo-client on my angular app.查询在 postman 和我的 angular 应用程序的 apollo-client 中工作正常。 Any suggestions would be appreciated.任何建议,将不胜感激。
And This is the query widget which gives the error这是给出错误的查询小部件
Query(
options: QueryOptions(
document: gql(ActionData.fetchCalendarActions),
variables: {
'userId': "60157b3dd6882132841d9a25",
'currentList': 'calendar'
}),
builder: (QueryResult result,
{VoidCallback refetch, FetchMore fetchMore}) {
if (result.hasException) {
print(result.exception.toString());
return Text(result.exception.toString());
}
if (result.isLoading) {
return Text('Loading');
}
print(result);
return Text('result');
},
),
and this is the query:这是查询:
class ActionData {
static String fetchCalendarActions =
"""query getCalendarActions(\$userId: ID!, \$currentList: String!) {
getActionForList(userId: \$userId, currentList: \$currentList ) {
_id
title
tags
}
}""";
}
The app seems unable to connect to the localhost server that has been set.该应用似乎无法连接到已设置的 localhost 服务器。 If you're running into this issue with your Flutter app running on an Android emulator trying to connect to your localhost server, you need to configure the IP address of the host machine - which is 10.0.2.2
.如果您在 Android 模拟器上运行 Flutter 应用程序时遇到此问题,尝试连接到您的本地主机服务器,您需要配置10.0.2.2
主机的地址。 The reason for this config is discussed in more detail on this post .这个配置的原因在这篇文章中有更详细的讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.