最近、サブスクリプションマネージャは廃止されました。私はレゾルバをセットアップし、サブスクライブを定義して関数を実行する方法を知りたいと思います。graphqlサブスクリプションの動作例を知りたいです。
2
A
答えて
0
アポロ2.0にアップグレードする必要があります。公式の文書がまだ更新されていないため、私は最近Apollo 2.0の使用方法についてwrite-upを行っています。要するに
、あなたは今、その代わりSubscriptionServer
に直接渡されますgraphql
パッケージからクライアントとexecute
とsubscribe
に今apollo-link
を使用する必要があります。
あなたはまず右のバージョンで右のパッケージが必要になります。
npm install --save [email protected] [email protected] [email protected] [email protected] [email protected] graphql-subscriptions subscriptions-transport-ws apollo-server-express express graphql graphql-tools body-parser
あなたは流星を実行している場合、あなたはまた、必要がある場合があります。
meteor add apollo swydo:blaze-apollo swydo:graphql webapp
今のコードのために、次のようにしてMeteor製ですが、Expressのような他のサーバーの種類にも簡単に対応できます。 working example appをダウンロードすることもできます。サーバー上で
import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import WebSocketLink from 'apollo-link-ws';
import Cache from 'apollo-cache-inmemory';
import { getOperationAST } from 'graphql';
const httpUri = 'http://localhost:3000/graphql';
const wsUri = 'ws://localhost:3000/subscriptions';
const link = ApolloLink.split(
operation => {
const operationAST = getOperationAST(operation.query, operation.operationName);
return !!operationAST && operationAST.operation === 'subscription';
},
new WebSocketLink({
uri: wsUri,
options: {
reconnect: true, //auto-reconnect
// // carry login state (should use secure websockets (wss) when using this)
// connectionParams: {
// authToken: localStorage.getItem("Meteor.loginToken")
// }
}
}),
new HttpLink({ uri: httpUri })
);
const cache = new Cache(window.__APOLLO_STATE);
const client = new ApolloClient({
link,
cache
});
:クライアントで
import { WebApp } from 'meteor/webapp'; // Meteor-specific
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { createApolloServer, addCurrentUserToContext } from 'meteor/apollo'; // specific to Meteor, but you can always check out the Express implementation
import { makeExecutableSchema } from 'graphql-tools';
import resolvers from './resolvers'; // your custom resolvers
import typeDefs from './schema.graphql'; // your custom schema
// make schema executable
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
// any additional context you use for your resolvers, if any
const context = {};
// start a graphql server with Express handling a possible Meteor current user
// if you're not using Meteor, check out https://github.com/apollographql/apollo-server for instructions on how to create a server in pure Node
createApolloServer({
schema,
context
}, {
// // enable access to GraphQL API cross-domain (requires NPM "cors" package)
// configServer: expressServer => expressServer.use(cors())
});
// create subscription server
// non-Meteor implementation here: https://github.com/apollographql/subscriptions-transport-ws
new SubscriptionServer({
schema,
execute,
subscribe,
// // on connect subscription lifecycle event
// onConnect: async (connectionParams, webSocket) => {
// // if a meteor login token is passed to the connection params from the client,
// // add the current user to the subscription context
// const subscriptionContext = connectionParams.authToken
// ? await addCurrentUserToContext(context, connectionParams.authToken)
// : context;
// return subscriptionContext;
// }
}, {
server: WebApp.httpServer,
path: '/subscriptions'
});
resolvers.js
import { withFilter } from 'graphql-subscriptions'; // will narrow down the changes subscriptions listen to
import { PubSub } from 'graphql-subscriptions';
import { People } from '../imports/api/collections'; // Meteor-specific for doing database queries
const pubsub = new PubSub();
const resolvers = {
Query: {
person(obj, args, context) {
const person = People.findOne(args.id);
if (person) {
// Mongo stores id as _id, but our GraphQL API calls for id, so make it conform to the API
person.id = person._id;
delete person._id;
}
return person;
}
},
Mutation: {
updatePerson(obj, args, context) {
// You'll probably want to validate the args first in production, and possibly check user credentials using context
People.update({ _id: args.id }, { $set: { name: args.name, eyeColor: args.eyeColor, occupation: args.occupation } });
pubsub.publish("personUpdated", { personUpdated: args }); // trigger a change to all subscriptions to this person
// Note: You must publish the object with the subscription name nested in the object!
// See: https://github.com/apollographql/graphql-subscriptions/issues/51
return args;
}
},
Subscription: {
personUpdated: {
// See: https://github.com/apollographql/graphql-subscriptions#channels-mapping
// Take a look at "Channels Mapping" for handling multiple create, update, delete events
// Also, check out "PubSub Implementations" for using Redis instead of PubSub
// PubSub is not recommended for production because it won't work if you have multiple servers
// withFilter makes it so you can only listen to changes to this person instead of all people
subscribe: withFilter(() => pubsub.asyncIterator('personUpdated'), (payload, args) => {
return (payload.personUpdated.id===args.id);
})
}
}
};
export default resolvers;
schema.graphql
enum EyeColor {
brown
blue
green
hazel
}
type Person {
id: ID
name: String
eyeColor: EyeColor
occupation: String
}
type Query {
person(id: ID!): Person
}
type Mutation {
updatePerson(id: ID!, name: String!, eyeColor: EyeColor!, occupation: String!): Person
}
type Subscription {
personUpdated(id: ID!): Person
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
この記事の全文は、この中の投稿:How to get Apollo 2.0 working with GraphQL + subscriptionsにあります。 GraphQLサーバ+サブスクリプションとアポロ2.0を使用する方法を示す
例のアプリはここで見つけることができます:meteor-apollo2
関連する問題
- 1. Express-GraphQLとReact-Apolloを使用したGraphQLサブスクリプション
- 2. GraphQLサブスクリプション:サブスクリプションのイベントに基づいてオブジェクトを更新する方法
- 3. サブスクリプションのGraphQL Serverでエラーが発生しました
- 4. GraphQL/Sangria-Graphqlのserversideの指定されたフィールドを知る
- 5. ReactとApolloでgraphqlサブスクリプションを使用してリアルタイムアプリケーションを作成する
- 6. GraphqlのサブスクリプションをGraphql.jsで使用する方法
- 7. graphql-サブスクリプションとマイクロサービスによる認証(認証ではない!)パターン
- 8. GraphQLサブスクリプション:サブスクリプションリゾルバで公開オブジェクトを取得する方法
- 9. GraphQLサブスクリプションのみを別のサーバに分割する方法
- 10. Apolloサブスクリプションが動作しない
- 11. Apollo Serverサブスクリプションが動作しない
- 12. date()の動作方法を知りたい場合
- 13. Outlook Webhook通知サブスクリプション
- 14. サブスクリプションがない場合のデフォルトの動作ですか?
- 15. サブスクリプション後の通知from authorize.net
- 16. ユーレカピア認知の例が動作しない
- 17. GraphQLサブスクリプションでAsyncIteratorが使用されるのはなぜですか?
- 18. IOSサブスクリプション終了通知
- 19. iOSのRMStoreによる自動更新サブスクリプションの通知方法
- 20. gcloud alpha pubsubサブスクリプションのシークメソッドが見つかりませんでした例外
- 21. BLE通知サブスクリプションを取得する133
- 22. Azureはなぜリソースグループレベルでサブスクリプションであり、またアプリケーションサービスプランレベルでサブスクリプションですか?
- 23. サブスクリプションは内部的にどのように動作し、アングル4でサブスクリプションを使用するのですか?
- 24. Rxjs Subjectがサブスクリプション/サブスクリプションからサブスクリプションされたときに通知を受け取る方法
- 25. Xamarin form MessagingCenterのサブスクリプションが期待どおりに動作しない
- 26. リレー・サブスクリプションがネイティブ・レスキューで動作しない
- 27. 未知のプラグイン "Relay" - React、GraphQL、Relay
- 28. 作曲GraphQLは
- 29. アクション複数のサブスクリプションのケーブル通知
- 30. MVCルーティングがgodaddyではなく紺色のサブスクリプションで動作しています
私も興味があります。 –