Express-Graphql はcontext
、最後の引数としてすべてのリゾルバー関数に渡されるオプションをサポートしています。Parse セッション トークンをヘッダーとして任意の/graphql
リクエストに渡し、それを次context
のように転送できます。
import graphqlHTTP from 'express-graphql';
const app = express();
app.use('/graphql', graphqlHTTP((req) => ({
// ...
context: {
parseSessionToken: req.headers['parse-session-token'],
},
})));
リゾルバー関数内で、クエリでこのトークンを使用して、適切な ACL が適用されるようにすることができます。
function resolve (_, args, context) {
return new Parse.Query('MyClass')
.first({ sessionToken: context.parseSessionToken });
}
クライアント側でリレーを使用している場合は、次のRelay.injectNetworkLayer
ようにこのヘッダーを含めるために使用します。
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('http://example.com/graphql', {
headers: {
'parse-session-token': Parse.User.current().getSessionToken(),
},
})
);
Apollo Client を使用createNetworkInterface
している場合は、ミドルウェア関数で使用してヘッダーを提供します。
import ApolloClient, { createNetworkInterface } from 'apollo-client';
const networkInterface = createNetworkInterface('http://example.com/graphql');
networkInterface.use([{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {}; // Create the header object if needed.
}
req.options.headers['parse-session-token'] = Parse.User.current().getSessionToken();
next();
}
}]);
const client = new ApolloClient({
networkInterface,
});