예제 #1
0
파일: GraphQLQuery.cs 프로젝트: hfmm99/HCM
        public async Task <List <T> > RunAsync(CancellationToken cancellationToken = default)
        {
            //Get GraphQL Query Name
            var graphQlField = QueryMethod.GetCustomAttribute <GraphQLFieldAttribute>();

            _queryName = graphQlField != null && !string.IsNullOrEmpty(graphQlField.FieldName) ? graphQlField.FieldName : QueryMethod.Name;

            //Get GraphQL Query Arguments
            var parameters = QueryMethod.GetParameters();
            var arguments  = new List <GraphQLRequestArgument>();
            var fields     = new List <GraphQLRequestFieldBase>();

            var items = Arguments.Zip(parameters, (arg, param) => new
            {
                Argument  = arg,
                Parameter = param
            }).ToArray();

            foreach (var item in items)
            {
                if (item.Argument != null && !item.Parameter.Name.Equals("sessionToken", StringComparison.InvariantCulture))
                {
                    arguments.Add(new GraphQLRequestArgument(item.Parameter.Name, item.Argument));
                }
            }

            var setPartnerId = items.FirstOrDefault(i => i.Parameter.Name.Equals("partnerId", StringComparison.InvariantCulture));
            var setSessionId = items.FirstOrDefault(i => i.Parameter.Name.Equals("sessionToken", StringComparison.InvariantCulture));

            string path = $"{ _graphQlClientService.EndPoint.Scheme }{ Uri.SchemeDelimiter}{ _graphQlClientService.EndPoint.Authority }{ _graphQlClientService.EndPoint.AbsolutePath }";

            _graphQlClientService.EndPoint = new Uri(path);

            //Get GraphQL Query Fields
            T defaultObject = DefaultGraphQL <T>();

            if (FieldsSelector != null)
            {
                var emptyObject = FieldsSelector(defaultObject);

                if (emptyObject != null)
                {
                    var attributes = emptyObject.GetType().GetProperties();

                    foreach (PropertyInfo attribute in attributes)
                    {
                        if (attribute.GetValue(emptyObject) == null)
                        {
                            continue;
                        }

                        fields.Add(new GraphQLRequestBasicField(attribute.Name));

                        if (attribute.PropertyType.IsGenericType && attribute.PropertyType.GetGenericTypeDefinition()
                            .GetInterface("IEnumerable") != null)
                        {
                            AddListObjects(fields, emptyObject, attribute);
                        }
                        else if (!attribute.PropertyType.IsSealed && attribute.PropertyType.IsClass)
                        {
                            AddComplexObjectsFromProp(fields, attribute, emptyObject);
                        }
                    }
                }
            }

            //Build GraphQL Request
            _graphQlRequest = new GraphQLRequest(_queryName, arguments.ToArray(), fields.ToArray());

            //Make HTTP call
            var response = _isMutation
                        ? (_isIEnumerableResult
                           ? await _graphQlClientService.PostMutationAsync(_graphQlRequest, executeOptionalFunc: GetGenericListMethod)
                           : await _graphQlClientService.PostMutationAsync(_graphQlRequest, executeOptionalFunc: GetGenericSingleObjMethod))
                        : (_isIEnumerableResult
                           ? await _graphQlClientService.PostQueryAsync(_graphQlRequest, executeOptionalFunc: GetGenericListMethod)
                           : await _graphQlClientService.PostQueryAsync(_graphQlRequest, executeOptionalFunc: GetGenericSingleObjMethod));

            //Return results
            return(response);
        }