From 85bfc677bf4a38c364ffd2ee4a18a09b14bfd1cd Mon Sep 17 00:00:00 2001 From: James Date: Sun, 5 Apr 2020 16:45:28 -0400 Subject: [PATCH] enables graphql retrieval of collection by ID --- src/graphql/init.js | 43 ++++++++++++++++++------ src/graphql/schema/getBuildObjectType.js | 10 +++--- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/graphql/init.js b/src/graphql/init.js index 86493e02b2..950c013d88 100644 --- a/src/graphql/init.js +++ b/src/graphql/init.js @@ -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', }; diff --git a/src/graphql/schema/getBuildObjectType.js b/src/graphql/schema/getBuildObjectType.js index 162693bc5d..49fc431bd2 100644 --- a/src/graphql/schema/getBuildObjectType.js +++ b/src/graphql/schema/getBuildObjectType.js @@ -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; } }