Ich arbeite daran, den neuen Apollo-Pubsub-Code zu lernen, anhand von Beispielen aus GitHunt-API und GitHunt-React. Als ich subscribe
auf dem Client aufrufen, erhalte ich die Konsole Protokollfehler:Apollo Publish & Subscribe Fehler: Feld kann nicht abgefragt werden?
"Cannot query field "createIM" on type "Subscription".
Ich habe versucht, sehr genau den Beispielcode zu folgen, aber ich bin etwas fehlt.
Wie kann ich diesen Fehler korrigieren?
CREATE_IM.JSX
class CreateIM extends React.Component {
[.....]
subscribe(fromID, toID, updateCommentsQuery) {
const SUBSCRIPTION_QUERY = gql`
subscription IMAdded($fromID: String!, $toID: String!, $msgText: String!){
createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
fromID
toID
msgText
}
}
`;
this.subscriptionObserver = this.props.client.subscribe({
query: SUBSCRIPTION_QUERY,
variables: { fromID: this.fromID, toID: this.toID },
}).subscribe({
next(data) {
debugger;
const newComment = data.commentAdded;
updateCommentsQuery((previousResult) => {
// if it's our own mutation, we might get the subscription result
// after the mutation result.
if (isDuplicateComment(newComment, previousResult.entry.comments)) {
return previousResult;
}
// update returns a new "immutable" list with the new comment
// added to the front.
return update(
previousResult,
{
entry: {
comments: {
$unshift: [newComment],
},
},
}
);
});
},
error(err) { debugger; console.error('err', err); }, //<== ERROR ON THIS LINE
});
}
SCHEMA
import Resolvers from '/imports/api/resolvers';
import Connectors from '/imports/api/db-connectors';
import { makeExecutableSchema } from 'graphql-tools';
const typeDefinitions = [`
type instant_message {
id: Int
fromID: String
toID: String
msgText: String
}
type Query {
instant_message(fromID: String, toID: String, msgText: String): [instant_message]
}
type Mutation {
createIM(
fromID: String!
toID: String!
msgText: String!
): instant_message
}
type Subscription {
# Subscription fires on every comment added
IMAdded(fromID: String!, toID: String!, msgText: String!): instant_message
}
schema {
query: Query,
mutation: Mutation
subscription: Subscription
}
`];
const executableSchema = makeExecutableSchema({
typeDefs: typeDefinitions,
resolvers: Resolvers,
connectors: Connectors,
logger: console,
});
export default executableSchema;
Versendungen (SERVER CODE)
import { print } from 'graphql-tag/printer';
import { PubSub, SubscriptionManager } from 'graphql-subscriptions';
import schema from '/imports/api/schema';
const pubsub = new PubSub();
const subscriptionManager = new SubscriptionManager({
schema,
pubsub,
setupFunctions: {
IMAdded: (options, args) => ({
IMAdded: comment => true, //not quite sure yet what validation needs to be here
}),
},
});
export { subscriptionManager, pubsub };
Resolvern
const resolvers = {
Query: {
instant_message(_, args) {
var ret = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues));
return ret;
}
},
Mutation: {
createIM(root, args, context) {
return Promise.resolve()
.then(() => (
connectors.IM.create(args)
))
.then(([args]) =>
connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues))
)
.then(comment => {
// publish subscription notification
pubsub.publish('IMAdded', comment);
return comment;
});
},
},
Subscription: {
IMAdded(fromID, toID, msgText) {
// the subscription payload is the comment.
return msgText;
},
}
};
Ich möchte sicherstellen, dass ich folge, was Sie richtig sagen. Sollte es '' 'sein subscribe (AbsenderID, EmpfängerID, updateCommentsQuery) { const SUBSCRIPTION_QUERY = gql' Abonnement IMAdded ($ AbsenderID: String !, $ EmpfängerID: String !, $ msgText: String) { } } ' ; '' – VikR
Ich kann mir ein paar andere Variationen vorstellen, die vielleicht das sind, was Sie beraten. Ich möchte sicherstellen, dass ich den richtigen Code bekomme, da ich Fehlermeldungen mit all den Variationen bekomme, die ich mir vorstellen kann, und ich möchte sicherstellen, dass ich diesen Teil richtig mache, um richtig weiterzumachen. :) – VikR
Ich habe Ihren Code nicht sorgfältig genug gelesen. Ich habe meine Antwort aktualisiert, um zu beheben, was ich denke, das eigentliche Problem ist. – helfer