简体   繁体   English

使用 graphql_flutter 连接到 Graphql API 时出错

[英]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.

相关问题 如何在 graphql_flutter 中重试对 GraphQLError 的请求 - How to retry a request on GraphQLError in graphql_flutter Websocket 重连循环,graphql_flutter - Websocket reconnection loop, graphql_flutter graphql_flutter - 从 pub.dev 页面复制代码 - 仍然出错 - graphql_flutter - copied code from the pub.dev page - still getting error Flutter with Firebase JWT 将 GraphQL (graphql_flutter) 请求发送到具有“格式错误的授权标头”的 Heroku Hasura - Flutter with Firebase JWT sends GraphQL (graphql_flutter) request to Heroku Hasura that has a “Malformed Authorization header” Flutter (graphql_flutter + gql):找到这个候选,但参数不匹配 - Flutter (graphql_flutter + gql): Found this candidate, but the arguments don't match graphql_flutter 返回 LazyCacheMap,built_value deserializeWith JSON String,如何让它们协同工作 - graphql_flutter return LazyCacheMap, built_value deserializeWith JSON String, how to make them work together 在操场上运行的 Graphql_flutter 突变在运行设备/模拟器上不起作用 - Graphql_flutter mutation running on playground doesn't work on running device/emulator 如何在每次构建Query()时禁用graphql_flutter查询后端 - How to disable graphql_flutter query backend every time build Query() graphql_flutter 突变查询需要返回语句 - 不确定如何添加它 - graphql_flutter mutation query expects return statement - not sure how to add it 在 GraphQL Flutter 中使用变量时出错 - Getting error while using variable in GraphQL Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM