protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef)
        {
            var name       = (string)unionDef.Name.Value;
            var typeConfig = Types.For(name);

            AssertKnownType(typeConfig);

            var type = new UnionGraphType
            {
                Name        = name,
                Description = typeConfig.Description ?? unionDef.Description?.Value.ToString() ?? unionDef.Comment?.Text.ToString(),
                ResolveType = typeConfig.ResolveType,
            }.SetAstType(unionDef);

            OverrideDeprecationReason(type, typeConfig.DeprecationReason);

            typeConfig.CopyMetadataTo(type);

            if (unionDef.Types?.Count > 0) // just in case
            {
                foreach (var x in unionDef.Types)
                {
                    string n = (string)x.Name.Value;
                    type.AddPossibleType((GetType(n) ?? new GraphQLTypeReference(n)) as IObjectGraphType);
                }
            }

            return(type);
        }
        private UnionGraphType BuildEntityGraphType(ISchema schema)
        {
            var union = new UnionGraphType
            {
                Name        = "_Entity",
                Description = "A union of all types that use the @key directive"
            };

            var entities = _types.Values.Where(IsEntity).Select(x => x as IObjectGraphType).ToList();

            foreach (var e in entities)
            {
                union.AddPossibleType(e);
            }

            union.ResolveType = x =>
            {
                if (x is Dictionary <string, object> dict)
                {
                    if (dict.TryGetValue("__typename", out object typeName))
                    {
                        return(new GraphQLTypeReference(typeName.ToString()));
                    }
                }

                // TODO: Provide another way to give graph type name, such as an attribute
                return(new GraphQLTypeReference(x.GetType().Name));
            };

            return(union);
        }
        public void build_union()
        {
            var schema = new Schema();

            var person = new ObjectGraphType {
                Name = "Person"
            };

            person.Field("name", new StringGraphType());
            person.IsTypeOf = type => true;

            var robot = new ObjectGraphType {
                Name = "Robot"
            };

            robot.Field("name", new StringGraphType());
            robot.IsTypeOf = type => true;

            var personOrRobot = new UnionGraphType {
                Name = "PersonOrRobot"
            };

            personOrRobot.AddPossibleType(person);
            personOrRobot.AddPossibleType(robot);

            var root = new ObjectGraphType {
                Name = "Root"
            };

            root.Field("hero", personOrRobot, resolve: ctx => ctx.RootValue);

            schema.Query = root;

            AssertQuerySuccess(
                schema,
                @"{ hero {
                    ... on Person { name }
                    ... on Robot { name }
                } }",
                @"{ hero: { name : 'Quinn' }}",
                root: new SomeObject {
                Name = "Quinn"
            });
        }
Пример #4
0
        protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef)
        {
            var typeConfig = Types.For(unionDef.Name.Value);

            var type = new UnionGraphType();

            type.Name        = unionDef.Name.Value;
            type.Description = typeConfig.Description;
            type.ResolveType = typeConfig.ResolveType;

            CopyMetadata(type, typeConfig);

            var possibleTypes = unionDef.Types.Select(x => GetType(x.Name.Value));

            possibleTypes.Apply(x => type.AddPossibleType(x as IObjectGraphType));
            return(type);
        }
Пример #5
0
        protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef)
        {
            var typeConfig = Types.For(unionDef.Name.Value);

            var type = new UnionGraphType
            {
                Name        = unionDef.Name.Value,
                Description = typeConfig.Description ?? unionDef.Comment?.Text,
                ResolveType = typeConfig.ResolveType
            };

            CopyMetadata(type, typeConfig);

            var possibleTypes = unionDef.Types.Select(x => GetType(x.Name.Value) ?? new GraphQLTypeReference(x.Name.Value));

            possibleTypes.Apply(x => type.AddPossibleType(x as IObjectGraphType));
            return(type);
        }
Пример #6
0
        protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef)
        {
            var typeConfig = Types.For(unionDef.Name.Value);

            var type = new UnionGraphType
            {
                Name        = unionDef.Name.Value,
                Description = typeConfig.Description,
                ResolveType = typeConfig.ResolveType
            };

            ApplyDeprecatedDirective(unionDef.Directives, reason =>
            {
                type.DeprecationReason = typeConfig.DeprecationReason ?? reason;
            });

            CopyMetadata(type, typeConfig);

            var possibleTypes = unionDef.Types.Select(x => GetType(x.Name.Value));

            possibleTypes.Apply(x => type.AddPossibleType(x as IObjectGraphType));
            return(type);
        }