Graphql 没有为接口/联合定义解析器 - java

try*_*ard 6 java graphql graphql-java

我在使用这种方法添加解析器时遇到问题graphql

@RestController
@RequestMapping("/api/dictionary/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictionaryController {
    @Value("classpath:items.graphqls")
    private Resource schemaResource;
    private GraphQL graphQL;
    private final DictionaryService dictionaryService;

    @PostConstruct
    public void loadSchema() throws IOException {
        File schemaFile = schemaResource.getFile();
        TypeDefinitionRegistry registry = new SchemaParser().parse(schemaFile);
        RuntimeWiring wiring = buildWiring();
        GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry, wiring);
        graphQL = GraphQL.newGraphQL(schema).build();
    }

private RuntimeWiring buildWiring() {
    DataFetcher<List<DictionaryItemWithParentDto>> fetcher6 = dataFetchingEnvironment -> dictionaryService.getClaimSubType();

        return RuntimeWiring.newRuntimeWiring()
                .type("Query", typeWriting ->
                   typeWriting
                    .dataFetcher("getClaimSubType", fetcher6)
                    )
                .build();
    }


public List<DictionaryItemWithParentDto> getClaimSubType() {
    return dictionaryService.getClaimSubType();
   }
  }
Run Code Online (Sandbox Code Playgroud)

items.graphqls 文件内容:

type Query {
    getClaimSubType: [DictionaryItemWithParentDto]
}

type DictionaryItemWithParentDto {
    code: String!
    name: String
    parents: [DictionaryItemDto]
}

type DictionaryItemDto {
    code: String!
    name: String
    description: String
}
Run Code Online (Sandbox Code Playgroud)

在 Java 中,我有Vehicle接口和两个实现它的类:AirplaneCar. 当我添加到架构这一行时:

union SearchResult = Airplane | Car
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

There is no type resolver defined for interface / union 'Vehicle' type, There is no type resolver defined for interface / union 'SearchResult' type]}
Run Code Online (Sandbox Code Playgroud)

我不知道如何处理它。

如果相反,我添加:

interface Vehicle {
    maxSpeed: Int
}

type Airplane implements Vehicle {
    maxSpeed: Int
    wingspan: Int
}

type Car implements Vehicle {
    maxSpeed: Int
    licensePlate: String
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

errors=[There is no type resolver defined for interface / union 'Vehicle' type]
Run Code Online (Sandbox Code Playgroud)

我如何使用我的方法处理这些错误?有没有另一种方法来处理它?

编辑

我猜添加这些代码行可以解决问题的中途问题:

TypeResolver t =  new TypeResolver() {
    @Override
    public GraphQLObjectType getType(TypeResolutionEnvironment env) {
        Object javaObject = env.getObject();
        if (javaObject instanceof Car) {
            return env.getSchema().getObjectType("Car");
        } else if (javaObject instanceof Airplane) {
            return env.getSchema().getObjectType("Airplane");
        } else {
            return env.getSchema().getObjectType("Car");
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

并添加到RuntimeWiring构建器:

            .type("Vehicle", typeWriting ->
                    typeWriting
                            .typeResolver(t)
            )


    @PostMapping("getVehicle")
    public ResponseEntity<Object> getVehicleMaxSpeed(@RequestBody String query) 
   {
        ExecutionResult result = graphQL.execute(query);
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }
Run Code Online (Sandbox Code Playgroud)

询问时:

query {
    getVehicle(maxSpeed: 30) {
        maxSpeed

    }
}
Run Code Online (Sandbox Code Playgroud)

我明白了,maxSpeed但是当我添加时wingspan出现错误

Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
Run Code Online (Sandbox Code Playgroud)

我加了

getVehicle(maxSpeed: Int): Vehicle
Run Code Online (Sandbox Code Playgroud)

graphqls文件。我认为多态在这里会起作用。

try*_*ard 9

添加这些代码行可以解决问题:

TypeResolver t =  new TypeResolver() {
    @Override
    public GraphQLObjectType getType(TypeResolutionEnvironment env) {
        Object javaObject = env.getObject();
        if (javaObject instanceof Car) {
            return env.getSchema().getObjectType("Car");
        } else if (javaObject instanceof Airplane) {
            return env.getSchema().getObjectType("Airplane");
        } else {
            return env.getSchema().getObjectType("Car");
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

并添加到RuntimeWiring构建器:

            .type("Vehicle", typeWriting ->
                    typeWriting
                            .typeResolver(t)
            )


    @PostMapping("getVehicle")
    public ResponseEntity<Object> getVehicleMaxSpeed(@RequestBody String query) 
   {
        ExecutionResult result = graphQL.execute(query);
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }
Run Code Online (Sandbox Code Playgroud)

询问时:

query {
    getVehicle(maxSpeed: 30) {
        maxSpeed

    }
}
Run Code Online (Sandbox Code Playgroud)

我明白了,maxSpeed但是当我添加时wingspan出现错误

Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
Run Code Online (Sandbox Code Playgroud)

我加了

getVehicle(maxSpeed: Int): Vehicle
Run Code Online (Sandbox Code Playgroud)

graphqls文件。我认为多态在这里会起作用。

如果我想要来自子类的字段,我可以这样要求它们:

query {
    getVehicle(maxSpeed: 10) {
        maxSpeed
        ... on Airplane {
        wingspan
      }
        ... on Car {
        licensePlate
      }
    }
}
Run Code Online (Sandbox Code Playgroud)