Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom additional query #262

Open
federico-s opened this issue Apr 22, 2020 · 2 comments
Open

Create custom additional query #262

federico-s opened this issue Apr 22, 2020 · 2 comments

Comments

@federico-s
Copy link

federico-s commented Apr 22, 2020

Hi,
I need to add custom queries to those auto-generated from the schema.
I write my .graphqls file with only a query inside and a GraphQLQueryResolver to resolve the query but the query is not added in the schema and I can't call it.
I didn't define the type in the .graphqls file because the type is autogenerated from the db schema.
What am I doing wrong?

@federico-s
Copy link
Author

Could someone answer, please?

@chanhengseang3
Copy link
Contributor

chanhengseang3 commented May 26, 2020

i added a some code to do it:

public abstract class SchemaBuilderHelper {

    private final String path;

    public SchemaBuilderHelper(final String path) {
        this.path = path;
    }

    public GraphQLSchema buildSchema() {
        try (final var reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(path)))) {
            return new SchemaGenerator().makeExecutableSchema(
                    new SchemaParser().parse(reader),
                    buildWiring());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected abstract RuntimeWiring buildWiring();

}

then extends class:

@Component
public class XxxSchemaBuilder extends SchemaBuilderHelper {
    private static final String PATH = "/gql/xxx.graphqls";

    @Autowired
    private XxxDataFetcher xxxDataFetcher;

    public XxxSchemaBuilder() {
        super(PATH);
    }

    @Override
    protected RuntimeWiring buildWiring() {
        return RuntimeWiring.newRuntimeWiring()
                .type("Mutation", typeWiring ->
                        typeWiring
                                .dataFetcher("updateXxx", xxxDataFetcher.updateXxx())
                )
                .build();
    }
}

then register it in schema:

@Override
    public void configure(final GraphQLShemaRegistration registry) {
        registry.register(graphQLSchemaBuilder()
                .build());
        registry.register(xxxSchemaBuilder.build());
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants