Skip to content

Commit

Permalink
refactor: upgrade GraphQL dependencies (#7970)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumouls committed Jun 10, 2022
1 parent 0dc2843 commit 0cd902b
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 334 deletions.
263 changes: 43 additions & 220 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
],
"license": "BSD-3-Clause",
"dependencies": {
"@graphql-tools/stitch": "6.2.4",
"@graphql-tools/utils": "6.2.4",
"@graphql-yoga/node": "2.6.0",
"@graphql-tools/merge": "8.2.11",
"@graphql-tools/schema": "8.3.11",
"@graphql-tools/utils": "8.6.10",
"@parse/fs-files-adapter": "1.2.2",
"@parse/push-adapter": "4.1.2",
"bcryptjs": "2.4.3",
Expand All @@ -31,10 +32,10 @@
"deepcopy": "2.1.0",
"express": "4.18.1",
"follow-redirects": "1.15.0",
"graphql": "15.8.0",
"graphql": "16.5.0",
"graphql-list-fields": "2.0.2",
"graphql-relay": "0.7.0",
"graphql-tag": "2.12.6",
"graphql-relay": "0.10.0",
"intersect": "1.0.1",
"jsonwebtoken": "8.5.1",
"jwks-rsa": "2.1.2",
Expand All @@ -58,6 +59,7 @@
"ws": "8.6.0"
},
"devDependencies": {
"graphql-tag": "2.12.6",
"@actions/core": "1.2.6",
"@apollo/client": "3.6.1",
"@babel/cli": "7.10.0",
Expand Down
6 changes: 4 additions & 2 deletions spec/AuthenticationAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,11 @@ describe('AuthenticationProviders', function () {
const provider = getMockMyOauthProvider();
Parse.User._registerAuthenticationProvider(provider);
await Parse.User._logInWith('myoauth');
expect(spy).toHaveBeenCalledWith({ usage: 'auth.myoauth', solution: 'auth.myoauth.enabled: true' });
expect(spy).toHaveBeenCalledWith({
usage: 'auth.myoauth',
solution: 'auth.myoauth.enabled: true',
});
});

});

describe('instagram auth adapter', () => {
Expand Down
8 changes: 4 additions & 4 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { getMainDefinition } = require('apollo-utilities');
const { createUploadLink } = require('apollo-upload-client');
const { SubscriptionClient } = require('subscriptions-transport-ws');
const { WebSocketLink } = require('@apollo/client/link/ws');
const { mergeSchemas } = require('@graphql-tools/schema');
const {
ApolloClient,
InMemoryCache,
Expand Down Expand Up @@ -10459,7 +10460,7 @@ describe('ParseGraphQLServer', () => {
});

describe('Custom API', () => {
describe('GraphQL Schema Based', () => {
describe('SDL based', () => {
let httpServer;
const headers = {
'X-Parse-Application-Id': 'test',
Expand Down Expand Up @@ -10582,7 +10583,7 @@ describe('ParseGraphQLServer', () => {
});
});

describe('SDL Based', () => {
describe('GraphQL Schema Based', () => {
let httpServer;
const headers = {
'X-Parse-Application-Id': 'test',
Expand Down Expand Up @@ -10879,8 +10880,7 @@ describe('ParseGraphQLServer', () => {
httpServer = http.createServer(expressApp);
parseGraphQLServer = new ParseGraphQLServer(parseServer, {
graphQLPath: '/graphql',
graphQLCustomTypeDefs: ({ autoSchema, stitchSchemas }) =>
stitchSchemas({ subschemas: [autoSchema] }),
graphQLCustomTypeDefs: ({ autoSchema }) => mergeSchemas({ schemas: [autoSchema] }),
});

parseGraphQLServer.applyGraphQL(expressApp);
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/SchemaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const requiredColumns = Object.freeze({
write: {
_Product: ['productIdentifier', 'icon', 'order', 'title', 'subtitle'],
_Role: ['name', 'ACL'],
}
},
});

const invalidColumns = ['length'];
Expand Down
54 changes: 10 additions & 44 deletions src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL/loaders/defaultGraphQLTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/loaders/defaultRelaySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const load = parseGraphQLSchema => {
}
},
obj => {
return parseGraphQLSchema.parseClassTypes[obj.className].classGraphQLOutputType;
return parseGraphQLSchema.parseClassTypes[obj.className].classGraphQLOutputType.name;
}
);

Expand Down
85 changes: 43 additions & 42 deletions src/GraphQL/loaders/schemaDirectives.js
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 };
26 changes: 13 additions & 13 deletions src/GraphQL/loaders/schemaTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
});

Expand Down

0 comments on commit 0cd902b

Please sign in to comment.