private static void AddSerializerToInputField(
            ITypeCompletionContext completionContext,
            ArgumentDefinition definition,
            NameString typeName)
        {
            ITypeInspector typeInspector = completionContext.TypeInspector;
            IExtendedType? resultType;

            if (definition is InputFieldDefinition inputField)
            {
                resultType = typeInspector.GetReturnType(inputField.Property, true);
            }
            else if (definition.Parameter is not null)
            {
                resultType = typeInspector.GetArgumentType(definition.Parameter, true);
            }
            else if (definition.Type is ExtendedTypeReference typeReference)
            {
                resultType = typeReference.Type;
            }
            else
            {
                throw new SchemaException(SchemaErrorBuilder.New()
                                          .SetMessage("Unable to resolve type from field `{0}`.", definition.Name)
                                          .SetTypeSystemObject(completionContext.Type)
                                          .Build());
            }

            definition.Formatters.Add(CreateSerializer(completionContext, resultType, typeName));
        }
示例#2
0
        private static (string NodeTypeName, Type IdRuntimeType)? GetIdInfo(
            ITypeCompletionContext completionContext,
            ArgumentDefinition definition)
        {
            ITypeInspector typeInspector = completionContext.TypeInspector;
            IDAttribute?   idAttribute   = null;
            IExtendedType? idType        = null;

            if (definition is InputFieldDefinition inputField)
            {
                idAttribute = (IDAttribute?)inputField.Property
                              .GetCustomAttributes(inherit: true)
                              .SingleOrDefault(a => a is IDAttribute);
                if (idAttribute == null)
                {
                    return(null);
                }

                idType = typeInspector.GetReturnType(inputField.Property, true);
            }
            else if (definition.Parameter is not null)
            {
                idAttribute = (IDAttribute?)definition.Parameter
                              .GetCustomAttributes(inherit: true)
                              .SingleOrDefault(a => a is IDAttribute);
                if (idAttribute == null)
                {
                    return(null);
                }

                idType = typeInspector.GetArgumentType(definition.Parameter, true);
            }
            else if (definition.Type is ExtendedTypeReference typeReference)
            {
                if (typeReference.Type.Kind == ExtendedTypeKind.Schema)
                {
                    return(null);
                }
            }

            if (idAttribute is null || idType is null)
            {
                throw new SchemaException(SchemaErrorBuilder.New()
                                          .SetMessage("Unable to resolve type from field `{0}`.", definition.Name)
                                          .SetTypeSystemObject(completionContext.Type)
                                          .Build());
            }

            Type   idRuntimeType = idType.ElementType?.Source ?? idType.Source;
            string nodeTypeName  = idAttribute?.TypeName.HasValue ?? false
                ? idAttribute.TypeName
                : completionContext.Type.Name;

            return(nodeTypeName, idRuntimeType);
        }