enables graphql retrieval of collection by ID

This commit is contained in:
James
2020-04-05 16:45:28 -04:00
parent 40770a4d13
commit 85bfc677bf
2 changed files with 38 additions and 15 deletions

View File

@@ -1,11 +1,17 @@
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLNonNull,
} = require('graphql');
const graphQLHTTP = require('express-graphql');
const { GraphQLObjectType, GraphQLString, GraphQLSchema } = require('graphql');
const getBuildObjectType = require('./schema/getBuildObjectType');
const buildWhereInputType = require('./schema/buildWhereInputType');
const formatName = require('./utilities/formatName');
const withPolicy = require('./resolvers/withPolicy');
const getLocaleStringType = require('./types/getLocaleStringType');
const getLocaleFloatType = require('./types/getLocaleFloatType');
const { findByID } = require('../collections/queries');
const Query = {
name: 'Query',
@@ -25,7 +31,7 @@ function init() {
},
};
const buildObjectType = getBuildObjectType(this);
const buildObjectType = getBuildObjectType(this.config, this.graphQL);
const userType = new GraphQLObjectType({
name: 'User',
@@ -38,7 +44,7 @@ function init() {
Query.fields.User = {
type: userType,
args: {
id: { type: GraphQLString },
id: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: (_, { id }) => {
return {
@@ -56,28 +62,45 @@ function init() {
policies,
fields,
labels: {
singular: singularLabel,
singular,
plural,
},
},
} = collection;
const label = formatName(singularLabel);
const singularLabel = formatName(singular);
const pluralLabel = formatName(plural);
collection.graphQLType = buildObjectType(label, fields, label);
collection.graphQLType = buildObjectType(singularLabel, fields, singularLabel);
collection.graphQLWhereInputType = buildWhereInputType({
name: label,
name: singularLabel,
fields,
parent: label,
parent: singularLabel,
});
Query.fields[label] = {
Query.fields[singularLabel] = {
type: collection.graphQLType,
args: {
id: { type: GraphQLString },
},
resolve: withPolicy(policies.read, async (_, { id }) => {
const doc = findByID({
depth: 0,
Model: collection.model,
id,
});
return doc;
}),
};
Query.fields[`get${pluralLabel}`] = {
type: collection.graphQLType,
args: {
where: { type: collection.graphQLWhereInputType },
},
resolve: withPolicy(policies.read, async (_, args, context) => {
console.log(JSON.stringify(args));
return {
image: 'test',
};

View File

@@ -13,20 +13,20 @@ const formatName = require('../utilities/formatName');
const combineParentName = require('../utilities/combineParentName');
const withNullableType = require('./withNullableType');
function getBuildObjectType(context) {
function getBuildObjectType(config, graphQL) {
const withLocalizedType = (field, type) => {
if (context.config.localization && field.localized) {
if (config.localization && field.localized) {
if (type instanceof GraphQLObjectType) {
const LocaleObjectType = context.graphQL.buildLocaleObjectType(field, type);
const LocaleObjectType = graphQL.buildLocaleObjectType(field, type);
return LocaleObjectType;
}
if (type === GraphQLString) {
return context.graphQL.types.LocaleStringType;
return graphQL.types.LocaleStringType;
}
if (type === GraphQLFloat) {
return context.graphQL.types.LocaleFloatType;
return graphQL.types.LocaleFloatType;
}
}