Skip to content

Commit

Permalink
Use metaline to keep the configuration a pure JSON (#39)
Browse files Browse the repository at this point in the history
* Use metaline for creating mappers

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Dec 6, 2023
1 parent 00da3d7 commit 2932dcc
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 5 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ async function main() {
// Resolver for retrieving multiple Books.
resolver: {
name: 'getBooksByIds',
argsAdapter: (partialResults) => {
return { ids: partialResults.map(r => r.id) }
}
argsAdapter: 'ids.$>#id'
}
}
},
Expand Down
27 changes: 25 additions & 2 deletions lib/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { SubscriptionClient } = require('@mercuriusjs/subscription-client')
const { getIntrospectionQuery } = require('graphql')
const fastify = require('fastify')
const mercurius = require('mercurius')
const metaline = require('metaline')
const { createEmptyObject, unwrapSchemaType } = require('./graphql-utils')
const { fetchSubgraphSchema, makeGraphqlRequest } = require('./network')
const { QueryBuilder } = require('./query-builder')
Expand Down Expand Up @@ -39,10 +40,11 @@ class Composer {
subgraphs = [],
onSubgraphError = onError,
subscriptions,
defaultArgsAdapter,
addEntitiesResolvers
} = options

let defaultArgsAdapter = options.defaultArgsAdapter

this.addEntitiesResolvers = !!addEntitiesResolvers

validateString(queryTypeName, 'queryTypeName')
Expand All @@ -52,6 +54,9 @@ class Composer {
validateFunction(onSubgraphError, 'onSubgraphError')

if (defaultArgsAdapter) {
if (typeof defaultArgsAdapter === 'string') {
defaultArgsAdapter = metaline(defaultArgsAdapter)
}
validateFunction(defaultArgsAdapter, 'defaultArgsAdapter')
}

Expand Down Expand Up @@ -122,7 +127,15 @@ class Composer {
validateString(fkey[p], `subgraphs[${subgraphName}].entities.${name}.fkeys[${k}].${p}`)
}
if (fkey.resolver) {
if (typeof fkey.resolver.argsAdapter === 'string') {
fkey.resolver.argsAdapter = metaline(fkey.resolver.argsAdapter)
}
if (typeof fkey.resolver.partialResults === 'string') {
fkey.resolver.partialResults = metaline(fkey.resolver.partialResults)
}

validateResolver(fkey.resolver, `subgraphs[${subgraphName}].entities.${name}.fkeys[${k}].resolver`)

if (!fkey.resolver.argsAdapter) {
fkey.resolver.argsAdapter = defaultArgsAdapter ?? createDefaultArgsAdapter(name, fkey.pkey)
}
Expand All @@ -138,6 +151,14 @@ class Composer {
if (!m[p]) { continue }
validateString(m[p], `subgraphs[${subgraphName}].entities.${name}.many[${k}].${p}`)
}

if (typeof m.resolver.argsAdapter === 'string') {
m.resolver.argsAdapter = metaline(m.resolver.argsAdapter)
}
if (typeof m.resolver.partialResults === 'string') {
m.resolver.partialResults = metaline(m.resolver.partialResults)
}

validateResolver(m.resolver, `subgraphs[${subgraphName}].entities.${name}.many[${k}].resolver`)
if (!m.resolver.argsAdapter) {
m.resolver.argsAdapter = defaultArgsAdapter ?? createDefaultArgsAdapter(name, m.pkey)
Expand All @@ -146,7 +167,9 @@ class Composer {

if (resolver) {
validateResolver(resolver, `subgraphs[${subgraphName}].entities.${name}.resolver`)
if (!resolver.argsAdapter) {
if (typeof resolver.argsAdapter === 'string') {
resolver.argsAdapter = metaline(resolver.argsAdapter)
} else if (!resolver.argsAdapter) {
resolver.argsAdapter = defaultArgsAdapter ?? createDefaultArgsAdapter(name, pkey)
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"fastify": "^4.24.3",
"graphql": "^16.8.1",
"mercurius": "^13.2.2",
"metaline": "^1.1.0",
"undici": "^6.0.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 2932dcc

Please sign in to comment.