public override object VisitField(GraphQLParser.FieldContext context)
        {
            var field = new Field();

            if (context.fieldName().alias() != null)
            {
                var aliasContext = context.fieldName().alias();
                field.Alias = aliasContext.NAME(0).GetText();
                field.Name  = aliasContext.NAME(1).GetText();
            }
            else
            {
                field.Name = context.fieldName().NAME().GetText();
            }

            if (context.arguments() != null)
            {
                field.Arguments = Visit(context.arguments()) as Arguments;
            }

            if (context.directives() != null)
            {
                field.Directives = Visit(context.directives()) as Directives;
            }

            if (context.selectionSet() != null)
            {
                field.Selections = Visit(context.selectionSet()) as Selections;
            }

            return(field);
        }
Exemplo n.º 2
0
 void CoerceDotNetValue(__Type fieldType, GraphQLParser.FieldContext field, Object value,
                        Context context = Context.Object)
 {
     try
     {
         if (value == null)
         {
             if (context == Context.List)
             {
                 output.AddScalarValue(null);
             }
             else
             {
                 output.AddScalarProperty(field.fieldName().GetText(), null);
             }
         }
         else if (fieldType.dotNetType == value.GetType() || (fieldType.dotNetType.IsEnum && value is String))
         {
             if (context == Context.List)
             {
                 output.AddScalarValue(value);
             }
             else
             {
                 output.AddScalarProperty(field.fieldName().GetText(), value);
             }
         }
         else
         {
             Error(
                 $"Error trying to coerce '{field.fieldName().GetText()}' of type '{value.GetType().Name}' to '{fieldType.kind}' with DotNetType: '{fieldType.dotNetType.Name}' ",
                 field);
         }
     }
     catch (Exception e)
     {
         Error(
             $"Error - '{e.Message}' trying to coerce '{field.fieldName().GetText()}' of type '{value.GetType().Name}' to '{fieldType.kind}' with DotNetType: '{fieldType.dotNetType.Name}' ",
             field);
     }
 }
Exemplo n.º 3
0
        public Dictionary <string, Object> CoerceArgumentValues(__Type objectType, GraphQLParser.FieldContext field,
                                                                Dictionary <string, Object> variableValues)
        {
            if (field.arguments() == null)
            {
                return(emptyArgs);
            }

            var coercedValues = new Dictionary <string, Object>();

            var argumentValues = field.arguments().argument();

            var fieldName = field.fieldName().GetText();

            var objectField = objectType.GetField((x) => x.name == fieldName);

            if (objectField == null)
            {
                Error(
                    $"Could not find field with name - {fieldName} in objecttype with name: {objectType.name} and underlying type:{objectType.dotNetType.Name}",
                    field);
            }

            foreach (var argumentDefinition in objectField.args)
            {
                var argumentName = argumentDefinition.name;
                var argumentType = argumentDefinition.type;
                var defaultValue = argumentDefinition.defaultValue;
                var value        =
                    argumentValues.Where(x => x.NAME().GetText() == argumentName)
                    .Select(x => x.valueOrVariable())
                    .FirstOrDefault();
                if (value?.variable() != null)
                {
                    var    variableName  = value.variable().NAME().GetText();
                    Object variableValue = null;
                    if (variableValues.ContainsKey(variableName))
                    {
                        variableValue = variableValues[variableName];
                    }

                    if (variableValue != null)
                    {
                        coercedValues[argumentName] = variableValue;
                    }
                    else if (defaultValue != null)
                    {
                        coercedValues[argumentName] = defaultValue;
                    }
                    else if (argumentType.kind == __TypeKind.NON_NULL)
                    {
                        Error(
                            $"Required field '{argumentName}' refered by variable '{variableName}' is not present in document",
                            field);
                    }
                    else
                    {
                        continue;
                    }
                }
                else if (value?.value() == null)
                {
                    if (defaultValue != null)
                    {
                        coercedValues[argumentName] = defaultValue;
                    }
                    else if (argumentType.kind == __TypeKind.NON_NULL)
                    {
                        Error($"Required field '{argumentName}' is not present in document", field);
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    coercedValues[argumentName] = CoerceDocumentValue(argumentType, argumentName, value.value());
                }
            }
            return(coercedValues);
        }
Exemplo n.º 4
0
        void CompleteValue(__Type fieldType, GraphQLParser.FieldContext field, List <GraphQLParser.FieldContext> fields,
                           Object result,
                           Dictionary <string, Object> variableValues, bool assertNotNull = false, Context context = Context.Object)
        {
            if (fieldType.kind == __TypeKind.NON_NULL)
            {
                var innerType = fieldType.ofType;
                CompleteValue(innerType, field, fields, result, variableValues, true);
            }
            else if (result == null && assertNotNull)
            {
                Error($"Null or empty value was found on non null field", field);
            }
            else if (fieldType.kind == __TypeKind.LIST)
            {
                if (result != null && TypeCheck.IsEnumerableType(result.GetType()) == false)
                {
                    Error($"Did not find list type for {field.fieldName().GetText()} - found {result.GetType().Name}",
                          field);
                }
                var innerType = fieldType.ofType;

                output.PushArray(field.fieldName().GetText());
                if (result != null)
                {
                    foreach (var c in (IEnumerable)result)
                    {
                        if (field.fieldName().GetText() == "types" && IsSchemaQuery && c is __Type &&
                            ((__Type)c).name.StartsWith("__"))
                        {
                            continue;
                        }
                        CompleteValue(innerType, field, fields, c, variableValues, false, Context.List);
                    }
                }
                output.Pop();
            }
            else if (fieldType.kind == __TypeKind.SCALAR || fieldType.kind == __TypeKind.ENUM)
            {
                CoerceDotNetValue(fieldType, field, result, context);
            }
            else if (fieldType.kind == __TypeKind.OBJECT || fieldType.kind == __TypeKind.UNION ||
                     fieldType.kind == __TypeKind.INTERFACE)
            {
                if (fieldType.kind == __TypeKind.OBJECT || fieldType.kind == __TypeKind.UNION)
                {
                    var objectType      = fieldType;
                    var subSelectionSet = MergeSelectionSets(fields);

                    if (context == Context.Object)
                    {
                        if (result == null)
                        {
                            output.AddScalarProperty(field.fieldName().GetText(), null);
                        }
                        else
                        {
                            output.PushObject(field.fieldName().GetText());
                            ExecuteSelectionSet(subSelectionSet, objectType, result, variableValues);
                        }
                    }
                    else if (context == Context.List)
                    {
                        if (result != null)
                        {
                            output.PushObject();
                            ExecuteSelectionSet(subSelectionSet, objectType, result, variableValues);
                        }
                    }

                    if (result != null)
                    {
                        output.Pop();
                    }
                }
                else
                {
                    Error($"Interface and Union not yet supported", field);
                }
            }
            else
            {
                Error($"Unexpected fieldType - {fieldType.kind}", field);
            }
        }