PostGraphile:makeExtendSchemaPlugin 中的 pgSettings user.id
PostGraphile: pgSettings user.id in makeExtendSchemaPlugin
是否可以在 PostGraphile 插件中访问 pgSettings
,特别是 makeExtendSchema
?这是我的中间件:
app.use(
postgraphile(
process.env.DATABASE_URL,
"public",
{
watchPg: true,
classicIds: true,
pgSettings: (req) => {
if (req.headers.cookie) {
const cookies = cookie.parse(req.headers.cookie);
return {
'user.id': cookies['next-auth.session-token']
}
}
return;
},
appendPlugins: [require('./add-cookie-plugin')]
}
)
);
我想要一个将 userId 添加到每个突变的插件,因为它在 cookie 中,我无法在 graphql 负载中发送它。如果我想要 SQL 命令,我看到 pg 可用。只想知道设置是否已经可用:
const { makeExtendSchemaPlugin, gql } = require("graphile-utils");
module.exports = makeExtendSchemaPlugin(build => {
const { pgSql: sql, inflection } = build;
return {
typeDefs: gql`
extend type Query {
userId: Int
}
`,
resolvers: {
Query: {
async userId() {
return // current_setting('user.id', true)
},
},
},
}
});
additionalGraphQLContextFromRequest
太棒了。但我必须创建整个解析器。相反,我创建了一个 custom mutation:
CREATE FUNCTION public.create_row(content text)
RETURNS public.rows
AS $$
INSERT INTO public.rows (user_id, content)
SELECT u.user_id, content FROM users u JOIN sessions s ON u.user_id=s.user_id WHERE s.session_token = current_setting('user.id', true)
RETURNING *;
$$ LANGUAGE sql VOLATILE STRICT;
是否可以在 PostGraphile 插件中访问 pgSettings
,特别是 makeExtendSchema
?这是我的中间件:
app.use(
postgraphile(
process.env.DATABASE_URL,
"public",
{
watchPg: true,
classicIds: true,
pgSettings: (req) => {
if (req.headers.cookie) {
const cookies = cookie.parse(req.headers.cookie);
return {
'user.id': cookies['next-auth.session-token']
}
}
return;
},
appendPlugins: [require('./add-cookie-plugin')]
}
)
);
我想要一个将 userId 添加到每个突变的插件,因为它在 cookie 中,我无法在 graphql 负载中发送它。如果我想要 SQL 命令,我看到 pg 可用。只想知道设置是否已经可用:
const { makeExtendSchemaPlugin, gql } = require("graphile-utils");
module.exports = makeExtendSchemaPlugin(build => {
const { pgSql: sql, inflection } = build;
return {
typeDefs: gql`
extend type Query {
userId: Int
}
`,
resolvers: {
Query: {
async userId() {
return // current_setting('user.id', true)
},
},
},
}
});
additionalGraphQLContextFromRequest
太棒了。但我必须创建整个解析器。相反,我创建了一个 custom mutation:
CREATE FUNCTION public.create_row(content text)
RETURNS public.rows
AS $$
INSERT INTO public.rows (user_id, content)
SELECT u.user_id, content FROM users u JOIN sessions s ON u.user_id=s.user_id WHERE s.session_token = current_setting('user.id', true)
RETURNING *;
$$ LANGUAGE sql VOLATILE STRICT;