-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add decorateBaseSchema function to modify dynamic schema
fix: fix exception
- Loading branch information
MARCO MANCO s281564
committed
May 3, 2021
1 parent
e56427c
commit 23f58a6
Showing
4 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const { gql } = require('apollo-server-core'); | ||
const { extendSchema } = require('graphql/utilities'); | ||
const { addResolversToSchema } = require('@graphql-tools/schema'); | ||
|
||
/** | ||
* | ||
* @param {*} targetQuery : Query wrapped int the extended object Type | ||
* @param {*} extendedType : Object type that should be extended | ||
* @param {*} baseSchema : GraphQL Schema where the type and query are written | ||
* @param {*} nameWrapper : Custom name for the wrapper | ||
* @param {*} argsNeeded : Arguments extracted from the father and passed from the wrapper | ||
* @returns | ||
*/ | ||
|
||
function capitalizeType(name) { | ||
return name[0].toUpperCase() + name.slice(1); | ||
} | ||
|
||
module.exports = { | ||
decorateBaseSchema: function ( | ||
targetQuery, | ||
extendedType, | ||
baseSchema, | ||
nameWrapper, | ||
argsNeeded | ||
) { | ||
if (!targetQuery) throw 'Parameter targetQuery cannot be empty!'; | ||
if (!extendedType) throw 'Parameter extendedType cannot be empty!'; | ||
if (!nameWrapper) throw 'Parameter nameWrapper cannot be empty!'; | ||
if (!argsNeeded) throw 'Parameter argsNeeded cannot be empty!'; | ||
if (!baseSchema) throw 'Parameter baseSchema cannot be empty!'; | ||
|
||
if (baseSchema.getQueryType().getFields()[targetQuery] === undefined) | ||
throw 'Parameter targetQuery not valid!'; | ||
const targetType = baseSchema.getQueryType().getFields()[targetQuery]; | ||
|
||
if (!targetType) throw 'targetType fault!'; | ||
|
||
let typeWrapper = capitalizeType(nameWrapper); | ||
let typeTargetQuery = capitalizeType(targetQuery); | ||
|
||
const extension = gql` | ||
extend type ${extendedType} { | ||
${nameWrapper}: ${typeWrapper} | ||
} | ||
type ${typeWrapper} { | ||
${targetQuery}: ${typeTargetQuery} | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
[extendedType]: { | ||
[nameWrapper]: (parent, args, context, info) => { | ||
let newParent = {}; | ||
for (e of argsNeeded) { | ||
if (parent[e] === undefined) | ||
throw `Error: ${e} is not into the parent object!`; | ||
newParent[e] = parent[e]; | ||
} | ||
return newParent; | ||
}, | ||
}, | ||
[typeWrapper]: { | ||
[targetQuery]: (parent, args, context, info) => { | ||
return targetType.resolve(parent, parent, context, info); | ||
}, | ||
}, | ||
}; | ||
|
||
const extendedSchema = extendSchema(baseSchema, extension); | ||
const newSchema = addResolversToSchema(extendedSchema, resolvers); | ||
return newSchema; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters