UNPKG

@composedb/devtools

Version:

Development tools for ComposeDB projects.

166 lines (165 loc) 4.3 kB
import { scalars as graphQLScalars } from '@composedb/graphql-scalars'; import { GraphQLScalarType } from 'graphql'; import { isEqual } from 'lodash-es'; export const SCALAR_RUNTIME_TYPES = { CeramicCommitID: 'commitid', CeramicStreamID: 'streamid', ChainAgnosticAccountID: 'accountid', ChainAgnosticChainID: 'chainid', GraphQLCountryCode: 'countrycode', GraphQLDate: 'date', GraphQLDateTime: 'datetime', GraphQLDID: 'did', GraphQLDuration: 'duration', GraphQLID: 'id', GraphQLLatitude: 'latitude', GraphQLLocalDate: 'localdate', GraphQLLocale: 'locale', GraphQLLocalTime: 'localtime', GraphQLLongitude: 'longitude', GraphQLTime: 'time', GraphQLTimeZone: 'timezone', GraphQLURI: 'uri', GraphQLUtcOffset: 'utcoffset', InterPlanetaryCID: 'cid' }; const SCALAR_SCHEMA_TITLES = Object.entries(SCALAR_RUNTIME_TYPES).reduce((acc, [title, type])=>{ acc[type] = title; return acc; }, {}); export function getGraphQLScalarSchema(type) { // The GraphQL Scalars library provides the JSON schema matching the scalars it exposes const schema = graphQLScalars[type]?.extensions.jsonSchema; if (schema == null) { throw new Error(`Missing JSON schema in scalar extensions for type: ${type}`); } // Ensure we have an explicit set title for the scalar for unique identification between tools and runtime const title = SCALAR_SCHEMA_TITLES[type]; if (title == null) { throw new Error(`Missing schema title for scalar: ${type}`); } return { ...schema, title }; } export const extraScalars = { AccountID: { type: 'string', title: 'ChainAgnosticAccountID', maxLength: 106 }, ChainID: { type: 'string', title: 'ChainAgnosticChainID', maxLength: 41 }, CID: { type: 'string', title: 'InterPlanetaryCID', maxLength: 100 }, CommitID: { type: 'string', title: 'CeramicCommitID', maxLength: 200 }, CountryCode: { ...getGraphQLScalarSchema('countrycode'), maxLength: 2 }, Date: { ...getGraphQLScalarSchema('date'), maxLength: 100 }, DateTime: { ...getGraphQLScalarSchema('datetime'), maxLength: 100 }, DID: { ...getGraphQLScalarSchema('did'), maxLength: 100 }, Duration: { ...getGraphQLScalarSchema('duration'), maxLength: 100 }, Latitude: getGraphQLScalarSchema('latitude'), LocalDate: { ...getGraphQLScalarSchema('localdate'), maxLength: 100 }, Locale: { ...getGraphQLScalarSchema('locale'), maxLength: 100 }, LocalTime: { ...getGraphQLScalarSchema('localtime'), maxLength: 100 }, Longitude: getGraphQLScalarSchema('longitude'), StreamID: { type: 'string', title: 'CeramicStreamID', maxLength: 100 }, Time: { ...getGraphQLScalarSchema('time'), maxLength: 100 }, TimeZone: { ...getGraphQLScalarSchema('timezone'), maxLength: 100 }, URI: { ...getGraphQLScalarSchema('uri'), maxLength: 100 }, UTCOffset: { ...getGraphQLScalarSchema('utcoffset'), maxLength: 100 } }; const scalars = { ...extraScalars, Boolean: { type: 'boolean' }, Float: { type: 'number' }, ID: { type: 'string', title: 'GraphQLID', maxLength: 100 }, Int: { type: 'integer' }, String: { type: 'string' } }; export function getScalarSchema(scalar) { const name = scalar instanceof GraphQLScalarType ? scalar.name : scalar; const schema = scalars[name]; if (schema == null) { throw new Error(`Unsupported scalar name: ${name}`); } return { ...schema }; } const scalarsByTitle = Object.values(scalars).reduce((acc, schema)=>{ if (schema.title != null) { acc[schema.title] = schema; } return acc; }, {}); export function isCommonScalar(schema) { if (schema.title == null) { return false; } const scalar = scalarsByTitle[schema.title]; return isEqual(scalar, schema); }