Example #1
0
        public Dictionary <__Type, GraphQLParser.SelectionSetContext> GetTypedSelectionSetContextFromUnionType(
            __Type unionType, GraphQLParser.SelectionSetContext ss)
        {
            Dictionary <__Type, GraphQLParser.SelectionSetContext> ret =
                new Dictionary <__Type, GraphQLParser.SelectionSetContext>();

            foreach (var s in ss.selection())
            {
                if (s.field() != null)
                {
                    Error($"Not expecting field under Union type - {unionType.name}", ss);
                }
                else if (s.fragmentSpread() != null)
                {
                    var fragmentSpreadName = s.fragmentSpread().fragmentName().GetText();

                    var fragment = GetFragments(x => x.fragmentName().GetText() == fragmentSpreadName).FirstOrDefault();
                    if (fragment == null)
                    {
                        continue;
                    }

                    var fragmentTypeName = fragment.typeCondition().typeName().GetText();

                    var fragmentType = schema.GetType(fragmentTypeName);

                    if (!DoesFragmentTypeApply(unionType, fragmentType, fragment))
                    {
                        continue;
                    }

                    if (fragmentType.kind == __TypeKind.UNION)
                    {
                        var z = GetTypedSelectionSetContextFromUnionType(fragmentType, fragment.selectionSet());
                        foreach (var c in z.Keys)
                        {
                            ret[c] = z[c];
                        }
                    }
                    else
                    {
                        ret[fragmentType] = fragment.selectionSet();
                    }
                }
                else if (s.inlineFragment() != null)
                {
                    var fragmentType = schema.GetType(s.inlineFragment().typeCondition().GetText());
                    if (fragmentType != null && !DoesFragmentTypeApply(unionType, fragmentType, s.inlineFragment()))
                    {
                        continue;
                    }
                    ret[fragmentType] = s.inlineFragment().selectionSet();
                }
            }
            return(ret);
        }
        public override object VisitSelectionSet(GraphQLParser.SelectionSetContext context)
        {
            var selections = new Selections();

            foreach (var selectionContext in context.selection())
            {
                var selection = Visit(selectionContext) as Selection;
                selections.Add(selection);
            }

            return(selections);
        }
Example #3
0
 /// <summary>
 /// Enter a parse tree produced by <see cref="GraphQLParser.selectionSet"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void EnterSelectionSet([NotNull] GraphQLParser.SelectionSetContext context)
 {
 }
Example #4
0
        void ValidateTypeAgainstSelectionSet(string typeName, GraphQLParser.SelectionSetContext selectionSet)
        {
            if (validatedSelectionSets.Contains(selectionSet) == false)
            {
                validatedSelectionSets.Add(selectionSet);
            }
            else
            {
                return;
            }

            var type = schema.GetType(typeName);

            if (type == null)
            {
                Error($"Type - {typeName} does not exist in schema", selectionSet);
            }


            foreach (var s in selectionSet.selection())
            {
                if (s.fragmentSpread() != null)
                {
                    var fragmentName = s.fragmentSpread().fragmentName().GetText();
                    var fragment     = GetFragments(x => x.fragmentName().GetText() == fragmentName).FirstOrDefault();
                    if (fragment == null)
                    {
                        Error($"Cannot find fragment name in document - {fragmentName}", s.fragmentSpread());
                    }
                    else
                    {
                        ValidateTypeAgainstSelectionSet(fragment.typeCondition().GetText(), fragment.selectionSet());
                    }
                }
                else if (s.inlineFragment() != null)
                {
                    // need to validate Interface here ....
                    var inlineType = s.inlineFragment().typeCondition().GetText();
                    if (type.kind == __TypeKind.INTERFACE || type.kind == __TypeKind.UNION)
                    {
                        if (type.possibleTypes.Exists(x => x.name == inlineType) == false)
                        {
                            Error($"Type - {inlineType} is not a possible target for interface {type.name} - possibleTypes are {type.possibleTypes.Aggregate("", (x, y) => x + "," + y.name)}", s);
                        }
                    }
                    ValidateTypeAgainstSelectionSet(s.inlineFragment().typeCondition().GetText(), s.inlineFragment().selectionSet());
                }
                else
                {
                    var fieldName = s.field().fieldName().GetText();
                    var field     = type.GetField((x) => x.name == fieldName);
                    if (field == null)
                    {
                        if (fieldName == "__schema")
                        {
                            field = schema.GetType("__SchemaContainer").GetField((x) => x.name == "__schema");
                        }
                        else
                        {
                            Error($"Field - {fieldName} does not exist in type - {type.name}", s);
                        }
                    }
                    if (s.field().arguments() != null)
                    {
                        ValidateArguments(field, s.field().arguments());
                    }

                    if (field.type.kind == __TypeKind.OBJECT || field.type.kind == __TypeKind.INTERFACE || field.type.kind == __TypeKind.UNION)
                    {
                        if (s.field().selectionSet() == null)
                        {
                            Error($"Received a field - {fieldName} - that is {field.type.kind} type - but query does not include selection set", s.field());
                        }

                        ValidateTypeAgainstSelectionSet(field.type.name, s.field().selectionSet());
                    }
                    else if (field.type.kind == __TypeKind.SCALAR)
                    {
                        if (s.field().selectionSet() != null)
                        {
                            Error($"Received a field - {fieldName} - that is SCALAR type - but query includes a selection set", s.field().selectionSet());
                        }
                    }
                    else if (field.type.kind == __TypeKind.ENUM)
                    {
                        if (s.field().selectionSet() != null)
                        {
                            Error($"Received a field - {fieldName} - that is ENUM type - but query includes a selection set", s.field().selectionSet());
                        }
                    }
                    else if (field.type.kind == __TypeKind.LIST)
                    {
                        if (field.type.ofType.kind != __TypeKind.SCALAR && field.type.ofType.kind != __TypeKind.ENUM)
                        {
                            if (s.field().selectionSet() == null)
                            {
                                Error(
                                    $"Received a field - {fieldName} - that is {field.type.kind} type with underlying type - {field.type.ofType.kind} - but query does not include selection set",
                                    s.field());
                            }

                            ValidateTypeAgainstSelectionSet(field.type.ofType.name, s.field().selectionSet());
                        }
                    }
                }
            }
        }
Example #5
0
 public ModifiableSelectionSet(GraphQLParser.SelectionSetContext sc)
 {
     selections.AddRange(sc.selection());
 }