Reputation: 49804
I created a GraphQL Page function. However, when I try to reuse it, I got error
Schema must contain unique named types but contains multiple types named "Page"
I know I can change Page
to two same functions and name them to JobsPage
and PrintsPage
.
However, is there an easy way to reuse it? Thanks
function Page(itemType) {
return new GraphQLObjectType({
name: 'Page',
fields: () => ({
totalCount: { type: GraphQLInt },
edges: { type: new GraphQLList(Edge(itemType)) },
pageInfo: { type: PageInfo }
})
});
}
const PrinterType = new GraphQLObjectType({
name: 'Printer',
fields: () => ({
id: { type: GraphQLString },
jobs: {
type: Page(JobType), // here I use Page first time
args: {
first: {
type: GraphQLInt
},
after: {
type: GraphQLString
}
},
resolve(parentValue, args) {
const { id } = parentValue;
const { first, after } = args;
return getPageJobs({ id, first, after });
}
},
prints: {
type: Page(PrintType), // here I use Page again
args: {
first: {
type: GraphQLInt
},
after: {
type: GraphQLString
}
},
resolve(parentValue, args) {
const { id } = parentValue;
const { first, after } = args;
return getPagePrints({ id, first, after });
}
},
})
});
Upvotes: 1
Views: 1868
Reputation: 49804
I missed it is just a pure function, I can pass a second value...
Here is one solution.
function Page(itemType, name) {
return new GraphQLObjectType({
name: `${name}Page`,
fields: () => ({
totalCount: { type: GraphQLInt },
edges: { type: new GraphQLList(Edge(itemType, name)) },
pageInfo: { type: PageInfo }
})
});
}
const PrinterType = new GraphQLObjectType({
name: 'Printer',
fields: () => ({
id: { type: GraphQLString },
jobs: {
type: Page(JobType, 'Jobs'), // here I use Page first time
args: {
first: {
type: GraphQLInt
},
after: {
type: GraphQLString
}
},
resolve(parentValue, args) {
const { id } = parentValue;
const { first, after } = args;
return getPageJobs({ id, first, after });
}
},
prints: {
type: Page(PrintType, 'Prints'), // here I use Page again
args: {
first: {
type: GraphQLInt
},
after: {
type: GraphQLString
}
},
resolve(parentValue, args) {
const { id } = parentValue;
const { first, after } = args;
return getPagePrints({ id, first, after });
}
},
})
});
Upvotes: 1