-
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
Combine 4 in 1 feat: add decorateBaseSchema function fix: add tokenJSONpath: '$.token', feat: add decorateBaseSchema function fix: add tokenJSONpath: '$.token', feat: add decorateBaseSchema function
- Loading branch information
MARCO MANCO s281564
committed
May 1, 2021
1 parent
65fed46
commit 3d17b73
Showing
4 changed files
with
156 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,65 @@ | ||
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 {*} argsNeeded : Optional arguments extracted from the father and passed from the wrapper | ||
* @param {*} baseSchema : GraphQL Schema where the type and query are written | ||
* @param {*} nameWrapper : Optional custom name for the wrapper | ||
* @returns | ||
*/ | ||
|
||
function capitalizeType(name) { | ||
return name[0].toUpperCase() + name.slice(1); | ||
} | ||
|
||
module.exports = { | ||
decorateBaseSchema: function ( | ||
targetQuery, | ||
extendedType, | ||
argsNeeded, | ||
nameWrapper, | ||
baseSchema | ||
) { | ||
if (!targetQuery) return baseSchema; | ||
if (!extendedType) return baseSchema; | ||
const targetType = baseSchema.getQueryType().getFields()[targetQuery]; | ||
if (!targetType) return baseSchema; | ||
|
||
nameWrapper = nameWrapper ? nameWrapper : 'fieldWrapper'; | ||
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) { | ||
newParent[e] = parent[e]; | ||
} | ||
return newParent !== {} ? newParent : parent; // gestire errori | ||
}, | ||
}, | ||
[typeWrapper]: { | ||
[targetQuery]: (parent, args, context, info) => { | ||
return targetType.resolve(parent, parent, context, info); | ||
}, | ||
}, | ||
}; | ||
|
||
// extending the schema | ||
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