private static void ResolveScopedVariableArguments(
            IResolverContext context,
            NameString schemaName,
            SelectionPathComponent component,
            IOutputField field,
            ICollection <Delegation.VariableValue> variables,
            ExtractFieldQuerySyntaxRewriter rewriter)
        {
            foreach (ArgumentNode argument in component.Arguments)
            {
                if (!field.Arguments.TryGetField(argument.Name.Value, out IInputField arg))
                {
                    throw new QueryException(
                              ErrorBuilder.New()
                              .SetMessage(
                                  StitchingResources.DelegationMiddleware_ArgumentNotFound,
                                  argument.Name.Value)
                              .SetExtension("argument", argument.Name.Value)
                              .SetCode(ErrorCodes.ArgumentNotFound)
                              .Build());
                }

                if (argument.Value is ScopedVariableNode sv)
                {
                    Delegation.VariableValue variable =
                        _resolvers.Resolve(context, sv, arg.Type);
                    IValueNode value = rewriter.RewriteValueNode(
                        schemaName, arg.Type, variable.Value);
                    variables.Add(variable.WithValue(value));
                }
            }
        }
            public ITypeDefinitionNode Rewrite(
                ISchemaInfo schema,
                ITypeDefinitionNode typeDefinition)
            {
                if (typeDefinition.Name.Value.Equals("Query") &&
                    typeDefinition is ObjectTypeDefinitionNode objectType)
                {
                    var path = new SelectionPathComponent(
                        new NameNode("foo"),
                        new[]
                    {
                        new ArgumentNode(
                            "a",
                            new ScopedVariableNode(
                                ScopeNames.ContextData,
                                "foo_a"))
                    });

                    Dictionary <string, FieldDefinitionNode> fields =
                        objectType.Fields.ToDictionary(t => t.Name.Value);
                    fields["foo"] = fields["foo"]
                                    .WithArguments(Array.Empty <InputValueDefinitionNode>())
                                    .AddDelegationPath("someSchema", path);
                    return(objectType.WithFields(fields.Values.ToArray()));
                }
                return(typeDefinition);
            }
Example #3
0
        private static void ResolveScopedVariableArguments(
            IResolverContext context,
            SelectionPathComponent component,
            IOutputField field,
            ICollection <VariableValue> variables)
        {
            foreach (ArgumentNode argument in component.Arguments)
            {
                if (!field.Arguments.TryGetField(argument.Name.Value,
                                                 out IInputField arg))
                {
                    throw new QueryException(new Error
                    {
                        Message = string.Format(
                            CultureInfo.InvariantCulture,
                            StitchingResources
                            .DelegationMiddleware_ArgumentNotFound,
                            argument.Name.Value)
                    });
                }

                if (argument.Value is ScopedVariableNode sv)
                {
                    variables.Add(_resolvers.Resolve(
                                      context, sv, arg.Type.ToTypeNode()));
                }
            }
        }
Example #4
0
        private static IReadOnlyList <VariableValue> ResolveScopedVariables(
            IResolverContext context,
            NameString schemaName,
            OperationType operationType,
            IEnumerable <SelectionPathComponent> components)
        {
            IStitchingContext stitchingContext =
                context.Service <IStitchingContext>();

            ISchema remoteSchema =
                stitchingContext.GetRemoteSchema(schemaName);

            IComplexOutputType type =
                remoteSchema.GetOperationType(operationType);

            var variables = new List <VariableValue>();

            SelectionPathComponent[] comps = components.Reverse().ToArray();

            for (int i = 0; i < comps.Length; i++)
            {
                SelectionPathComponent component = comps[i];

                if (!type.Fields.TryGetField(component.Name.Value,
                                             out IOutputField field))
                {
                    throw new QueryException(new Error
                    {
                        Message = string.Format(
                            CultureInfo.InvariantCulture,
                            StitchingResources
                            .DelegationMiddleware_PathElementInvalid,
                            component.Name.Value,
                            type.Name)
                    });
                }

                ResolveScopedVariableArguments(
                    context, component, field, variables);

                if (i + 1 < comps.Length)
                {
                    if (!field.Type.IsComplexType())
                    {
                        throw new QueryException(new Error
                        {
                            Message = StitchingResources
                                      .DelegationMiddleware_PathElementTypeUnexpected
                        });
                    }
                    type = (IComplexOutputType)field.Type.NamedType();
                }
            }

            return(variables);
        }
Example #5
0
        private FieldNode CreateSelection(
            FieldNode previous,
            SelectionPathComponent next)
        {
            var selectionSet = new SelectionSetNode(
                null,
                new List <ISelectionNode> {
                previous
            });

            return(CreateSelection(selectionSet, next, null));
        }
Example #6
0
        private static void ResolveScopedVariableArguments(
            IResolverContext context,
            SelectionPathComponent component,
            IOutputField field,
            ICollection <VariableValue> variables)
        {
            ITypeConversion typeConversion =
                context.Service <IServiceProvider>()
                .GetTypeConversion();

            foreach (ArgumentNode argument in component.Arguments)
            {
                if (!field.Arguments.TryGetField(argument.Name.Value,
                                                 out IInputField arg))
                {
                    throw new QueryException(new Error
                    {
                        Message = string.Format(
                            CultureInfo.InvariantCulture,
                            StitchingResources
                            .DelegationMiddleware_ArgumentNotFound,
                            argument.Name.Value)
                    });
                }

                if (argument.Value is ScopedVariableNode sv)
                {
                    VariableValue variable =
                        _resolvers.Resolve(context, sv, arg.Type.ToTypeNode());

                    object value = variable.Value;

                    if (!arg.Type.IsInstanceOfType(value))
                    {
                        value = ConvertValue(typeConversion, arg.Type, value);
                    }

                    variable = new VariableValue
                               (
                        variable.Name,
                        variable.Type,
                        arg.Type.Serialize(value),
                        variable.DefaultValue
                               );

                    variables.Add(variable);
                }
            }
        }
Example #7
0
        private FieldNode CreateSelection(
            SelectionSetNode selectionSet,
            SelectionPathComponent next,
            string alias)
        {
            var aliasNode = string.IsNullOrEmpty(alias)
                ? null : new NameNode(alias);

            return(new FieldNode
                   (
                       null,
                       next.Name,
                       aliasNode,
                       Array.Empty <DirectiveNode>(),
                       RewriteVariableNames(next.Arguments).ToList(),
                       selectionSet
                   ));
        }
Example #8
0
        private DocumentNode CreateDelegationQuery(
            OperationType operation,
            Stack <SelectionPathComponent> path,
            FieldNode selection)
        {
            FieldNode current = selection;

            if (path.Any())
            {
                string responseName = current.Alias == null
                    ? current.Name.Value
                    : current.Alias.Value;

                SelectionPathComponent component = path.Pop();

                string alias = component.Name.Value.EqualsOrdinal(responseName)
                    ? null
                    : responseName;

                current = CreateSelection(
                    current.SelectionSet,
                    component,
                    responseName);

                while (path.Any())
                {
                    current = CreateSelection(current, path.Pop());
                }
            }

            return(new DocumentNode(
                       null,
                       new List <IDefinitionNode>
            {
                CreateOperation(operation, current)
            }));
        }
Example #9
0
        private static void ResolveScopedVariableArguments(
            IResolverContext context,
            SelectionPathComponent component,
            IOutputField field,
            ICollection <VariableValue> variables)
        {
            ITypeConversion typeConversion =
                context.Service <IServiceProvider>()
                .GetTypeConversion();

            foreach (ArgumentNode argument in component.Arguments)
            {
                if (!field.Arguments.TryGetField(argument.Name.Value,
                                                 out IInputField arg))
                {
                    throw new QueryException(new Error
                    {
                        Message = string.Format(
                            CultureInfo.InvariantCulture,
                            StitchingResources
                            .DelegationMiddleware_ArgumentNotFound,
                            argument.Name.Value)
                    });
                }

                if (argument.Value is ScopedVariableNode sv)
                {
                    VariableValue variable =
                        _resolvers.Resolve(context, sv, arg.Type.ToTypeNode());

                    if (context.Schema.TryGetType(
                            arg.Type.NamedType().Name,
                            out INamedInputType inputType))
                    {
                        object value = variable.Value;

                        if (!inputType.IsInstanceOfType(value))
                        {
                            value = typeConversion.Convert(
                                typeof(object), inputType.ClrType, value);
                        }

                        variable = new VariableValue
                                   (
                            variable.Name,
                            variable.Type,
                            inputType.Serialize(value),
                            variable.DefaultValue
                                   );
                    }
                    else
                    {
                        // TODO : resources
                        throw new QueryException(
                                  ErrorBuilder.New()
                                  .SetMessage(string.Format(
                                                  CultureInfo.InvariantCulture,
                                                  "Serialize argument {0} of type {1}.",
                                                  arg.Name, arg.Type.Visualize()))
                                  .SetPath(context.Path)
                                  .Build());
                    }

                    variables.Add(variable);
                }
            }
        }