all files / semantic-graphql/src/graphql/ getGraphqlFieldConfig.js

87.18% Statements 34/39
60% Branches 12/20
100% Functions 1/1
91.43% Lines 32/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60   27×           16× 16×   16×   16×       16×   11×       11× 11×     16×   16×             16×   16×      
const { GraphQLList } = require('graphql');
const { xsdIri, rdfsLiteral, rdfsSubPropertyOf, rdfsRange } = require('../constants');
const warn = require('../utils/warn');
const { walklook } = require('../graph/traversal');
const memorize = require('../graph/memorize');
const requireGraphqlRelay = require('../requireGraphqlRelay');
const isGraphqlList = require('./isGraphqlList');
const getGraphqlDescription = require('./getGraphqlDescription');
const getGraphqlObjectType = require('./getGraphqlObjectType');
const getGraphqlScalarType = require('./getGraphqlScalarType');
const getGraphqlPolymorphicScalarType = require('./getGraphqlPolymorphicScalarType');
const getGraphqlPolymorphicObjectType = require('./getGraphqlPolymorphicObjectType');
const getGraphqlScalarResolver = require('./getGraphqlScalarResolver');
const getGraphqlObjectResolver = require('./getGraphqlObjectResolver');
const getRelayConnectionDefinitions = require('./getRelayConnectionDefinitions');
 
const isLiteral = iri => iri.startsWith(xsdIri) || iri === rdfsLiteral;
 
function getGraphqlFieldConfig(g, iri) {
  // Look for a range, return it if found
  // Otherwise for each super-property, look for a range,
  // if not found, check their super-properties and so on
  // TODO: check walklook, maybe test it
  const ranges = [...walklook(g, iri, rdfsSubPropertyOf, rdfsRange)];
  const nRanges = ranges.length;
 
  Iif (!nRanges) return;
 
  const fieldConfig = {
    description: getGraphqlDescription(g, iri),
  };
 
  if (ranges.every(isLiteral)) {
    fieldConfig.resolve = getGraphqlScalarResolver(g, iri);
    fieldConfig.type = nRanges === 1 ? getGraphqlScalarType(g, ranges[0]) : getGraphqlPolymorphicScalarType(g, ranges);
  }
  else Iif (ranges.some(isLiteral)) {
    return warn(`Mixed literal/non-literal ranges on ${iri}:\n${ranges}`);
  }
  else {
    fieldConfig.resolve = getGraphqlObjectResolver(g, iri, ranges);
    fieldConfig.type = nRanges === 1 ? getGraphqlObjectType(g, ranges[0]) : getGraphqlPolymorphicObjectType(g, ranges);
  }
 
  Eif (isGraphqlList(g, iri)) fieldConfig.type = new GraphQLList(fieldConfig.type);
 
  Iif (g.config.relay && g[iri].isRelayConnection) {
    fieldConfig.args = requireGraphqlRelay().connectionArgs;
    fieldConfig.type = getRelayConnectionDefinitions(g, ranges[0]).connectionType;
  }
 
  // Support partial overrides from user
  // full override is achieved with the memorize wrapper
  Iif (typeof g[iri].graphqlFieldConfigExtension === 'object') Object.assign(fieldConfig, g[iri].graphqlFieldConfigExtension);
 
  return fieldConfig;
}
 
module.exports = memorize(getGraphqlFieldConfig, 'graphqlFieldConfig');