0

I am trying to use GraphQL in Flutter with Hasura. This time, I am using Ferry Flutter but currently my response just returns null even though the endpoint is correct and I can access the data within the hasura/console.

When I simply curl or web access, it returns {"path":"$","error":"resource does not exist","code":"not-found"}

First here is my client initialization

Client initClient(String url) {
  final link = HttpLink(url);

  final client = Client(
    link: link,
  );
  return client;
}

Next, I assign my endpoint in the main.dart

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  GetIt.instance.registerSingleton<Client>(
    initClient('http://localhost:8081/v1/graphql'),
  );
  runApp(App());
}

And here is my docker-compose.yaml

version: "3.6"
services:
  postgres:
    image: postgres:12
    restart: always
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: pass
  graphql-engine:
    image: hasura/graphql-engine:v2.0.3
    ports:
      - "8081:8080"
    depends_on:
      - "postgres"
    restart: always
    environment:
      HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:pass@postgres:5432/postgres
      ## enable the console served by server
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
      ## enable debugging mode. It is recommended to disable this in production
      HASURA_GRAPHQL_DEV_MODE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log

      ## uncomment next line to set an admin secret
      # HASURA_GRAPHQL_ADMIN_SECRET: shouldISetSomething?
volumes:
  db_data:

Finally, I tried to define simple StatelessWidget with Ferry Flutter as below.

class ToDoScreen extends StatelessWidget {
  ToDoScreen({Key? key}) : super(key: key);

  final req = GFetchToDoListReq();

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: CustomScrollView(
        slivers: [
          CupertinoSliverNavigationBar(
            largeTitle: Text('TODO'),
            trailing: CupertinoButton(
              padding: const EdgeInsets.all(5),
              onPressed: () => Navigator.pushNamed(context, '/new'),
              child: Icon(
                CupertinoIcons.plus,
                size: 30.0,
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate(
              [
                Operation<GFetchToDoData, GFetchToDoListVars>(
                  client: GetIt.instance<Client>(),
                  operationRequest: req,
                  builder: (BuildContext context,
                      OperationResponse<GFetchToDoData?,
                              GFetchToDoListVars?>?
                          response,
                      error) {
                    if (response!.loading) {
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                 // HERE, todos are null so I can't pass null operation.

                    final todos = response.data?.recipes;

                    if (recipes!.isEmpty) {
                      return _buildNoRecipes(context);
                    }

                    return Column(
                      children: [
                       // some codes
                          ),
                      ],
                    );
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

I am assuming I have to add admin secret in the client header but I am not sure if it is correct, and also not sure how to define admin secret or whatever I need to access to the endpoint correctly.

husky
  • 831
  • 3
  • 10
  • 24

1 Answers1

0

You can add your admin secret to the HttpLink using defaultHeaders parameter.

final link = HttpLink('your-graphql-link',
  defaultHeaders: {
    'content-type': 'application/json',
    'x-hasura-admin-secret':
        'your-hasura-admin-secret',
  });
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jake
  • 1
  • 1