-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: upgrade GraphQL dependencies (#7970)
- Loading branch information
Showing
10 changed files
with
128 additions
and
334 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -1,55 +1,56 @@ | ||
import gql from 'graphql-tag'; | ||
import { SchemaDirectiveVisitor } from '@graphql-tools/utils'; | ||
import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils'; | ||
import { FunctionsRouter } from '../../Routers/FunctionsRouter'; | ||
|
||
export const definitions = gql` | ||
export const definitions = ` | ||
directive @resolve(to: String) on FIELD_DEFINITION | ||
directive @mock(with: Any!) on FIELD_DEFINITION | ||
`; | ||
|
||
const load = parseGraphQLSchema => { | ||
parseGraphQLSchema.graphQLSchemaDirectivesDefinitions = definitions; | ||
|
||
class ResolveDirectiveVisitor extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field) { | ||
field.resolve = async (_source, args, context) => { | ||
try { | ||
const { config, auth, info } = context; | ||
|
||
let functionName = field.name; | ||
if (this.args.to) { | ||
functionName = this.args.to; | ||
} | ||
|
||
return ( | ||
await FunctionsRouter.handleCloudFunction({ | ||
params: { | ||
functionName, | ||
}, | ||
config, | ||
auth, | ||
info, | ||
body: args, | ||
}) | ||
).response.result; | ||
} catch (e) { | ||
parseGraphQLSchema.handleError(e); | ||
const resolveDirective = schema => | ||
mapSchema(schema, { | ||
[MapperKind.OBJECT_FIELD]: fieldConfig => { | ||
const directive = getDirective(schema, fieldConfig, 'resolve')?.[0]; | ||
if (directive) { | ||
const { to: targetCloudFunction } = directive; | ||
fieldConfig.resolve = async (_source, args, context, gqlInfo) => { | ||
try { | ||
const { config, auth, info } = context; | ||
const functionName = targetCloudFunction || gqlInfo.fieldName; | ||
return ( | ||
await FunctionsRouter.handleCloudFunction({ | ||
params: { | ||
functionName, | ||
}, | ||
config, | ||
auth, | ||
info, | ||
body: args, | ||
}) | ||
).response.result; | ||
} catch (e) { | ||
parseGraphQLSchema.handleError(e); | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
parseGraphQLSchema.graphQLSchemaDirectives.resolve = ResolveDirectiveVisitor; | ||
|
||
class MockDirectiveVisitor extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field) { | ||
field.resolve = () => { | ||
return this.args.with; | ||
}; | ||
} | ||
} | ||
return fieldConfig; | ||
}, | ||
}); | ||
|
||
const mockDirective = schema => | ||
mapSchema(schema, { | ||
[MapperKind.OBJECT_FIELD]: fieldConfig => { | ||
const directive = getDirective(schema, fieldConfig, 'mock')?.[0]; | ||
if (directive) { | ||
const { with: mockValue } = directive; | ||
fieldConfig.resolve = async () => mockValue; | ||
} | ||
return fieldConfig; | ||
}, | ||
}); | ||
|
||
parseGraphQLSchema.graphQLSchemaDirectives.mock = MockDirectiveVisitor; | ||
parseGraphQLSchema.graphQLSchemaDirectives = schema => mockDirective(resolveDirective(schema)); | ||
}; | ||
|
||
export { load }; |
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