From 9fa20dec49a2cf883f9f033281556eadd0bb707d Mon Sep 17 00:00:00 2001 From: Antoine Cormouls Date: Wed, 11 Mar 2020 18:26:51 +0100 Subject: [PATCH] Switch ACL to Relay Global Id --- spec/ParseGraphQLServer.spec.js | 19 ++++++++++++++++--- src/GraphQL/loaders/defaultGraphQLTypes.js | 3 ++- src/GraphQL/transformers/mutation.js | 4 ++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/spec/ParseGraphQLServer.spec.js b/spec/ParseGraphQLServer.spec.js index e9cbc64dc2..20ddf5f775 100644 --- a/spec/ParseGraphQLServer.spec.js +++ b/spec/ParseGraphQLServer.spec.js @@ -17,6 +17,7 @@ const { SubscriptionClient } = require('subscriptions-transport-ws'); const { WebSocketLink } = require('apollo-link-ws'); const ApolloClient = require('apollo-client').default; const gql = require('graphql-tag'); +const { toGlobalId } = require('graphql-relay'); const { GraphQLObjectType, GraphQLString, @@ -8284,6 +8285,18 @@ describe('ParseGraphQLServer', () => { await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear(); + const gqlUser = ( + await apolloClient.query({ + query: gql` + query getUser($id: ID!) { + user(id: $id) { + id + } + } + `, + variables: { id: user.id }, + }) + ).data.user; const { data: { createSomeClass }, } = await apolloClient.mutate({ @@ -8317,7 +8330,7 @@ describe('ParseGraphQLServer', () => { fields: { ACL: { users: [ - { userId: user.id, read: true, write: true }, + { userId: gqlUser.id, read: true, write: true }, { userId: user2.id, read: true, write: false }, ], roles: [ @@ -8334,13 +8347,13 @@ describe('ParseGraphQLServer', () => { __typename: 'ACL', users: [ { - userId: user.id, + userId: toGlobalId('_User', user.id), read: true, write: true, __typename: 'UserACL', }, { - userId: user2.id, + userId: toGlobalId('_User', user2.id), read: true, write: false, __typename: 'UserACL', diff --git a/src/GraphQL/loaders/defaultGraphQLTypes.js b/src/GraphQL/loaders/defaultGraphQLTypes.js index 581a59a45a..8a59089e56 100644 --- a/src/GraphQL/loaders/defaultGraphQLTypes.js +++ b/src/GraphQL/loaders/defaultGraphQLTypes.js @@ -14,6 +14,7 @@ import { GraphQLBoolean, GraphQLUnionType, } from 'graphql'; +import { toGlobalId } from 'graphql-relay'; import { GraphQLUpload } from 'graphql-upload'; class TypeValidationError extends Error { @@ -553,7 +554,7 @@ const ACL = new GraphQLObjectType({ Object.keys(p).forEach(rule => { if (rule !== '*' && rule.indexOf('role:') !== 0) { users.push({ - userId: rule, + userId: toGlobalId('_User', rule), read: p[rule].read ? true : false, write: p[rule].write ? true : false, }); diff --git a/src/GraphQL/transformers/mutation.js b/src/GraphQL/transformers/mutation.js index d2f3c8b7b5..3bb05e249f 100644 --- a/src/GraphQL/transformers/mutation.js +++ b/src/GraphQL/transformers/mutation.js @@ -99,6 +99,10 @@ const transformers = { } if (value.users) { value.users.forEach(rule => { + const globalIdObject = fromGlobalId(rule.userId); + if (globalIdObject.type === '_User') { + rule.userId = globalIdObject.id; + } parseACL[rule.userId] = { read: rule.read, write: rule.write,