Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take operation name from document string when QueryOptions.operationName is not specified #1476

Open
jv-soares opened this issue Jan 10, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@jv-soares
Copy link

I think its bad that we have to specify the QueryOptions.operationName even when the operation name is part of the query string:

final document = gql(r'''
query Example() {
  id
}
''');
final options = QueryOptions(document: document);
print(options.operationName) // prints null

Also if the names in QueryOptions.operationName and in the query string don't exactly match the request simply doesn't work and I don't get any exception.

Solution

I've solved this problem on my side by implementing an extension of DocumentNode from gql and using it in QueryOptions

import 'package:gql/ast.dart' as ast;

extension DocumentNodeExtension on ast.DocumentNode {
  String? get operationName {
    final node = definitions.first as ast.OperationDefinitionNode;
    return node.name?.value;
  }
}

final document = gql(r'''
query Example() {
  id
}
''');
final options = QueryOptions(operationName: document.operationName, document: document);
print(options.operationName) // prints 'Example'

I thought maybe the operation name could be extracted like this by default when QueryOptions.operationName is not specified.

Is this reasonable or am I missing something here?

@jv-soares jv-soares added the enhancement New feature or request label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant