Beispiel #1
0
    private ValueTask <object?> ResolveRef(ISchema schema, ObjectType type)
    {
        var inClassResolverContextObject =
            type.ContextData[WellKnownContextData.EntityResolver];

        Assert.NotNull(inClassResolverContextObject);
        FieldResolverDelegate inClassResolverDelegate =
            Assert.IsType <FieldResolverDelegate>(inClassResolverContextObject);
        IResolverContext context = CreateResolverContext(schema);

        context.SetLocalValue("data", new ObjectValueNode());
        return(inClassResolverDelegate.Invoke(context));
    }
Beispiel #2
0
    private static async ValueTask <object?> ResolveSingleNode(
        IResolverContext context,
        IIdSerializer serializer,
        NameString argumentName)
    {
        StringValueNode nodeId         = context.ArgumentLiteral <StringValueNode>(argumentName);
        IdValue         deserializedId = serializer.Deserialize(nodeId.Value);
        NameString      typeName       = deserializedId.TypeName;

        context.SetLocalValue(NodeId, nodeId.Value);
        context.SetLocalValue(InternalId, deserializedId.Value);
        context.SetLocalValue(InternalType, typeName);
        context.SetLocalValue(WellKnownContextData.IdValue, deserializedId);

        if (context.Schema.TryGetType <ObjectType>(typeName, out ObjectType? type) &&
            type.ContextData.TryGetValue(NodeResolver, out var o) &&
            o is FieldResolverDelegate resolver)
        {
            return(await resolver.Invoke(context).ConfigureAwait(false));
        }

        return(null);
    }
    public static async Task <List <object?> > _Entities(
        ISchema schema,
        IReadOnlyList <Representation> representations,
        IResolverContext context)
    {
        var entities = new List <object?>();

        foreach (Representation representation in representations)
        {
            if (schema.TryGetType <INamedType>(representation.TypeName, out var entityType) &&
                !entityType.ContextData.ContainsKey(ExtendMarker))
            {
                entityType = null;
            }

            if (entityType != null)
            {
                if (entityType.ContextData.TryGetValue(EntityResolver, out var value) &&
                    value is Func <object, object?> d)
                {
                    entities.Add(d(representation));
                }
                else
                {
                    throw ThrowHelper.EntityResolver_NoResolverFound();
                }
            }
            else if (schema.TryGetType <ObjectType>(representation.TypeName, out var objectType) &&
                     objectType.ContextData.TryGetValue(EntityResolver, out var value) &&
                     value is FieldResolverDelegate resolver)
            {
                context.SetLocalValue("data", representation.Data);
                entities.Add(await resolver.Invoke(context).ConfigureAwait(false));
            }
            else
            {
                throw ThrowHelper.EntityResolver_NoResolverFound();
            }
        }

        return(entities);
    }
Beispiel #4
0
    private static async ValueTask <object?> ResolveManyNode(
        IResolverContext context,
        IIdSerializer serializer)
    {
        if (context.ArgumentKind(Ids) == ValueKind.List)
        {
            ListValueNode    list  = context.ArgumentLiteral <ListValueNode>(Ids);
            Task <object?>[] tasks = ArrayPool <Task <object?> > .Shared.Rent(list.Items.Count);

            var result = new object?[list.Items.Count];

            try
            {
                for (var i = 0; i < list.Items.Count; i++)
                {
                    context.RequestAborted.ThrowIfCancellationRequested();

                    // it is guaranteed that this is always a string literal.
                    StringValueNode nodeId         = (StringValueNode)list.Items[i];
                    IdValue         deserializedId = serializer.Deserialize(nodeId.Value);
                    NameString      typeName       = deserializedId.TypeName;

                    context.SetLocalValue(NodeId, nodeId.Value);
                    context.SetLocalValue(InternalId, deserializedId.Value);
                    context.SetLocalValue(InternalType, typeName);
                    context.SetLocalValue(WellKnownContextData.IdValue, deserializedId);

                    tasks[i] =
                        context.Schema.TryGetType <ObjectType>(typeName, out ObjectType? type) &&
                        type.ContextData.TryGetValue(NodeResolver, out var o) &&
                        o is FieldResolverDelegate resolver
                            ? resolver.Invoke(context).AsTask()
                            : _nullTask;
                }

                for (var i = 0; i < list.Items.Count; i++)
                {
                    context.RequestAborted.ThrowIfCancellationRequested();

                    Task <object?> task = tasks[i];
                    if (task.IsCompleted)
                    {
                        if (task.Exception is null)
                        {
                            result[i] = task.Result;
                        }
                        else
                        {
                            result[i] = null;
                            ReportError(context, i, task.Exception);
                        }
                    }
                    else
                    {
                        try
                        {
                            result[i] = await task;
                        }
                        catch (Exception ex)
                        {
                            result[i] = null;
                            ReportError(context, i, ex);
                        }
                    }
                }

                return(result);
            }
            finally
            {
                ArrayPool <Task <object?> > .Shared.Return(tasks);
            }
        }
        else
        {
            var result = new object?[1];
            result[0] = await ResolveSingleNode(context, serializer, Ids);

            return(result);
        }
    }