Ejemplo n.º 1
0
        public override object VisitField(GraphQLParser.FieldContext context)
        {
            var field = new Field();

            NewNode(field, context);

            if (context.NAME() != null)
            {
                field.Name = context.NAME().GetText();
            }

            if (context.alias() != null)
            {
                field.Alias = context.alias().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.SelectionSet = Visit(context.selectionSet()) as SelectionSet;
            }

            return(field);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        void ExecuteField(GraphQLParser.FieldContext parentField, __Type objectType, Object objectValue,
                          __Type fieldType,
                          List <GraphQLParser.FieldContext> fields,
                          Dictionary <string, Object> variableValues,
                          Dictionary <string, GraphQLParser.FragmentDefinitionContext> visitedFragments)
        {
            var field = fields.First();

            var argumentValues = CoerceArgumentValues(objectType, field, variableValues);

            var resolvedValue =
                ResolveFieldValue(objectType, objectValue, field.fieldName().GetText(), argumentValues, field);

            CompleteValue(fieldType, field, fields, resolvedValue, variableValues);
        }
Ejemplo n.º 4
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);
     }
 }
Ejemplo n.º 5
0
        Object ResolveFieldValue(__Type objectType, Object objectValue, String fieldName,
                                 Dictionary <string, Object> argumentValues, GraphQLParser.FieldContext field)
        {
            if (objectValue == null)
            {
                return(null);
            }

            if (fieldName == "__typename")
            {
                return(objectType.name);
            }

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

            if (fieldObj == null)
            {
                Error($"Could not resolve property type - {fieldName} in context of {objectType.name}", field);
            }

            if (fieldObj.FieldType == __Field.FieldTypeEnum.Property && argumentValues.Any())
            {
                Error($"Found a property type - {fieldName} - but are presented with set of argumentValues, indicating it should be a method type ", field);
            }

            if (fieldObj.ResolveProperty == null && fieldObj.ResolveMethod == null)
            {
                Error($"Found a property type - {fieldName} from {objectType.name} which has no method or property resolver", field);
            }

            if (fieldObj.ResolveProperty != null)
            {
                return(fieldObj.ResolveProperty(objectValue));
            }
            else
            {
                return(fieldObj.ResolveMethod(objectValue, argumentValues));
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
0
 bool includeDirective(GraphQLParser.FieldContext fc)
 {
     // TODO: process include directive ..
     return(true);
 }
Ejemplo n.º 9
0
 bool skipDirective(GraphQLParser.FieldContext fc)
 {
     // TODO: process skip directive ...
     return(false);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="GraphQLParser.field"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitField([NotNull] GraphQLParser.FieldContext context)
 {
 }