Express-Graphql supports a context
optionすべてのリゾルバ関数の最後の引数として渡されます。あなたはどの/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(),
},
})
);
あなたはヘッダを供給するミドルウェア機能で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,
});
おかげで@namuol、私はリレーを使用していると私は、ユーザのをつかむために、クライアント側でのログイン機能を追加したいですトークン。これを行う簡単な方法はありますか?明示的に別のAPIを作成する必要がありますか、それを行うにはgraphQLを使用できますか? –