예제 #1
0
        private void MutateRelationship(PendingNode fromNodeId, PendingNode toNodeId, string relTypeName, Dictionary <string, object> variables, string rMutType)
        {
            if (_schemaCache == null)
            {
                _schemaCache = GetSchema();
            }
            //basic name pattern match, nothing fancy... could use mapping in JSON file but keep it simple for now
            var mutations = _schemaCache.GetMutations();// toNodeId.NodeName);
            //var relTypeName = relType.ToString();

            var mutationName  = $"{rMutType}_{fromNodeId.NodeName}_{relTypeName}_{toNodeId.NodeName}";
            var mutationField = mutations.FirstOrDefault(m => m.Name == mutationName);
            //find matching interfaces? just support for AbstractElement for now

            var toNodeType   = _schemaCache.GetBuildingGraphType(toNodeId.NodeName);
            var fromNodeType = _schemaCache.GetBuildingGraphType(fromNodeId.NodeName);

            if (mutationField == null)
            {
                foreach (var toNodInf in toNodeType.Interfaces)
                {
                    //var toNodInf = toNodeType.Interfaces.FirstOrDefault();
                    foreach (var fromNodInf in fromNodeType.Interfaces)
                    {
                        //var fromNodInf = fromNodeType.Interfaces.FirstOrDefault();

                        if (mutationField == null && !string.IsNullOrEmpty(fromNodInf))
                        {
                            mutationName  = $"{rMutType}_{fromNodInf}_{relTypeName}_{toNodeId.NodeName}";
                            mutationField = mutations.FirstOrDefault(m => m.Name == mutationName);
                        }

                        if (mutationField == null && !string.IsNullOrEmpty(toNodInf))
                        {
                            mutationName  = $"{rMutType}_{fromNodeId.NodeName}_{relTypeName}_{toNodInf}";
                            mutationField = mutations.FirstOrDefault(m => m.Name == mutationName);
                        }

                        if (mutationField == null && !string.IsNullOrEmpty(fromNodInf) && !string.IsNullOrEmpty(toNodInf))
                        {
                            mutationName  = $"{rMutType}_{fromNodInf}_{relTypeName}_{toNodInf}";
                            mutationField = mutations.FirstOrDefault(m => m.Name == mutationName);
                        }
                        if (mutationField != null)
                        {
                            break;
                        }
                    }
                    if (mutationField != null)
                    {
                        break;
                    }
                }
            }

            var mutationArgs = variables != null ? new Dictionary <string, object>(variables) : new Dictionary <string, object>();

            mutationArgs.Add("fromId", fromNodeId);
            mutationArgs.Add("toId", toNodeId);
            //mutationArgs.Add("MutationDateTime", "");
            //mutationArgs.Add("MutationUser", "");

            if (mutationField != null)
            {
                MutationRequest mr = new MutationRequest(mutationField, toNodeId, mutationArgs);
                _mutationRelateQueue.Enqueue(mr);
            }
            else
            {
                throw new Exception("No supported mutations for " + relTypeName);
            }
        }
예제 #2
0
        /// <summary>
        /// Adds or Updates a node in the graph
        /// </summary>
        /// <param name="node">The node to add/update</param>
        /// <param name="variables">Variables to add/update for this node</param>
        /// <param name="mergeOn">To update a node, add Ids or other identifying variables to match the existing node. Leave null to create a new node.</param>
        /// <returns>The pending node which can be used to relate it to other pending nodes</returns>
        public PendingNode Push(string nodeName, Dictionary <string, object> variables, Dictionary <string, object> mergeOn)
        {
            //find create mutation
            //find parameters
            //translate values?


            //map node name to GrapgQL type
            BuildingGraphMappedType mappedType = null;

            if (_clientMapping != null)
            {
                mappedType = _clientMapping.Types.FirstOrDefault(t => t.NativeType == nodeName);
            }

            var IsUpdate       = (mergeOn != null);
            var mutationPrefix = IsUpdate ? "Update" : "Create";
            var mutationName   = mutationPrefix + nodeName;

            if (_schemaCache == null)
            {
                _schemaCache = GetSchema();
            }
            var mutationField = _schemaCache.GetMutations(nodeName).FirstOrDefault(m => m.Name == mutationName);

            if (mutationField == null)
            {
                return(new PendingNode(nodeName));                      // throw new Exception("Unknown mutation type: " + mutationName);
            }
            //map node variable name to GraphQL variable name
            Dictionary <string, object> mutationArgs = null;

            if (mappedType == null)
            {
                mutationArgs = new Dictionary <string, object>(variables);
            }
            else
            {
                mutationArgs = new Dictionary <string, object>(variables);
            }

            if (IsUpdate)
            {
                //find matching id arguments for merge operation
                var mergeArgs = mutationField.Args.Where(arg => arg.TypeName == "ID!" && mergeOn.ContainsKey(arg.Name));
                foreach (var marg in mergeArgs)
                {
                    if (!mutationArgs.ContainsKey(marg.Name))
                    {
                        mutationArgs.Add(marg.Name, mergeOn[marg.Name]);
                    }
                }
            }

            var             pn = new PendingNode(nodeName);
            MutationRequest mr = new MutationRequest(mutationField, pn, mutationArgs)
            {
                ReturnFields = "Id"
            };

            _mutationPushQueue.Enqueue(mr);
            return(pn);
        }