resolverFunctionFromElement function

Future<String> resolverFunctionFromElement(
  1. GeneratorCtx ctx,
  2. ExecutableElement element
)

Implementation

Future<String> resolverFunctionFromElement(
  GeneratorCtx ctx,
  ExecutableElement element,
) async {
  final hasSubsAnnot =
      const TypeChecker.fromRuntime(Subscription).hasAnnotationOfExact(element);
  final isStream = isStreamOrAsyncStream(element.returnType);

  if (hasSubsAnnot && !isStream || isStream && !hasSubsAnnot) {
    throw Exception('$element should return a Stream to be a Subscription.');
  }

  final body = await resolverFunctionBodyFromElement(ctx, element);
  return '''
${isStream ? 'subscribe' : 'resolve'}: (obj, ctx) {
  $body
}
''';
}