GraphQL类型在类型上嵌套和循环依赖

时间:2017-01-30 09:46:06

标签: javascript graphql relayjs relay graphql-js

这里我有3种类型

JobType

EmployerType

SeekerType

job只有一个Employer创建了

const JobsType = new GraphQLObjectType({
    name: 'JobsType',
    fields: {
        jobsId: {
            type: GraphQLID,
            resolve: obj => obj._id,
        },
        title: { type: GraphQLString },
        description: { type: GraphQLString },
        experience: { type: GraphQLString },
        position: { type: GraphQLString },
        openings: { type: GraphQLInt },
        employer: {
            type: EmployerType
            resolve: () => // somedata here
        }
    },
});

Employer可以创建多个jobs所以

const EmployerType = new GraphQLObjectType({
    name: 'EmployerType',
    fields: {
        userId: {
            type: GraphQLID,
            resolve: obj => obj._id,
        },
        company: { type: GraphQLString },
        email: { type: GraphQLString },
        password: { type: GraphQLString },
        industry: { type: ENUM.IndustryType },
        ownerShip: { type: ENUM.OwnerShipType },
        website: { type: GraphQLString },
        jobs: {
            type: JobsType,
            resolve: (obj) => // some data to resolve
        }
    },
});

seeker可以适用于许多jobs

const SeekerType = new GraphQLObjectType({
    name: 'SeekerType',
    fields: {
        userId: {
            type: GraphQLID,
            resolve: obj => obj._id,
        },
        email: { type: GraphQLString },
        password: { type: GraphQLString },
        firstName: { type: GraphQLString },
        lastName: { type: GraphQLString },
        education: { type: GraphQLString },
        address: { type: GraphQLString },
        jobs: {
            type: JobsType,
            resolve: (obj) => // some data to resolve
        }
    },
});

所以这里似乎在GraphQLObjectType

上有多个和循环的依赖关系

Jobs包含EmployerType,再次EmployerType包含jobs,而job又包含EmployerType

所以喜欢这个

{
    job {
        jobId,
        jobTitle,
        employer {
            userId,
            company,
            email,
            job {
                jobId, 
                jobTitle,
                employer {
                    userId,
                    company,
                    email
                }
            }
        }
    }
}

这不是正确的方式,因为依赖关系越来越深入

同样适用于SeekerType

{
    seeker {
        userId,
        email,
        firstName,
        lastName,
        job {
            jobId,
            title,
            description,
            employer {
                userId,
                company,
                email,
                job {
                    jobId,
                    title,
                    employer {
                        userId,
                        company
                    }
                }
            }
        }
    }
}

这可能是什么解决方案

0 个答案:

没有答案