Beispiel #1
0
        private static void RenameObjectField(
            ObjectTypeDefinitionNode objectType,
            RenameFieldsContext renameContext)
        {
            IReadOnlyCollection <InterfaceTypeDefinitionNode> interfaceTypes =
                GetInterfaceTypes(objectType, renameContext.Types);

            foreach (FieldDefinitionNode fieldDefinition in
                     objectType.Fields)
            {
                NameString originalName =
                    fieldDefinition.GetOriginalName(renameContext.SchemaName);
                if (!originalName.Equals(fieldDefinition.Name.Value))
                {
                    foreach (InterfaceTypeDefinitionNode interfaceType in
                             GetInterfacesThatProvideFieldDefinition(
                                 originalName, interfaceTypes))
                    {
                        RenameInterfaceField(interfaceType,
                                             renameContext, originalName,
                                             fieldDefinition.Name.Value);
                    }
                }
            }
        }
        private static RuntimeTypeDirective?GetDirectiveValue(
            HotChocolate.Language.IHasDirectives hasDirectives,
            NameString directiveName)
        {
            DirectiveNode?directive = hasDirectives.Directives.FirstOrDefault(
                t => directiveName.Equals(t.Name.Value));

            if (directive is { Arguments : { Count : > 0 } })
 public static bool TryGetSourceDirective(
     this IHasDirectives hasDirectives,
     NameString schemaName,
     out SourceDirective sourceDirective)
 {
     sourceDirective = hasDirectives.Directives[DirectiveNames.Source]
                       .Select(t => t.ToObject <SourceDirective>())
                       .FirstOrDefault(t => schemaName.Equals(t.Schema));
     return(sourceDirective != null);
 }
Beispiel #4
0
        public int IndexOfField(NameString fieldName)
        {
            for (var i = 0; i < _fields.Length; i++)
            {
                if (fieldName.Equals(_fields[i].Name))
                {
                    return(i);
                }
            }

            return(-1);
        }
 private static bool HasSourceDirective(
     DirectiveNode directive,
     NameString schemaName)
 {
     if (DirectiveNames.Source.Equals(directive.Name.Value))
     {
         ArgumentNode argument = directive.Arguments.FirstOrDefault(t =>
                                                                    DirectiveFieldNames.Source_Schema.Equals(t.Name.Value));
         return(argument != null &&
                argument.Value is StringValueNode sv &&
                schemaName.Equals(sv.Value));
     }
     return(false);
 }
Beispiel #6
0
        private static void AddNewFieldName(
            ComplexTypeDefinitionNodeBase type,
            RenameFieldsContext renameContext,
            NameString originalFieldName,
            NameString newFieldName)
        {
            FieldDefinitionNode fieldDefinition = type.Fields.FirstOrDefault(
                t => originalFieldName.Equals(t.GetOriginalName(
                                                  renameContext.SchemaName)));

            if (fieldDefinition != null)
            {
                renameContext.RenamedFields[fieldDefinition] = newFieldName;
            }
        }
Beispiel #7
0
 private static void RenameInterfaceField(
     InterfaceTypeDefinitionNode interfaceType,
     RenameFieldsContext renameContext)
 {
     foreach (FieldDefinitionNode fieldDefinition in
              interfaceType.Fields)
     {
         NameString originalName =
             fieldDefinition.GetOriginalName(renameContext.SchemaName);
         if (!originalName.Equals(fieldDefinition.Name.Value))
         {
             RenameInterfaceField(
                 interfaceType, renameContext,
                 originalName, fieldDefinition.Name.Value);
         }
     }
 }
Beispiel #8
0
        private static Dictionary <NameString, NameString> GetRenamedTypes(
            DocumentNode document,
            NameString schemaName)
        {
            var names = new Dictionary <NameString, NameString>();

            foreach (NamedSyntaxNode type in document.Definitions
                     .OfType <NamedSyntaxNode>())
            {
                NameString originalName = type.GetOriginalName(schemaName);
                if (!originalName.Equals(type.Name.Value))
                {
                    names[originalName] = type.Name.Value;
                }
            }

            return(names);
        }
Beispiel #9
0
        GetInterfacesThatProvideFieldDefinition(
            NameString originalFieldName,
            IEnumerable <InterfaceTypeDefinitionNode> interfaceTypes)
        {
            var items = new List <InterfaceTypeDefinitionNode>();

            foreach (InterfaceTypeDefinitionNode interfaceType in
                     interfaceTypes)
            {
                if (interfaceType.Fields.Any(t =>
                                             originalFieldName.Equals(t.Name.Value)))
                {
                    items.Add(interfaceType);
                }
            }

            return(items);
        }
Beispiel #10
0
    protected MiddlewareBase(
        RequestDelegate next,
        IRequestExecutorResolver executorResolver,
        IHttpResultSerializer resultSerializer,
        NameString schemaName)
    {
        if (executorResolver == null)
        {
            throw new ArgumentNullException(nameof(executorResolver));
        }

        _next = next ??
                throw new ArgumentNullException(nameof(next));
        _resultSerializer = resultSerializer ??
                            throw new ArgumentNullException(nameof(resultSerializer));
        SchemaName      = schemaName;
        IsDefaultSchema = SchemaName.Equals(Schema.DefaultName);
        ExecutorProxy   = new RequestExecutorProxy(executorResolver, schemaName);
    }
Beispiel #11
0
        public object?OnAfterDeserialize(object?runtimeValue)
        {
            if (runtimeValue is null)
            {
                return(null);
            }

            if (runtimeValue is IdValue id &&
                (!_validateType || _typeName.Equals(id.TypeName)))
            {
                return(id.Value);
            }

            if (runtimeValue is string s)
            {
                try
                {
                    id = _idSerializer.Deserialize(s);

                    if (!_validateType || _typeName.Equals(id.TypeName))
                    {
                        return(id.Value);
                    }
                }
                catch
                {
                    throw new GraphQLException(
                              ErrorBuilder.New()
                              .SetMessage("The ID `{0}` has an invalid format.", s)
                              .Build());
                }

                throw new GraphQLException(
                          ErrorBuilder.New()
                          .SetMessage("The ID `{0}` is not an ID of `{1}`.", s, _typeName)
                          .Build());
            }

            if (runtimeValue is IEnumerable <IdValue> idEnumerable)
            {
                IList list = _createList();

                foreach (IdValue idv in idEnumerable)
                {
                    if (!_validateType || _typeName.Equals(idv.TypeName))
                    {
                        list.Add(idv.Value);
                    }
                }

                return(list);
            }

            if (runtimeValue is IEnumerable <string> stringEnumerable)
            {
                try
                {
                    IList list = _createList();

                    foreach (string sv in stringEnumerable)
                    {
                        id = _idSerializer.Deserialize(sv);

                        if (!_validateType || _typeName.Equals(id.TypeName))
                        {
                            list.Add(id.Value);
                        }
                    }

                    return(list);
                }
                catch
                {
                    throw new GraphQLException(
                              ErrorBuilder.New()
                              .SetMessage(
                                  "The IDs `{0}` have an invalid format.",
                                  string.Join(", ", stringEnumerable))
                              .Build());
                }
            }

            throw new GraphQLException(
                      ErrorBuilder.New()
                      .SetMessage("The specified value is not a valid ID value.")
                      .Build());
        }
 private static bool IsTypeNameField(NameString fieldName)
 {
     return(fieldName.Equals(IntrospectionFields.TypeName));
 }