11import { gql } from '@apollo/client'
2+ import { GraphQLJSON , GraphQLJSONObject } from 'graphql-type-json'
23import { aetherSchemaPlugin } from './aetherSchemaPlugin'
34import { AetherSchemaType } from './aetherSchemas'
5+ import { isEmpty } from '../utils/commonUtils'
46
57/* --- Scalars --------------------------------------------------------------------------------- */
68
7- const CUSTOM_SCALARS = [ 'scalar Date' ]
9+ const CUSTOM_SCALARS = [ 'scalar Date' , 'scalar JSON' , 'scalar JSONObject' ]
810
911/* --- Types ----------------------------------------------------------------------------------- */
1012
@@ -123,8 +125,8 @@ const aetherSchemaDefinitions = (aetherSchema: ResolverSchemaType, prefix = 'typ
123125 // -- Arraylikes --
124126 AetherArray : createDefinition ( 'Array' ) ,
125127 // -- Complex types --
126- // AetherUnion: createDefinition('Union') // TODO: To implement
127- // AetherTuple: createDefinition('Tuple') // TODO: To implement
128+ AetherUnion : createDefinition ( 'JSON' ) ,
129+ AetherTuple : createDefinition ( 'JSON' ) ,
128130 } )
129131 // Transform into usable graphql definitions
130132 const schemaDef = `
@@ -178,7 +180,24 @@ const createResolverDefinition = (resolverConfig: ResolverConfigType) => {
178180
179181/** --- aetherGraphSchema() -------------------------------------------------------------------- */
180182/** -i- Turn a mapped object of aetherResolvers into an executable GraphQL schema */
181- const aetherGraphSchema = ( aetherResolvers : ResolverMapType ) => {
183+ const aetherGraphSchema = (
184+ aetherResolvers : ResolverMapType ,
185+ {
186+ customSchemaDefinitions = '' ,
187+ customQueryDefinitions = '' ,
188+ customMutationDefinitions = '' ,
189+ customScalars = { } ,
190+ customQueries = { } ,
191+ customMutations = { } ,
192+ } : {
193+ customSchemaDefinitions ?: string
194+ customQueryDefinitions ?: string
195+ customMutationDefinitions ?: string
196+ customScalars ?: Record < string , ( ...args : unknown [ ] ) => Promise < unknown > >
197+ customQueries ?: Record < string , ( ...args : unknown [ ] ) => Promise < unknown > >
198+ customMutations ?: Record < string , ( ...args : unknown [ ] ) => Promise < unknown > >
199+ } = { }
200+ ) => {
182201 const resolverEntries = Object . entries ( aetherResolvers )
183202 const resolverConfigs = resolverEntries . map ( ( [ resolverName , resolver ] ) => ( {
184203 resolverName,
@@ -187,27 +206,44 @@ const aetherGraphSchema = (aetherResolvers: ResolverMapType) => {
187206 isMutation : ! ! resolver ?. isMutation ,
188207 resolver,
189208 } ) )
209+
190210 const mutationConfigs = resolverConfigs . filter ( ( resolverConfig ) => resolverConfig . isMutation )
211+ const mutationDefs = [ customMutationDefinitions , ...mutationConfigs . map ( createResolverDefinition ) ] . filter ( Boolean ) // prettier-ignore
212+ const hasMutations = mutationDefs . length > 0 || ! isEmpty ( customMutations )
213+
191214 const queryConfigs = resolverConfigs . filter ( ( resolverConfig ) => ! resolverConfig . isMutation )
192- const dataTypeDefs = Array . from ( new Set ( aetherGraphDefinitions ( resolverConfigs ) ) )
193- const mutationDefs = mutationConfigs . map ( createResolverDefinition )
194- const queryDefs = queryConfigs . map ( createResolverDefinition )
195- const hasMutations = mutationDefs . length > 0
196- const hasQueries = queryDefs . length > 0
215+ const queryDefs = [ customQueryDefinitions , ...queryConfigs . map ( createResolverDefinition ) ] . filter ( Boolean ) // prettier-ignore
216+ const hasQueries = queryDefs . length > 0 || ! isEmpty ( customQueries )
217+
197218 const mutation = hasMutations ? `type Mutation {\n ${ mutationDefs . join ( '\n ' ) } \n}` : ''
198219 const query = hasQueries ? `type Query {\n ${ queryDefs . join ( '\n ' ) } \n}` : ''
199- const allTypeDefs = [ ...CUSTOM_SCALARS , ...dataTypeDefs , mutation , query ] . filter ( Boolean )
220+
221+ const dataTypeDefs = Array . from ( new Set ( aetherGraphDefinitions ( resolverConfigs ) ) )
222+ const allTypeDefs = [
223+ customSchemaDefinitions ,
224+ ...CUSTOM_SCALARS ,
225+ ...dataTypeDefs ,
226+ mutation ,
227+ query ,
228+ ] . filter ( Boolean )
229+
200230 const typeDefsString = allTypeDefs . join ( '\n\n' )
201231 const graphqlSchemaDefs = gql `${ typeDefsString } ` // prettier-ignore
232+
202233 const rebuildFromConfig = ( handlers , { resolverName, resolver } ) => ( {
203234 ...handlers ,
204235 [ resolverName ] : resolver ,
205236 } )
206- const queryResolvers = queryConfigs . reduce ( rebuildFromConfig , { } )
207- const mutationResolvers = mutationConfigs . reduce ( rebuildFromConfig , { } )
237+
238+ const queryResolvers = queryConfigs . reduce ( rebuildFromConfig , customQueries )
239+ const mutationResolvers = mutationConfigs . reduce ( rebuildFromConfig , customMutations )
240+
208241 return {
209242 typeDefs : graphqlSchemaDefs ,
210243 resolvers : {
244+ JSON : GraphQLJSON ,
245+ JSONObject : GraphQLJSONObject ,
246+ ...customScalars ,
211247 ...( hasQueries ? { Query : queryResolvers } : { } ) ,
212248 ...( hasMutations ? { Mutations : mutationResolvers } : { } ) ,
213249 } ,
0 commit comments