getDirectiveValue function

Object? getDirectiveValue(
  1. String name,
  2. String argumentName,
  3. List<DirectiveNode> directives,
  4. Map<String, Object?>? variableValues, {
  5. Map<String, GraphQLDirective> directivesMap = const {},
})

Implementation

Object? getDirectiveValue(
  String name,
  String argumentName,
  List<DirectiveNode> directives,
  Map<String, Object?>? variableValues, {
  Map<String, GraphQLDirective> directivesMap = const {},
}) {
  final directive = directives.firstWhereOrNull(
    (d) => d.name.value == name,
  );
  if (directive == null) return null;

  final argument = directive.arguments.firstWhereOrNull(
    (arg) => arg.name.value == argumentName,
  );
  if (argument == null) {
    final arg = directivesMap[name]?.inputs.firstWhereOrNull(
          (arg) => arg.name == argumentName,
        );
    return arg?.defaultValue;
  }

  final value = argument.value;
  if (value is VariableNode) {
    final variableName = value.name.value;
    if (variableValues == null || !variableValues.containsKey(variableName)) {
      // TODO: 2I this probably should not be here?
      throw GraphQLException.fromMessage(
        'Unknown variable: "$variableName"',
        location: (value.span ??
                value.name.span ??
                argument.span ??
                argument.name.span)
            ?.start,
      );
    }
    return variableValues[variableName];
  }
  return computeValue(null, value, variableValues);
}