graphile / graphile/graphile.github.io

Example of changing from simple subscriptions to (equivalent) custom subscriptions

Aperta
#269 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
SCSS
Stelle
27
Fork
126
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

```js
// MySubscriptionPlugin.js

const base64 = (str) => Buffer.from(String(str)).toString("base64");
const nodeIdFromDbNode = (dbNode) => base64(JSON.stringify(dbNode));

const { makeExtendSchemaPlugin, gql, embed } = require("graphile-utils");

const listenTopic = async (args, _context, _resolveInfo) => {
const authCheckPassed = true; // TODO: implement your auth checks here
if (authCheckPassed) {
return `postgraphile:${args.topic}`;
} else {
throw new Error("You're not allowed to subscribe to this topic");
}
};

function filterFn(payload, variables, context, info) {
return true;
}

const MyPlugin = makeExtendSchemaPlugin((build) => {
const { extend, newWithHooks, pgSql: sql, $$isQuery, resolveNode } = build;

return {
typeDefs: gql`
type ListenPayload {
query: Query
relatedNode: Node
relatedNodeId: ID
}

extend type Subscription {
"""
Listen for any changes within the Platform with filter functionality
"""
listen(topic: String!, filter: String): ListenPayload @pgSubscription(
topic: ${embed(listenTopic)}
)
}
`,

resolvers: {
Subscription: { listen: { resolve: (event) => event } },
ListenPayload: {
query: () => $$isQuery,
relatedNodeId: (event) => nodeIdFromDbNode(event.__node__),
async relatedNode(event, _args, resolveContext, resolveInfo) {
const {
getDataFromParsedResolveInfoFragment,
} = resolveInfo.graphile.fieldContext;
return resolveNode(
nodeIdFromDbNode(event.__node__),
build,
{ getDataFromParsedResolveInfoFragment },
{},
resolveContext,
resolveInfo
);
},
},
},
};
});

module.exports = MyPlugin;
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.