I am fairly new to GraphQL and quite like the new schema language. I was just wondering why the resolve method is not a part of the it. It would be really awesome if there was a feature that would allow the resolve functionality to work with the new schema. Thanks
We have a package for this that we and some other companies have been using in production for a while, graphql-tools.
I think it's exactly what you're looking for, and even supports unions/interfaces, custom scalars, etc.
See an example here: https://github.com/apollostack/graphql-syntax#graphql-tools
Full docs here: http://dev.apollodata.com/tools/graphql-tools/generate-schema.html (although a bit outdated)
See a full example server here: https://github.com/apollostack/GitHunt-API/blob/master/api/schema.js
@achakravarty For more context: the resolvers aren't part of the GraphQL schema language, because they are implementation specific. Resolve functions contain code, which will look different in Python, Scala, Go, etc. The schema language is independent of the implementation.
@helfer Makes sense. But given that, I was wondering whether it would make sense to support a syntax that allows defining resolve methods and binding them separately like it does in grapql-tool as mentioned my @stubailo
For example:
type User {
name: getName(user: User) : String //here getName is a function that would take a user and return a String.
}
And build a way to attach a resolve method, ideally based on convention eg.
resolvers: {
User: {
getName: (user) => { return "Mr." + user.firstName + ' ' + user.lastName; }
}
}
And finally be able to bind them to the schema as:
schema.buildSchema(`
type User {
name: getName(user: User) : String
}
`)
.withResolvers(resolvers);
@achakravarty yeah, that's basically what graphql-tools does for javascript. It has a function called makeExecutableSchema, which takes a schema and resolve functions, and then returns an executable schema. I'm sure this could easily be built for other languages as well.
@helfer Cheers! Will refer that then. You can close this issue.
Most helpful comment
@helfer Makes sense. But given that, I was wondering whether it would make sense to support a syntax that allows defining resolve methods and binding them separately like it does in grapql-tool as mentioned my @stubailo
For example:
And build a way to attach a resolve method, ideally based on convention eg.
And finally be able to bind them to the schema as: