首页 > 解决方案 > 将我的 spring Boot Graphql 连接到 Apollo Gateway

问题描述

我正在尝试使用 Spring Boot 开发将公开 GraphQL 的微服务,并将它们全部连接到 Apollo Gateway,但出现以下错误:

UnhandledPromiseRejectionWarning:错误:Apollo 服务器需要现有的模式、模块或 typeDefs

这是我附上的代码

客户端解析器

@Component
public class ClientResolver implements GraphQLQueryResolver {

    /** The Constant logger. */
    private static final Logger logger = LoggerFactory.getLogger(ClientResolver.class);

    /** The client repository. */
    private ClientRepository clientRepository;

    /**
     * Instantiates a new client resolver.
     *
     * @param clientRepository the client repository
     */
    public ClientResolver(ClientRepository clientRepository) {
        this.clientRepository = clientRepository;
    }

    /**
     * Find a Client by any field.
     *
     * @param client the client
     * @return the iterable
     */
    public Iterable<Client> find(Object client) {

        ObjectMapper mapper = new ObjectMapper();
        Client clientMapped = mapper.convertValue(client, Client.class);

        ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues().withIgnoreCase();

        Example<Client> exampleClient = Example.of(clientMapped, matcher);

        Iterable<Client> clientFromDb = clientRepository.findAll(exampleClient);

        if (clientFromDb != null) {

            for (Client c : clientFromDb) {
                logger.info("microStrategy getIdClient {}", c.getClientRefId());
            }
        }

        return clientFromDb;
    }
}

客户端架构

scalar Object

type Client {

 clientRefId : String

 agencyMarketingCampaignExclusionFlag : Boolean

 clientActiveFlag : Boolean
 clientSegmentationMosaicGeocoding : String

 clientSegmentationMosaicGroup : String

 clientSegmentationMosaicIlotCode : String

 clientSegmentationMosaicIlotDetail : String

 clientSegmentationMosaicInseeCode : String

 clientSegmentationMosaicIrisCode : String

 clientSegmentationMosaicLatitude : String

 clientSegmentationMosaicLongitude : String

 clientSegmentationMosaicType : String


 clientSegmentationMosaicWealth : Int

 clientSegmentationMosaicX : String

 clientSegmentationMosaicY : String

 clientSegmentationPotentialStatus : String

 clientSegmentationUrbanCode : String

 clientSeniority : Int

 clientSexCode : String

 clientSirenActive : Boolean

 clientSirenCode : String

 clientSirenInital : String

 clientSiretAssignedAutomaticallyFlag : Boolean

 clientSiretAssignedBySirenFlag : Boolean

 clientSiretCode : String

 clientSiretInital : String

 clientSiretSource : String

 clientSpecificFlag : Boolean

 clientTenantFlag : Boolean

 clientTitle : String

 clientType : String

 clientTypeCode : String

 clientUnsubscribeFlag : Boolean

 clientWealthyFlag : Boolean

 entrepriseName : String
}

type Query {
    find(client : Object): [Client]

}

schema {
  query: Query

}

网关.js


const { ApolloServer } = require('apollo-server');

const { ApolloGateway } = require('@apollo/gateway');

const gateway = new ApolloGateway({
    serviceList: [
      { name: "client", url: "http://localhost:8081/graphql" },

      // { name: "inventory", url: "http://localhost:4004/graphql" }
    ]
  });

  (async () => {
    const { schema, executor } = await gateway.load();

    const server = new ApolloServer({ schema, executor });

    server.listen().then(({ url }) => {
      console.log(` Server ready at ${url}`);
    });
  })();

错误信息

{
  message: "Validation error of type FieldUndefined: Field '_service' in type 'Query' is undefined @ '_service'",
  locations: [ { line: 1, column: 30, sourceName: null } ],
  description: "Field '_service' in type 'Query' is undefined",
  validationErrorType: 'FieldUndefined',
  queryPath: [ '_service' ],
  errorType: 'ValidationError',
  path: null,
  extensions: null
} 0 [
  {
    message: "Validation error of type FieldUndefined: Field '_service' in type 'Query' is undefined @ '_service'",
    locations: [ [Object] ],
    description: "Field '_service' in type 'Query' is undefined",
    validationErrorType: 'FieldUndefined',
    queryPath: [ '_service' ],
    errorType: 'ValidationError',
    path: null,
    extensions: null
  }
]
[INFO] Tue Feb 25 2020 15:25:52 GMT+0100 (GMT+01:00) apollo-gateway: Gateway successfully loaded schema.
        * Mode: unmanaged
(node:70412) UnhandledPromiseRejectionWarning: Error: Apollo Server requires either an existing schema, modules or typeDefs
    at ApolloServer.initSchema (c:\NWLS\workspace-ASK\federation-demo-master\node_modules\apollo-server-core\dist\ApolloServer.js:240:23)

在 Java 控制台中我有这个消息

Query failed to validate : 'query GetServiceDefinition { _service { sdl } }'

有什么想法吗?

标签: graphql-javaapollo-federationapollo-gateway

解决方案


推荐阅读