Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
feat(http): meteor user ID in http handler (#190)
Browse files Browse the repository at this point in the history
* Add successful test for Meteor.userId usage.

* Add failing Meteor.userId test for HTTP endpoint

* Invoke execute with a method invocation

This will make Meteor.userId work in resolvers when calling them from
the HTTP endpoint.
  • Loading branch information
jamiter authored Jun 5, 2018
1 parent a849378 commit cc14e06
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/server/createGraphQLMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execute, parse } from 'graphql';
import { contextToFunction } from './contextToFunction';
import { invokeDDP } from './invokeDDP';

export function createGraphQLMiddleware({
schema,
Expand All @@ -14,14 +15,14 @@ export function createGraphQLMiddleware({
operationName,
} = req.body;

const data = await execute(
const data = await invokeDDP(async () => execute(
schema,
parse(query),
{},
await createContext({ userId: req.userId }),
variables,
operationName,
);
), { userId: req.userId });

const json = JSON.stringify(data);

Expand Down
23 changes: 23 additions & 0 deletions lib/server/invokeDDP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DDP } from 'meteor/ddp';
import { Random } from 'meteor/random';

const { DDPCommon } = Package['ddp-common'];

// For details, please see https://github.com/meteor/meteor/issues/6388

function createInvocation(options) {
return new DDPCommon.MethodInvocation({
isSimulation: false,
setUserId: () => {},
unblock: () => {},
connection: null,
randomSeed: Random.id(),
...options,
});
}

export function invokeDDP(func, options) {
const invocation = createInvocation(options);

return DDP._CurrentMethodInvocation.withValue(invocation, func);
}
1 change: 1 addition & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var packages = [
'ecmascript',
'promise',
'webapp',
'random',
];

Package.describe({
Expand Down
21 changes: 21 additions & 0 deletions specs/client/apollo-link-ddp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ describe('DDPMethodLink', function () {
error: done,
});
});

it('returns the meteorUserId', function (done) {
const operation = {
query: gql`query { meteorUserId }`,
};

const observer = this.link.request(operation);

observer.subscribe({
next: ({ data }) => {
try {
chai.expect(data.meteorUserId).to.be.a('string');
chai.expect(data.meteorUserId).to.equal(this.userId);
done();
} catch (e) {
done(e);
}
},
error: done,
});
});
});

describe('when disconnected', function () {
Expand Down
11 changes: 11 additions & 0 deletions specs/client/apollo-link-meteor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ describe('MeteorLink', function () {
chai.expect(data.userId).to.be.a('string');
chai.expect(data.userId).to.equal(this.userId);
});

it('returns the meteorUserId', async function () {
const operation = {
query: gql`query { meteorUserId }`,
};

const { data } = await this.client.query(operation);

chai.expect(data.meteorUserId).to.be.a('string');
chai.expect(data.meteorUserId).to.equal(this.userId);
});
});
});
});
Expand Down
4 changes: 4 additions & 0 deletions specs/data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const resolvers = {
Query: {
foo: () => 'bar',
userId: (_, __, { userId } = {}) => userId,
// Using Meteor.userId() yourself is not recommended. Use the context userId.
// But to support a lot of Meteor packages it's useful, because they use it underwater.
// See https://github.com/apollographql/meteor-integration/issues/92
meteorUserId: () => Meteor.userId(),
ddpContextValue: (_, __, { ddpContext } = {}) => ddpContext,
},
Subscription: {
Expand Down
1 change: 1 addition & 0 deletions specs/data/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const typeDefs = [`
type Query {
foo: String
userId: String
meteorUserId: String
ddpContextValue: String
}
Expand Down

0 comments on commit cc14e06

Please sign in to comment.