Skip to content

Graphql not initialized in get_it #206

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

Closed
Aminul-Haque-Aome opened this issue Jun 26, 2021 · 4 comments
Closed

Graphql not initialized in get_it #206

Aminul-Haque-Aome opened this issue Jun 26, 2021 · 4 comments

Comments

@Aminul-Haque-Aome
Copy link

I want to implement GraphQL client in my flutter app. For Dependency injection, I use GetIt library. But when I run the app, it says

Invalid argument (Object of type HomeGraphQLService is not registered inside GetIt. Did you forget to pass an instance name? (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;)): HomeGraphQLService

Session.dart

abstract class Session {
  String getAccessToken();
}

SessionImpl.dart

class SessionImpl extends Session {
  SharedPreferences sharedPref;

  SessionImpl(SharedPreferences sharedPref) {
    this.sharedPref = sharedPref;
  }

  @override
  String getAccessToken() {
    return sharedPref.getString('access_token') ?? "";
  }

}

GraphQLClientGenerator.dart

class GraphQLClientGenerator {
  Session session;

  GraphQLClientGenerator(Session session) {
    this.session = session;
  }

  GraphQLClient getClient() {
    final HttpLink httpLink = HttpLink('https://xxx/graphql');
    final AuthLink authLink = AuthLink(getToken: () async => 'Bearer ${_getAccessToken()}');
    final Link link = authLink.concat(httpLink);

    return GraphQLClient(link: link, cache: GraphQLCache(store: InMemoryStore()));
  }

  String _getAccessToken() {
    return session.getAccessToken();
  }
}

ServiceLocator.dart

Future<void> initDependencies() async {
  await _initSharedPref();
  _initSession();
  _initGraphQLClient();
  _initGraphQLService();
}

Future<void> _initSharedPref() async {
  SharedPreferences sharedPref = await SharedPreferences.getInstance();
  serviceLocator.registerSingleton<SharedPreferences>(sharedPref);
}

void _initSession() {
  serviceLocator.registerLazySingleton<Session>(()=>SessionImpl(serviceLocator()));
}

void _initGraphQLClient() {
  serviceLocator.registerLazySingleton<GraphQLClient>(() => GraphQLClientGenerator(serviceLocator()).getClient());
}

void _initGraphQLService() {
  serviceLocator.registerLazySingleton<HomeGraphQLService>(() => HomeGraphQLService(serviceLocator()));
}

main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
  );
  
  await initDependencies();

  runApp(MyApp());
}
@escamoteur
Copy link
Collaborator

in which line does it throw the exception?

@Aminul-Haque-Aome
Copy link
Author

in which line does it throw the exception?

it said, HomeGraphQlService not registered inside Get_it. But I have already registered that. I really don't know, what the actual problem is

@Aminul-Haque-Aome
Copy link
Author

in which line does it throw the exception?

here is my HomeGraphQLService class

class HomeGraphQLService {
  GraphQLClient graphQLClient;

  HomeGraphQLService(GraphQLClient graphQLClient) {
    this.graphQLClient = graphQLClient;
  }

  Future<SubjectResponse> getAllCourseOf(String className, String groupName) async {
    try {
      final response = await graphQLClient.query(getAllCourseQuery(className, groupName));
      return SubjectResponse.fromJson((response.data));
    } on OperationException catch (exception) {
      return Future.error(exception.toString());
    }
  }
}
QueryOptions getAllCourseQuery(String className, String groupName) {
  String query = """
    query GetSubject($className: String, $groupName: String) {
      subjects(class: $className, group: $groupName) {
        code
        display
        insights {
          coming_soon
          purchased
        }
      }
    }
    """;

  return QueryOptions(
    document: gql(query),
    variables: <String, dynamic>{
      'className': className,
      'groupName': groupName,
    },
  );
}

@escamoteur
Copy link
Collaborator

And where do you try to access HomeGraphQlService ? that's what I meant with in which line in your code does it throw the exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants