refactor: upgrade GraphQL dependencies (#7970)

This commit is contained in:
Antoine Cormouls
2022-06-10 14:01:45 +02:00
committed by GitHub
parent 0dc2843503
commit 0cd902b8c2
10 changed files with 127 additions and 333 deletions

View File

@@ -1,8 +1,8 @@
import Parse from 'parse/node';
import { GraphQLSchema, GraphQLObjectType, DocumentNode, GraphQLNamedType } from 'graphql';
import { stitchSchemas } from '@graphql-tools/stitch';
import { mergeSchemas } from '@graphql-tools/schema';
import { mergeTypeDefs } from '@graphql-tools/merge';
import { isDeepStrictEqual } from 'util';
import { SchemaDirectiveVisitor } from '@graphql-tools/utils';
import requiredParameter from '../requiredParameter';
import * as defaultGraphQLTypes from './loaders/defaultGraphQLTypes';
import * as parseClassTypes from './loaders/parseClassTypes';
@@ -203,9 +203,8 @@ class ParseGraphQLSchema {
if (this.graphQLCustomTypeDefs) {
schemaDirectives.load(this);
if (typeof this.graphQLCustomTypeDefs.getTypeMap === 'function') {
// In following code we use underscore attr to avoid js var un ref
// In following code we use underscore attr to keep the direct variable reference
const customGraphQLSchemaTypeMap = this.graphQLCustomTypeDefs._typeMap;
const findAndReplaceLastType = (parent, key) => {
if (parent[key].name) {
@@ -280,51 +279,18 @@ class ParseGraphQLSchema {
this.graphQLSchema = await this.graphQLCustomTypeDefs({
directivesDefinitionsSchema: this.graphQLSchemaDirectivesDefinitions,
autoSchema: this.graphQLAutoSchema,
stitchSchemas,
graphQLSchemaDirectives: this.graphQLSchemaDirectives,
});
} else {
this.graphQLSchema = stitchSchemas({
schemas: [
this.graphQLSchemaDirectivesDefinitions,
this.graphQLAutoSchema,
this.graphQLSchema = mergeSchemas({
schemas: [this.graphQLAutoSchema],
typeDefs: mergeTypeDefs([
this.graphQLCustomTypeDefs,
],
mergeDirectives: true,
this.graphQLSchemaDirectivesDefinitions,
]),
});
this.graphQLSchema = this.graphQLSchemaDirectives(this.graphQLSchema);
}
// Only merge directive when string schema provided
const graphQLSchemaTypeMap = this.graphQLSchema.getTypeMap();
Object.keys(graphQLSchemaTypeMap).forEach(graphQLSchemaTypeName => {
const graphQLSchemaType = graphQLSchemaTypeMap[graphQLSchemaTypeName];
if (
typeof graphQLSchemaType.getFields === 'function' &&
this.graphQLCustomTypeDefs.definitions
) {
const graphQLCustomTypeDef = this.graphQLCustomTypeDefs.definitions.find(
definition => definition.name.value === graphQLSchemaTypeName
);
if (graphQLCustomTypeDef) {
const graphQLSchemaTypeFieldMap = graphQLSchemaType.getFields();
Object.keys(graphQLSchemaTypeFieldMap).forEach(graphQLSchemaTypeFieldName => {
const graphQLSchemaTypeField = graphQLSchemaTypeFieldMap[graphQLSchemaTypeFieldName];
if (!graphQLSchemaTypeField.astNode) {
const astNode = graphQLCustomTypeDef.fields.find(
field => field.name.value === graphQLSchemaTypeFieldName
);
if (astNode) {
graphQLSchemaTypeField.astNode = astNode;
}
}
});
}
}
});
SchemaDirectiveVisitor.visitSchemaDirectives(
this.graphQLSchema,
this.graphQLSchemaDirectives
);
} else {
this.graphQLSchema = this.graphQLAutoSchema;
}

View File

@@ -1210,12 +1210,12 @@ const loadArrayResult = (parseGraphQLSchema, parseClassesArray) => {
resolveType: value => {
if (value.__type === 'Object' && value.className && value.objectId) {
if (parseGraphQLSchema.parseClassTypes[value.className]) {
return parseGraphQLSchema.parseClassTypes[value.className].classGraphQLOutputType;
return parseGraphQLSchema.parseClassTypes[value.className].classGraphQLOutputType.name;
} else {
return ELEMENT;
return ELEMENT.name;
}
} else {
return ELEMENT;
return ELEMENT.name;
}
},
});

View File

@@ -39,7 +39,7 @@ const load = parseGraphQLSchema => {
}
},
obj => {
return parseGraphQLSchema.parseClassTypes[obj.className].classGraphQLOutputType;
return parseGraphQLSchema.parseClassTypes[obj.className].classGraphQLOutputType.name;
}
);

View File

@@ -1,8 +1,7 @@
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
`;
@@ -10,46 +9,48 @@ export const definitions = gql`
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);
}
};
}
};
}
}
return fieldConfig;
},
});
parseGraphQLSchema.graphQLSchemaDirectives.resolve = ResolveDirectiveVisitor;
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;
},
});
class MockDirectiveVisitor extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
field.resolve = () => {
return this.args.with;
};
}
}
parseGraphQLSchema.graphQLSchemaDirectives.mock = MockDirectiveVisitor;
parseGraphQLSchema.graphQLSchemaDirectives = schema => mockDirective(resolveDirective(schema));
};
export { load };

View File

@@ -29,19 +29,19 @@ const SCHEMA_FIELD = new GraphQLInterfaceType({
},
resolveType: value =>
({
String: SCHEMA_STRING_FIELD,
Number: SCHEMA_NUMBER_FIELD,
Boolean: SCHEMA_BOOLEAN_FIELD,
Array: SCHEMA_ARRAY_FIELD,
Object: SCHEMA_OBJECT_FIELD,
Date: SCHEMA_DATE_FIELD,
File: SCHEMA_FILE_FIELD,
GeoPoint: SCHEMA_GEO_POINT_FIELD,
Polygon: SCHEMA_POLYGON_FIELD,
Bytes: SCHEMA_BYTES_FIELD,
Pointer: SCHEMA_POINTER_FIELD,
Relation: SCHEMA_RELATION_FIELD,
ACL: SCHEMA_ACL_FIELD,
String: SCHEMA_STRING_FIELD.name,
Number: SCHEMA_NUMBER_FIELD.name,
Boolean: SCHEMA_BOOLEAN_FIELD.name,
Array: SCHEMA_ARRAY_FIELD.name,
Object: SCHEMA_OBJECT_FIELD.name,
Date: SCHEMA_DATE_FIELD.name,
File: SCHEMA_FILE_FIELD.name,
GeoPoint: SCHEMA_GEO_POINT_FIELD.name,
Polygon: SCHEMA_POLYGON_FIELD.name,
Bytes: SCHEMA_BYTES_FIELD.name,
Pointer: SCHEMA_POINTER_FIELD.name,
Relation: SCHEMA_RELATION_FIELD.name,
ACL: SCHEMA_ACL_FIELD.name,
}[value.type]),
});