【发布时间】:2019-11-14 00:22:38
【问题描述】:
使用python跨多个微服务实现GraphQL,有的使用Ariadne,有的使用graphene(和graphene-Django)。由于微服务架构,选择 Apollo Federation 合并来自不同微服务的模式。
使用 Ariadne,非常简单(首先是模式),还有一个小例子:
from ariadne import QueryType, gql, make_executable_schema, MutationType, ObjectType
from ariadne.asgi import GraphQL
query = QueryType()
mutation = MutationType()
sdl = """
type _Service {
sdl: String
}
type Query {
_service: _Service!
hello: String
}
"""
@query.field("hello")
async def resolve_hello(_, info):
return "Hello"
@query.field("_service")
def resolve__service(_, info):
return {
"sdl": sdl
}
schema = make_executable_schema(gql(sdl), query)
app = GraphQL(schema, debug=True)
现在,阿波罗联邦可以毫无问题地接受这一点:
const { ApolloServer } = require("apollo-server");
const { ApolloGateway } = require("@apollo/gateway");
const gateway = new ApolloGateway({
serviceList: [
// { name: 'msone', url: 'http://192.168.2.222:9091' },
{ name: 'mstwo', url: 'http://192.168.2.222:9092/graphql/' },
]
});
(async () => {
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor });
// server.listen();
server.listen(
3000, "0.0.0.0"
).then(({ url }) => {
console.log(`???? Server ready at ${url}`);
});
})();
为此,我可以在 3000 上对服务器运行 graphql 查询。
但是,使用石墨烯,尝试实现与 Ariadne 相同的功能:
import graphene
class _Service(graphene.ObjectType):
sdl = graphene.String()
class Query(graphene.ObjectType):
service = graphene.Field(_Service, name="_service")
hello = graphene.String()
def resolve_hello(self, info, **kwargs):
return "Hello world!"
def resolve_service(self, info, **kwargs):
from config.settings.shared import get_loaded_sdl
res = get_loaded_sdl() # gets the schema defined later in this file
return _Service(sdl=res)
schema = graphene.Schema(query=Query)
# urls.py
urlpatterns = [
url(r'^graphql/$', GraphQLView.as_view(graphiql=True)),
]
,... 现在导致阿波罗联盟出错:
GraphQLSchemaValidationError: Type Query 必须定义一个或多个字段。
当我检查这件事时,我发现 apollo 使用以下 graphql 查询调用微服务:
query GetServiceDefinition { _service { sdl } }
通过 Insomnia/Postman/GraphiQL 和 Ariadne 在微服务上运行它会得到:
{
"data": {
"_service": {
"sdl": "\n\ntype _Service {\n sdl: String\n}\n\ntype Query {\n _service: _Service!\n hello: String\n}\n"
}
}
}
# Which expanding the `sdl` part:
type _Service {
sdl: String
}
type Query {
_service: _Service!
hello: String
}
以及使用石墨烯的微服务:
{
"data": {
"_service": {
"sdl": "schema {\n query: Query\n}\n\ntype Query {\n _service: _Service\n hello: String\n}\n\ntype _Service {\n sdl: String\n}\n"
}
}
}
# Which expanding the `sdl` part:
schema {
query: Query
}
type Query {
_service: _Service
hello: String
}
type _Service {
sdl: String
}
所以,它们在定义如何获取sdl 时都是一样的,我检查了微服务响应,发现石墨烯响应也在发送正确的数据,
Json 响应“数据”等于:
execution_Result: OrderedDict([('_service', OrderedDict([('sdl', 'schema {\n query: Query\n}\n\ntype Query {\n _service: _Service\n hello: String\n}\n\ntype _Service {\n sdl: String\n}\n')]))])
那么,Apollo Federation 无法成功获取此微服务架构的原因可能是什么?
【问题讨论】:
-
联合服务需要实现federation spec。在 Apollo 中,这是通过使用
buildFederatedSchema函数来完成的。我不确定石墨烯supports anything like that. -
据我了解,在成功实现 Ariadne 之后,要使联合服务正常工作,架构中需要有一个
_service字段,类型为_Service,它具有一个字段sdl; whcih 将整个模式作为字符串返回。这很奇怪,因为这只是重复,本质上在模式中有一个字段,它返回所述模式。您是正确的,石墨烯本身并不支持这一点,但几乎每个后端都没有尝试使用 graphql,就像 Ariadne 我们只是定义他们的文档所说的需要。
标签: python-3.x graphql microservices apollo apollo-server