Skip to content

Commit 2c40310

Browse files
douglasmuraokadavimacedo
authored andcommitted
GraphQL: /me pointers not working (#5745)
When using the `/me` endpoint to fetch the current user, it does not fetches data from any Pointer data type field, even though the field was defined in the GraphQL schema.
1 parent 3d63545 commit 2c40310

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,56 @@ describe('ParseGraphQLServer', () => {
31043104
expect(resultUserName).toEqual(userName);
31053105
expect(resultEmail).toEqual(email);
31063106
});
3107+
3108+
it('should return logged user including pointer', async () => {
3109+
const foo = new Parse.Object('Foo');
3110+
foo.set('bar', 'hello');
3111+
3112+
const userName = 'user1',
3113+
password = 'user1',
3114+
email = '[email protected]';
3115+
3116+
const user = new Parse.User();
3117+
user.setUsername(userName);
3118+
user.setPassword(password);
3119+
user.setEmail(email);
3120+
user.set('userFoo', foo);
3121+
await user.signUp();
3122+
3123+
await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();
3124+
3125+
const session = await Parse.Session.current();
3126+
const result = await apolloClient.query({
3127+
query: gql`
3128+
query GetCurrentUser {
3129+
users {
3130+
me {
3131+
objectId
3132+
sessionToken
3133+
userFoo {
3134+
bar
3135+
}
3136+
}
3137+
}
3138+
}
3139+
`,
3140+
context: {
3141+
headers: {
3142+
'X-Parse-Session-Token': session.getSessionToken(),
3143+
},
3144+
},
3145+
});
3146+
3147+
const {
3148+
objectId,
3149+
sessionToken,
3150+
userFoo: resultFoo,
3151+
} = result.data.users.me;
3152+
expect(objectId).toEqual(user.id);
3153+
expect(sessionToken).toBeDefined();
3154+
expect(resultFoo).toBeDefined();
3155+
expect(resultFoo.bar).toEqual('hello');
3156+
});
31073157
});
31083158

31093159
describe('Users Mutations', () => {

src/GraphQL/loaders/usersQueries.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,57 @@
11
import { GraphQLNonNull, GraphQLObjectType } from 'graphql';
2-
import UsersRouter from '../../Routers/UsersRouter';
3-
4-
const usersRouter = new UsersRouter();
2+
import getFieldNames from 'graphql-list-fields';
3+
import Parse from 'parse/node';
4+
import rest from '../../rest';
5+
import Auth from '../../Auth';
6+
import { extractKeysAndInclude } from './parseClassTypes';
57

68
const load = parseGraphQLSchema => {
79
const fields = {};
810

911
fields.me = {
1012
description: 'The Me query can be used to return the current user data.',
1113
type: new GraphQLNonNull(parseGraphQLSchema.meType),
12-
async resolve(_source, _args, context) {
14+
async resolve(_source, _args, context, queryInfo) {
1315
try {
14-
const { config, auth, info } = context;
15-
return (await usersRouter.handleMe({ config, auth, info })).response;
16+
const { config, info } = context;
17+
18+
if (!info || !info.sessionToken) {
19+
throw new Parse.Error(
20+
Parse.Error.INVALID_SESSION_TOKEN,
21+
'Invalid session token'
22+
);
23+
}
24+
const sessionToken = info.sessionToken;
25+
const selectedFields = getFieldNames(queryInfo);
26+
27+
const { include } = extractKeysAndInclude(selectedFields);
28+
const response = await rest.find(
29+
config,
30+
Auth.master(config),
31+
'_Session',
32+
{ sessionToken },
33+
{
34+
include: include
35+
.split(',')
36+
.map(included => `user.${included}`)
37+
.join(','),
38+
},
39+
info.clientVersion
40+
);
41+
if (
42+
!response.results ||
43+
response.results.length == 0 ||
44+
!response.results[0].user
45+
) {
46+
throw new Parse.Error(
47+
Parse.Error.INVALID_SESSION_TOKEN,
48+
'Invalid session token'
49+
);
50+
} else {
51+
const user = response.results[0].user;
52+
user.sessionToken = sessionToken;
53+
return user;
54+
}
1655
} catch (e) {
1756
parseGraphQLSchema.handleError(e);
1857
}

0 commit comments

Comments
 (0)