Files
payload/src/collections/graphql/register.js

101 lines
2.7 KiB
JavaScript

const {
GraphQLString,
GraphQLNonNull,
GraphQLInt,
} = require('graphql');
const formatName = require('../../graphql/utilities/formatName');
const {
getCreate, getFind, getFindByID, getDelete, getUpdate,
} = require('./resolvers');
const buildPaginatedListType = require('../../graphql/schema/buildPaginatedListType');
function registerCollections() {
Object.keys(this.collections).forEach((slug) => {
const collection = this.collections[slug];
const {
config: {
labels: {
singular,
plural,
},
fields,
},
} = collection;
const singularLabel = formatName(singular);
const pluralLabel = formatName(plural);
collection.graphQL = {};
collection.graphQL.type = this.buildObjectType(
singularLabel,
fields,
singularLabel,
true,
);
collection.graphQL.whereInputType = this.buildWhereInputType(
singularLabel,
fields,
singularLabel,
);
collection.graphQL.mutationInputType = this.buildMutationInputType(
singularLabel,
fields,
singularLabel,
);
this.Query.fields[singularLabel] = {
type: collection.graphQL.type,
args: {
id: { type: GraphQLString },
locale: { type: this.types.localeInputType },
fallbackLocale: { type: this.types.fallbackLocaleInputType },
},
resolve: getFindByID(this.config, collection),
};
this.Query.fields[pluralLabel] = {
type: buildPaginatedListType(pluralLabel, collection.graphQL.type),
args: {
where: { type: collection.graphQL.whereInputType },
locale: { type: this.types.localeInputType },
fallbackLocale: { type: this.types.fallbackLocaleInputType },
page: { type: GraphQLInt },
limit: { type: GraphQLInt },
sort: { type: GraphQLString },
},
resolve: getFind(this.config, collection),
};
this.Mutation.fields[`create${singularLabel}`] = {
type: collection.graphQL.type,
args: {
data: { type: collection.graphQL.mutationInputType },
},
resolve: getCreate(this.config, collection),
};
this.Mutation.fields[`update${singularLabel}`] = {
type: collection.graphQL.type,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
data: { type: collection.graphQL.mutationInputType },
},
resolve: getUpdate(this.config, collection),
};
this.Mutation.fields[`delete${singularLabel}`] = {
type: collection.graphQL.type,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: getDelete(collection),
};
});
}
module.exports = registerCollections;