Graphql return enum collection

tryingHard :

I want to return all values of enum using graphql. I have schema:

schema {
    query: Query
}

type Query {
    getDataTypes: [DictionaryType]
}


enum DictionaryType{
   RISK
   SALES_CHANNEL
   PERSON_TYPE
}

We have normal java enum:

public enum DictionaryType {
    RISK,
    SALES_CHANNEL,
    PERSON_TYPE
}

and Controller with configuration:

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<Set<DictionaryType>> fetcher3 = dataFetchingEnvironment -> {
            return dictionaryService.getDictionaryTypes();
        };

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

    @PostMapping("getDataTypes")
    public ResponseEntity<Object> getDataTypes(@RequestBody String query) {
        ExecutionResult result = graphQL.execute(query);
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }
}   

When i make POST to http://localhost:50238/getDataTypes with body:

{
    getDataTypes {

    }
}

I get "errorType": "InvalidSyntax", in response.

DavidG :

That's an invalid query as you have braces with no content (i.e. { }). Your schema suggests that the query should be much simpler though:

{ getDataTypes }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=153452&siteId=1